簡體   English   中英

運行此代碼時,為什么循環不等待我的響應?

[英]When running this code, why does the loop not wait for my response?

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循環的范圍內,所以只調用一次。

一旦進入 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. 為了減少重復,所以你只有一個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()

暫無
暫無

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

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