2016-10-02 71 views
-2

問題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 
+2

* *一個問題,請。 –

回答

0

python3

import operator 

runners = {} 

for x in range(3): 
    name = input("name: ") 
    elapsed_time = int(input("time: ")) 
    runners[name] = elapsed_time 

templates = [ 
    "{name} wins! His time is {time}.", 
    "{name} is in second place. His time is {time}", 
    "and {name} is in third place. His time is {time}" 
] 

for idx, (name, elapsed_time) in enumerate(sorted(runners.items(), key=operator.itemgetter(1))): 
    print(templates[idx].format(name=name, time=elapsed_time), end=" ") 
print() 
+0

這適用於我。謝謝。但。 你能解釋一下你的代碼嗎? (runners.items(),key = operator.itemgetter(1))): print(templates [idx])。格式(名稱=名稱,時間= elapsed_time),結束=「」) print() –

+0

1)它只是'name'與'時間'在'runners'字典''https://docs.python.org/ 3 /庫/ stdtypes。html#dict 2)'runners'按值(時間)排序,'enumerate'返回迭代的當前步驟,0,1,2在你的情況下,然後你用這個'idx'請求'templates'進行字符串和格式化你的跑步者的當前「位置」 第一次(第一次)迭代'idx'將爲0,所以'templates [0]'將返回第一個字符串 - '「{name}勝利!他的時間是{time}。」 ',在下一次迭代中(第二位)'idx'將爲1,我們將從'templates'等第二個字符串 –

0

如蟒提到doc

最好是認爲一個字典作爲一組無序關鍵的:值對,與該鍵是獨特的要求(一個字典內)

使用OrderedDict

from collections import OrderedDict 

no_of_runners = int(input()) 

runners = dict() 
for i in range(no_of_runners): 
    name = input() 
    time = float(input()) 
    runners[name] = time 
sorted_runners = OrderedDict(sorted(runners.items(), key=lambda t: t[1])) 

runners_list = [(i,j) for i,j in sorted_runners.items()] 

print (runners_list[0][0], "wins! His time is", runners_list[0][1], runners_list[1][0], "is in second place. His time is", runners_list[1][1], "and ", runners_list[2][0], "is in third place. his time is ", runners_list[2][1]) 
0

的Python 2.7

runners={} 
for i in range(3): 
    name=raw_input("Please write the name of Runner!") 
    time_elapsed = float(raw_input("Please write the time you spend!")) 
    runners[name]=time_elapsed 
position=[] 
for name, elapsed in sorted(runners.iteritems(), key=lambda elapsed: elapsed[1]): 
    position.append((name, elapsed)) 
print position[0][0], "wins! His time is", position[0][1],'\n', position[1][0], "is second. His time is", position[1][1], "\n", position[2][0], "is third. His time is ", position[2][1] 

Python3

runners={} 
for i in range(3): 
    name=input("Please write the name of Runner!") 
    time_elapsed = float(input("Please write the time you spend!")) 
    runners[name]=time_elapsed 
position=[] 
for name, elapsed in sorted(runners.items(), key=lambda elapsed: elapsed[1]): 
    position.append((name, elapsed)) 
print ('\n',position[0][0], "wins! His time is", position[0][1],'\n', position[1][0], "is second. His time is", position[1][1], "\n", position[2][0], "is third. His time is ", position[2][1])