2015-06-23 98 views
-1

我試圖循環這個塊4次,所以它顯示在第一,第二,第三和第四的參賽者結束。有人可以幫忙嗎?列表 - contestants在Python中循環4次

total = 0 

contestants = ["Vera Lynn" , "Billy Halliday", "Frank Sinatra"] 
contestant_name = input(" What's Your Name: \n ") # Asking For The Contestants Name 

print("Judges make your votes\n") 

for vote in range(0, 4): # Making A Loop 
    judge = input("Yes Or No: ") 
    if judge == 'Yes': 
     total = total + 1 
     print(total) 

if total >= 2: 
    print("Well Done ", contestant_name, " you have", total, "votes." + 
      " you are moving on to the next round") 
else: 
    print("Unlucky", contestant_name, " you have", total, "votes." + 
      " Unfortuanely you are not moving to the next round") 
+3

期望的輸出和電流輸出是什麼? – Zenadix

+0

如果你想整個事情做4次,看看誰來到了哪個地方,你必須重新調整一下。字典將有助於跟蹤每個人的分數;一些檢查,以確保參賽者不會兩次;等等 – maccartm

回答

0

不能完全確定這是否是你要什麼 - 我同意別人需要的代碼進行重組,您應該包括當前和預期的輸出。這就是說我希望這將成爲你的一個很好的起點。

contestants = {"Vera Lynn": 0, "Billy Halliday": 0, "Frank Sinatra": 0} 

contestants[input("What's your name?")] = 0 

print("Judges make your votes\n") 
for name in contestants.keys(): 
    print("judging %s" % name) 
    for vote in range(0, 4): 
     vote = input("Yes or No: ") 
     if vote == "Yes": 
      contestants[name] += 1 
    if contestants[name] >= 2: 
     print("Well Done ", name, " you have", contestants[name], "votes." +" you are moving on to the next round") 
    else: 
     print("Unlucky", name, " you have", contestants[name], "votes." + " Unfortuanely you are not moving to the next round") 


# to place and print them: 
placing = sorted(contestants, key=contestants.get) 
for i, val in enumerate(placing): 
    print("%s was number %d" % (val,i+1)) 

我知道這是非常基本的,有一些問題,但希望它可以幫助您更好地理解基礎知識。如果您想了解更多信息,我建議您撥打Learn Python The Hard Way。祝你好運。