Python Cows and Bulls游戏 - 无限循环

问题描述 投票:0回答:1

这是我在StackOverflow上的第一篇文章,所以如果我做错了什么,请告诉我。这款名为Cows and Bulls的游戏类似于一款名为Mastermind https://en.wikipedia.org/wiki/Bulls_and_Cows的游戏。基本上,你必须猜测四位数代码而不给出任何重复的数字。我遇到的错误是,当我到达名为“检查重复数字的代码”的部分时,如果随机代码中有重复项,它将在while语句中进入无限循环。有人能帮我这个吗?

import random
def main():
    bulls=0 #Correct Number in Correct Position
    cows=0 #Correct Number in Wrong Position
    CODE=random.randint(1000,9999)
    CODE=str(CODE)
    codeFirst=int(CODE[0])
    codeSecond=int(CODE[1])
    codeThird=int(CODE[2])
    codeFourth=int(CODE[3])
    GUESS=int(input("Guess:"))
    repeatedCode=True

    #Check for exactly four digits
    if GUESS <= 1000 or GUESS >= 9999:
        print("Invalid input - Guess must be four digits!")
        return
    elif GUESS >= 1000 and GUESS <= 9999:
    #If all previous conditions cleared set guess values
    GUESS=str(GUESS)
    guessFirst=int(GUESS[0])
    guessSecond=int(GUESS[1])
    guessThird=int(GUESS[2])
    guessFourth=int(GUESS[3])

    #Check code for repeated numbers
    while repeatedCode == True:
        if codeFirst == codeSecond or codeFirst == codeThird or codeFirst == 
 codeFourth:
             CODE=random.randint(1000,9999)
             print("Test1")
             repeatedCode == True
        elif codeSecond == codeThird or codeSecond == codeFourth:
            CODE=random.randint(1000,9999)
            print("Test2")
            repeatedCode == True
        elif codeThird == codeFourth:
            CODE=random.randint(1000,9999)
            print("Test3")
            repeatedCode == True
        else:
            repeatedCode==False
            break

    print('Test passes repeated code')

    #Check for repeated numbers
    if guessFirst == guessSecond or guessFirst == guessThird or guessFirst 
== guessFourth:
        print("Invalid Input - guess cannot contain duplicates")
        return
    elif guessSecond == guessThird or guessSecond == guessFourth:
        print("Invalid Input - guess cannot contain duplicates")
        return
    elif guessThird == guessFourth:
        print("Invalid Input - guess cannot contain duplicates")
        return

    #Bulls and Cows
    if guessFirst == codeFirst:
        bulls=bulls + 1
    elif guessFirst == codeSecond or guessFirst == codeThird or guessFirst 
== codeFourth:
        cows=cows + 1

    if guessSecond == codeSecond:
        bulls + 1
    elif guessSecond == codeFirst or guessSecond == codeThird or guessSecond 
== codeFourth:
        cows=cows + 1

    if guessThird == codeThird:
        bulls=bulls + 1
    elif guessThird == codeFirst or guessThird == codeSecond or guessThird 
== codeFourth:
        cows=cows + 1

    if guessFourth == codeFourth:
        bulls=bulls + 1
    elif guessFourth == codeFirst or guessFourth == codeSecond or guessFourth == codeThird:
        cows=cows + 1

    #Final Text Output   



print("Guess:",guessFirst, guessSecond,guessThird,guessFourth,"Code:",codeFirst,codeSecond,\
      codeThird,codeFourth,"Results:", bulls,"-",cows)
    return
main()
python random infinite-loop
1个回答
1
投票

它将进入无限循环,因为当存在重复数字时,您只是更新Code的值而不是CodeFirst,CodeSecond,CodeThird和CodeFourth,它们在条件中使用。它应该是这样的

    while repeatedCode == True:
        if codeFirst == codeSecond or codeFirst == codeThird or codeFirst == codeFourth:
             CODE=random.randint(1000,9999)
             CODE=str(CODE)
             codeFirst=int(CODE[0])
             codeSecond=int(CODE[1])
             codeThird=int(CODE[2])
             codeFourth=int(CODE[3])
             print("Test1")
        elif codeSecond == codeThird or codeSecond == codeFourth:
             CODE=random.randint(1000,9999)
             CODE=str(CODE)
             codeFirst=int(CODE[0])
             codeSecond=int(CODE[1])
             codeThird=int(CODE[2])
             codeFourth=int(CODE[3])
             print("Test2")
        elif codeThird == codeFourth:
             CODE=random.randint(1000,9999)
             CODE=str(CODE)
             codeFirst=int(CODE[0])
             codeSecond=int(CODE[1])
             codeThird=int(CODE[2])
             codeFourth=int(CODE[3])
             print("Test3")
        else:
            repeatedCode=False

您还可以使用一个集来测试代码是否重复数字而不是创建四个变量。我会让你弄明白你如何使用套装。

© www.soinside.com 2019 - 2024. All rights reserved.