当运行这段代码时,为什么循环不等待我的响应?
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()
我想我会分享另一种写这篇文章的方法,它可能对初学者有帮助。目标是:
- 为了减少重复,所以你只有一个
print("You Lose")
而不是三个。
- 将获胜逻辑分离到一个单独的函数中,这样更容易测试逻辑部分并允许代码重用。
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()
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()
我想我会分享另一种写这篇文章的方法,它可能对初学者有帮助。目标是:
- 为了减少重复,所以你只有一个
print("You Lose")
而不是三个。 - 将获胜逻辑分离到一个单独的函数中,这样更容易测试逻辑部分并允许代码重用。
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()