在Python中循环4次

时间:2015-06-23 19:20:21

标签: python loops

我试图将这个区块循环4次,以便最终显示出第1名,第2名,第3名和第4名的选手。有人可以帮忙吗?列表 - contestants

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")

1 个答案:

答案 0 :(得分: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。祝你好运。