問題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
* *一個問題,請。 –