问题1:如何添加字典与询问用户,I.E.输入(“写运动员的名字”),输入(“写经过的时间!”)按值排序并正确打印键和值。 PYTHON
问题2: 我想作这样的输出。 底下我的代码....草稿:)
import operator
字典
runners = {"John": 9, "Mike": 2, "Venera": 4}
输出:
1st. "Entered runner name ", came at, "input time"
2nd. "Entered runner name ", came at, "input time"
3rd. "Entered runner name ", came at, "input time"
下面就从排序至少更
s = sorted(runners.items(), key=operator.itemgetter(1))
print(s)
示例会让您充分理解我的问题。所以,示例代码是这样的,我只是想让它更容易。
names =[]
array_times = []
循环它的问题,在范围(3)不写一堆投入 对于我:
name=input("Please write the name of Runner!")
#Appending name to the array name above!
names.append(name)
time_elapsed = float(input("Please write the time you spend!"))
#Appending it to the array above!
array_times.append(time_elapsed)
# Just to make it easier to understand, assigning the values in arrays above to variables.
runner1= names[0]
runner2=names[1]
runner3=names[2]
time1=array_times[0]
time2=array_times[1]
time3=array_times[2]
写作的if-else语句来检查哪个是哪个地方。
if time1<time2 and time1<time3 and time2<time3:#First winner, second in second, third is last.
print (runner1, "wins! His time is", time1, runner2, "is second place. His time is", time2, "and ", runner3, "is third place. his time is ", time3)
elif time2<time1 and time2<time3 and time1<time3:#Second winner, first in second place, third is last.
print (runner2, "wins! His time is", time2, runner1, "is second place. His time is", time1, "and ", runner3, "is third place. his time is ", time3)
elif time3<time1 and time3<time2 and time1<time2:#Third winner, First is second, second is last.
print (runner3, "wins! His time is", time3, runner1, "is second place. His timae is", time1, "and ", runner2, "is third place. his time is ", time2)
elif time3<time1 and time3<time2 and time2<time1:#Third winner, second is second, first is last.
print (runner3, "wins! His time is", time3, runner2, "is second place. His time is", time2, "and ", runner1, "is third place. his time is ", time1)
elif time2<time1 and time2<time3 and time3<time1:#Third winner, second is second, first is last.
print (runner2, "wins! His time is", time2, runner3, "is second place. His time is", time
* *一个问题,请。 –