【问题标题】:When running this code, why does the loop not wait for my response?运行此代码时,为什么循环不等待我的响应?
【发布时间】:2019-12-26 01:47:44
【问题描述】:
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 程序员)

【问题讨论】:

  • while True: 根据定义会一遍又一遍地重复,除非你有办法跳出循环。我想你只是想删除while 声明。
  • 您必须请求用户输入,而不是输入choice 语句(没有效果)。提示:在所有ifs 检查后,您只能请求用户输入一次
  • @dcg 你为什么写在评论里?这正是答案......即使是stackoverflow也不愿意在评论中给出答案,并且仅将其用于添加信息。

标签: python while-loop infinite


【解决方案1】:

输入语句不在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()

【讨论】:

    【解决方案2】:

    我想我会分享另一种写这篇文章的方法,这可能对初学者有帮助。目标是:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 2019-09-24
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      相关资源
      最近更新 更多