运行此代码时,为什么循环不等待我的响应?

时间:2019-12-26 01:47:44

标签: python while-loop infinite

import random

def rock_paper_scissors(choice):
    computer_choice = random.choice(['rock', 'paper', 'scissors'])
    while True:
        if choice == computer_choice:
            print("Tie! Play again.")
            choice
        elif choice.lower() == 'quit':
            break

        elif choice.lower() == 'rock' and computer_choice.lower() == 'paper':
            print("TRY AGAIN!", computer_choice.lower(), "beats", choice.lower())
            choice
        elif choice.lower() == 'rock' and computer_choice.lower() == 'scissors':
            print("YOU WON!", choice.lower(), "beats", computer_choice.lower())
            choice
        elif choice.lower() == 'paper' and computer_choice.lower() == 'scissors':
            print("TRY AGAIN!", computer_choice.lower(), "beats", choice.lower())
            choice
        elif choice.lower() == 'paper' and computer_choice.lower() == 'rock':
            print("YOU WON!",  choice.lower(), "beats", computer_choice.lower())
            choice
        elif choice.lower() == 'scissors' and computer_choice.lower() == "rock":
            print("TRY AGAIN!", computer_choice.lower(), "beats", choice.lower())
            choice
        elif choice.lower() == 'scissors' and computer_choice.lower() == "paper":
            print("YOU WON!", choice.lower(), 'beats', computer_choice.lower())
            choice
        else:
            print("Sorry, invalid.")
            choice

print(rock_paper_scissors(input("Rock paper or scissors (enter 'q' to quit): ")))

运行此代码时,将在运行框中一遍又一遍地重复elif打印内容。我不知道我应该修复while语句还是在elif语句中添加其他代码。输入工作正常,但我不知道要添加到while循环中的内容。 (我是Python初学者)

2 个答案:

答案 0 :(得分:2)

输入语句不在while循环的范围内,因此仅被调用一次。

在while循环中,没有任何更改更改选择变量,因此一遍又一遍地触发相同的打印语句。

将输入语句与计算机选择初始化一起移动到while循环中(以便计算机可以每次选择一个新选项)都可以解决您的问题。

import random
def rock_paper_scissors():  
    while True:
        computer_choice = random.choice(['rock', 'paper', 'scissors'])
        choice = input("Rock paper or scissors (enter 'q' to quit): ")
        if choice == computer_choice:
            print("Tie! Play again.")
        elif choice.lower() == 'quit':
            break
        elif choice.lower() == 'rock' and computer_choice.lower() == 'paper':
            print("TRY AGAIN!", computer_choice.lower(), "beats", choice.lower())
        elif choice.lower() == 'rock' and computer_choice.lower() == 'scissors':
            print("YOU WON!", choice.lower(), "beats", computer_choice.lower())
        elif choice.lower() == 'paper' and computer_choice.lower() == 'scissors':
            print("TRY AGAIN!", computer_choice.lower(), "beats", choice.lower())
        elif choice.lower() == 'paper' and computer_choice.lower() == 'rock':
            print("YOU WON!",  choice.lower(), "beats", computer_choice.lower())
        elif choice.lower() == 'scissors' and computer_choice.lower() == "rock":
            print("TRY AGAIN!", computer_choice.lower(), "beats", choice.lower())
        elif choice.lower() == 'scissors' and computer_choice.lower() == "paper":
            print("YOU WON!", choice.lower(), 'beats', computer_choice.lower())
        else:
            print("Sorry, invalid.")
rock_paper_scissors()

答案 1 :(得分:0)

我想我会分享一种替代的编写方式,这可能对初学者有所帮助。目标是:

  1. 为减少重复,因此您只有一个print("You Lose")而不是三个。
  2. 将获胜的逻辑分离为一个单独的函数,这使得测试逻辑部分更加容易并允许代码重用。
import random

def did_player_win(computer, player):
    # Returns None for a tie, True for a win, False for a lose

    if computer == player:
        return None

    if computer == 'paper':
        return False if player == 'rock' else True

    if computer == 'scissors':
        return False if player == 'paper' else True

    if computer == 'rock':
        return False if player == 'scissors' else True


def play():
    while True:
        player = input("Rock, paper or scissors (enter 'q' to quit): ")
        player = player.lower()
        if player not in ['rock', 'paper', 'scissors']:
            break

        computer = random.choice(['rock', 'paper', 'scissors'])

        player_won = did_player_win(computer, player)
        if player_won is True:
            print("YOU WON! {0} beats {1}".format(player, computer))    
        elif player_won is False:
            print("TRY AGAIN! {0} beats {1}".format(computer, player))
        else:
            print("Tie!  Both chose {0}".format(player))

play()