在python中为计算机科学做一个赋值,我的for循环重复了一次以上的部分

时间:2014-05-30 04:24:25

标签: python

这是我的计算机科学课程的代码,我正在制作一个你必须猜测城市的猜谜游戏,我将会有一长串有提示的城市,但是当我现在播放时它会重复相同的城市偶尔两次,它应该只运行5个城市的代码部分。我知道它拥有的城市越多,重复的可能性就越小,但我只会有大约20个城市,所以我想知道如何阻止它重复一次城市两次。 Ps:我不知道如何使用这个网站所以我不知道我是否正确地问了这个问题我认为缩进也搞砸了。

代码:

import random
for counter in range(0,5):
Cities = random.randint (1,9)

if Cities == 1:
print("This city is the fourth largest city in South Korea")
City1="Daegu"
guess1= input() 
if guess1!=City1:
    print("The City you are looking for is the birthplace of Leo Hong")
    guess1=input()
if guess1!=City1:
    print("Alright this is your last guess, this city begins with a D")
    guess1=input()
if guess1!=City1:
    print("You lose how could you not guess that")
    exit() 
if guess1==City1:
    print("Congrats worthless player you got this city, the next one is difficult see if you can get it")

if Cities == 2:
    print("This city is a small farm filled town north of Burlington and Waterdown")
    City2="Carlisle"
    guess2= input()
    if guess2!=City2:
        print("The City you are looking for is where Connor lives")
        guess2=input()
    if guess2!=City2:
        print("Alright this is your last guess, this city begins with a C")
        guess2=input()
    if guess2!=City2:
        print("You lose get out of my sight at once")
        exit()
    if guess2==City2:
        print("Wow worthless player aren't you smart you got this city, we'll see if you can get this next city")

if Cities == 3:
    print("This city is the largest city in Afghanistan")
    City3="Kabul"
    guess3= input()
    if guess3!=City3:
        print("This city is کا‌‌‌بل")
        guess3=input()
    if guess3!=City3:
        print("Alright this is your last guess, this city begins with a K")
        guess3=input()
    if guess3!=City3:
        print("You lose get out of my sight at once filthy pleb")
        exit()
    if guess3==City3:
        print("amazing worthless player you're smart, we'll see if you can get this next city")

if Cities == 4:
    print("This city is a tiny town in Texas with a sick name")
    City4="Happy"
    guess4= input()
    if guess4!=City4:
        print("Their motto is, A town without a frown")
        guess4=input()
    if guess4!=City4:
        print("Alright this is your last guess, this town is named after a mood")
        guess4=input()
    if guess4!=City4:
        print("You lose get out of my sight at once filthy pleb")
        exit()
    if guess4==City4:
        print("great worthless player you're genius like Neil, we'll see if you can get the next city")

if Cities == 5:
    print("now this city is a small town in Iowa with a physics related name")
    City5="Gravity"
    guess5= input()
    if guess5!=City5:
        print("Their motto is were down to earth")
        guess5=input()
    if guess5!=City5:
        print("Your last hint is 9.81")
        guess5=input()
    if guess5!=City5:
        print("You lose get out of my sight at once filthy pleb")
        exit()
    if guess5==City5:
        print("great worthless player youve completed the city Gravity, lets see whats next")

if Cities == 6:
    print("This city is a large city in somalia")
    City6="Mogadishu"
    guess6= input()
    if guess6!=City6:
        print("It is the setting of Black Hawk Down")
        guess5=input()
    if guess6!=City6:
        print("It is on the coast of somalia")
        guess6=input()
    if guess6!=City6:
        print("You lose get out of my sight at once filthy pleb")
        exit()
    if guess6==City6:
        print("great worthless player youve got Mogadishu lets see whats next")

if Cities == 7:
    print("This city is a large city in Mongolia")
    City7="Ulaanbaatar"
    guess7= input()
    if guess7!=City7:
        print("It is an independant municipality")
        guess7=input()
    if guess7!=City7:
        print("It is the capital of Mongolia")
        guess7=input()
    if guess7!=City7:
        print("You lose get out of my sight at once filthy pleb")
        exit()
    if guess7==City7:
        print("great worthless player youve got Ulaanbaatar lets see whats next")

if Cities == 8:
    print("This one is a small community in Arkansas, USA")
    City8="Toadsuck"
    guess8= input()
    if guess8!=City8:
        print("this close relative of the frog sucks")
        guess8=input()
    if guess8!=City8:
        print("The answer is ____suck where the blank is the relative of the frog")
        guess8=input()
    if guess8!=City8:
        print("You lose get out of my sight at once filthy pleb")
        exit()
    if guess8==City8:
        print("lmao that one was funny great worthless player youve got Toadsuck lets see whats next")

if Cities == 9:
    print("This one is a village in Connecticut")
    City9="Gaylordsville"
    guess9= input()
    if guess9!=City9:
        print("this is the ville of the main character in Meet the Fockers")
        guess9=input()
    if guess9!=City9:
        print("in Litchfield county")
        guess9=input()
    if guess9!=City9:
        print("You lose get out of my sight at once filthy pleb")
        exit()
    if guess9==City9:
        print("good for you worthless player youve got Gaylordsville, lets see whats next")

1 个答案:

答案 0 :(得分:2)

问题在于,在每次迭代中,您都会使用random.randint来进行下一次猜测。不要使用它,试试这个:

for city in random.sample(range(NUM_CITIES), NUM_ROUNDS):
    ...

这会从NUM_ROUNDS中选择一个随机range(NUM_CITIES)值。请注意,这允许city为0到NUM_CITIES - 1而不是1到NUM_CITIES。你可以使用range(1, NUM_CITIES+1),如果你真的希望它从1开始,但是我建议你习惯从0开始,因为这是编程的标准。