循环错误(家庭作业)

2024-04-20 02:32:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题(显然)循环没有按预期工作。假设其他一切都按预期工作,我的第二个while循环(根据分级机)调用raw\u输入的次数超出了需要。你知道吗

代码玩文字游戏。你可以玩一手牌,重放一手牌,或退出。第二个循环决定你是玩手还是玩电脑。你知道吗

所有被调用的函数都正常工作,但是循环调用原始输入的次数太多了。你知道吗

抱歉,如果有很多其他问题,我是相对新的编码。你知道吗

userInput = ''
playInput = ''
hasPlayed = False

# while still playing, e exits
while userInput != 'e':

    userInput = raw_input("Enter n to deal a new hand, r to replay the last hand, or e to end game: ").lower()

    if userInput not in 'nre':

        print("Invalid command.")

    elif userInput == 'r' and hasPlayed == False:

        print("You have not played a hand yet. Please play a new hand first!")
        print

    else:

        while True:

           # 
            print
            playInput = raw_input("Enter u to have yourself play, c to have the computer play: ").lower()

            if playInput == 'u':

                print
                hand = dealHand(HAND_SIZE)
                playHand(hand, wordList, HAND_SIZE)
                hasPlayed = True

            elif playInput == 'c':

                print
                hand = dealHand(HAND_SIZE)
                compPlayHand(hand, wordList, HAND_SIZE)
                hasPlayed = True

            else:

                print("Invalid command.")
    print

Tags: tofalsetrueplaysizerawhave次数
1条回答
网友
1楼 · 发布于 2024-04-20 02:32:13

你的循环运行得非常好;它永远在循环,就像你告诉它的那样:

while True:

现在缺少的是一种退出循环的方法。不同条件下的任一试验:

playing = True
while playing:
    # game
    #
    # stop playing with
    playing = False

或者使用break关键字显式中断循环:

while True:
    # game
    #
    # stop playing with
    break

相关问题 更多 >