簡體   English   中英

While循環在Python中不起作用

[英]While loop not working in Python

while循環無法正常工作。 Again變量將決定是否將再次執行while循環。 如果再次= 1,則將執行while循環,並且程序將再次運行。 如果再次= 0,則不會。

由於某種原因,再次始終為= 1,因此無論如何,總是執行while循環。 有人注意到代碼中的錯誤嗎?

 score = 0
 loops = 0
 again = 1
 while (again != 0):
    import random
    real = random.randint(1,9)
    fake1 = random.randint(1,9)
    fake2 = random.randint(1,9)
    comb = random.randint(1,9)
    rep = 0
    guess = 0

    if (comb == 1 or comb == 2 or comb == 3):
        print(real, fake1, fake2)
        loops += 1
        guess = int(input("Choose between these numbers"))
        if (guess == real):
            score += 1
            print ("Congrats!")
        else:
            print ("Wrong, better luck next time!")
    if (comb == 4 or comb == 5 or comb == 6):
        print (fake1, fake2, real)
        loops += 1
        guess = int(input("Choose between these numbers"))
        if (guess == real):
            score += 1
            print ("Congrats!")
        else:
            print ("Wrong, better luck next time!")

    if (comb == 7 or comb == 8 or comb == 9):
        print (fake2, real, fake1)
        loops += 1
        guess = int(input("Choose between these numbers"))
        if (guess == real):
            score += 1
            print ("Congrats!")
        else:
            print ("Wrong, better luck next time!")
    again == int(input("Do you wanna go again?"))
    print(again)

在將值分配給再次調用的變量時,您可以使用比較運算符:

again == int(input("Do you wanna go again?"))

您必須刪除一個等號:

again = int(input("Do you wanna go again?"))
again == int(input("Do you wanna go again?"))

這不會做您想的,因為==表示它正在檢查此語句是否為真。 您想要一個=。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM