-1

我需要这个程序来打印出你猜对和错的次数。我有一本城市和国家的字典(称为 ccdict),例如“英国”:“伦敦”。这是代码:

def choices (Correct):
    list=[Correct]
    f=0
    cities=ccdict.values()
    while f + 1<5:
        f=f+1
        list.append(cities[random.randint(0, len(cities))])
    list.sort()
    return list


countries = ccdict.keys()
randcountry = countries[random.randint(0, len(countries))]
print "What is the capital of " + randcountry + "?"
print "If you want to stop this game, type 'exit'"
randlist = choices(ccdict[randcountry])

for i in range(0,5):
        print str(i+1) + " - " + randlist[i]

input=raw_input("Give me the number of the city that you think is the capital of " + randcountry + " ")

if input == "exit":                               
    print "Now exiting game!" + sys.exit()       

elif  randlist[int(input)-1] == ccdict[randcountry]:
    print "Correct"

else:
    print "WRONG! The capital of " + randcountry + " is " + ccdict[randcountry]
4

1 回答 1

5

移动你的int()直到你的测试if

input = raw_input("Give me the number of the city that you think is the capital of " + randcountry + " ")

if input == "exit":
    print "Now exiting game!"
    sys.exit()

elif  randlist[int(input) - 1] == ccdict[randcountry]:
    print "Correct"

请注意上述版本如何仅input在最后一分钟转换为整数。

于 2013-06-12T17:02:54.257 回答