返回到while循环的起点



我正在制作一个简单的石头剪刀布游戏。每赢一次,你就获得一分。电脑也是一样。玩家与电脑之间,先得3分者为胜。一切都很好,但我想添加一个额外的功能,这是一个选项,再次播放。

import random
user_name_input = input("What is your name?: ")
print(f"Oh hello {user_name_input}!")
options = ["rock", "paper", "scissors"]

while True:
user_points = 0
comp_points = 0
print("ROCK--PAPER--SCISSORS!")
user_choice = input("SHOOT!: ").lower()
comp_choice = random.choice(options)
if user_choice not in options:
print("Please choose either rock, paper or scissors")
print("n")
continue
# CONDITIONS
if user_choice == "rock" and comp_choice == "scissors":
print("WIN")
print(f"Computer's Choice: {comp_choice}")
user_points += 1
print(f"Your Points: {user_points}")
print(f"Computer's Points: {comp_points}")
print("n")
elif user_choice == "paper" and comp_choice == "rock":
print("WIN")
print(f"Computer's Choice: {comp_choice}")
user_points += 1
print(f"Your Points: {user_points}")
print(f"Computer's Points: {comp_points}")
print("n")
elif user_choice == "scissors" and comp_choice == "paper":
print("WIN")
print(f"Computer's Choice: {comp_choice}")
user_points += 1
print(f"Your Points: {user_points}")
print(f"Computer's Points: {comp_points}")
print("n")
elif user_choice == comp_choice:
print(f"Computer's Choice: {comp_choice}")
print("Same picks, play again")
print(f"Your Points: {user_points}")
print(f"Computer's Points: {comp_points}")
print("n")
else:
print("LOSS")
print(f"Computer's Choice: {comp_choice}")
comp_points += 1
print(f"Your Points: {user_points}")
print(f"Computer's Points: {comp_points}")
print("n")
if user_points == 3:
print("You've won")
print(
f"You did it {user_name_input}!, you're a rock, paper, scissors MASTER!!!!")
quit()

elif comp_points == 3:
print("You've lost")
print(f"Sorry {user_name_input}, you'll get it next time!")
quit()

我在底部添加了这个条件,并且我期望使用"continue"语句,python将循环回到while循环的开始,游戏将再次运行。然而,这完全打乱了我的程序,现在当我运行它时,只玩了一次,电脑问用户是否想再玩一次。它不像原来那样循环3次。

if play_again == "yes":
user_points = 0
comp_points = 0
print("Great! Let's go!")
continue
else:
play_again == "no"
print("Ok thanks for playing")
quit()

有人知道为什么吗?谢谢你。

简短的回答:您必须将user_points和comp_points重新设置为零。循环不会重置在循环外声明的值。

如果play_again == "yes",则编译器从顶部重新执行。但是在您的循环中,您只将user_choicecomp_choice重置为新值,因此user_pointscomp_points保持不变。

让我们假设在最后一场游戏中用户获胜,这意味着user_points为3。现在如果玩家继续玩游戏,user_points的值仍然是3。现在它仍然会让你玩一轮,因为你在开始时没有检查分数。现在当你完成第一轮并检查点数时,user_points仍然是3,所以循环将结束。

要解决这个问题,你需要修改:

if play_again == "yes":
print("Great! Let's go!")
continue
else:
play_again == "no"
print("Ok thanks for playing")
quit()

if play_again == "yes":
print("Great! Let's go!")
user_points = 0
comp_points = 0
continue
else:
play_again == "no"
print("Ok thanks for playing")
quit()

您可以在函数中添加所有内容进口随机

def game():
user_name_input = input("What is your name?: ")
print(f"Oh hello {user_name_input}!")
options = ["rock", "paper", "scissors"]
user_points = 0
comp_points = 0

while True:
# All your while loop
play_again = input("Do you want to play again?: ")
if play_again == "yes":
print("Great! Let's go!")
game()  # This calls again your function

elif play_again == "no":
print("Ok thanks for playing")
game()

最新更新