我是新来的编码。我想试着写一个简单的石头剪刀布游戏。但我不知道怎么结束比赛。你知道吗
在这个程序的末尾,如果用户输入错误,我想再次转到end变量。我试着用注释行,但它不工作。你知道吗
player1 = input("What is player 1's name ? ")
player2 = input("What is player 2's name ? ")
player1 = player1.title()
player2 = player2.title()
while True:
print(player1 + " What do you choose ? rock / paper / scissors : ")
a = input()
print(player2 + " What do you choose ? rock / paper / scissors : ")
b = input()
if a == "rock" and b == "scissors" :
print(player1, "won !!!")
elif a == "scissors" and b == "rock":
print(player2, "won !!!")
elif a == "paper" and b == "rock":
print(player1, "won !!!")
elif a == "rock" and b == "paper":
print(player2, "won !!!")
elif a == "scissors" and b == "paper":
print(player1, "won !!!")
elif a == "paper" and b == "scissors":
print(player2, "won !!!")
elif a == b:
print("Its a tie :-(")
elif a or b != "rock" or "paper" or "scissors":
print("Wrong input, Try again")
end = input("Do you want to play again ? yes/no ") == "yes"
if input == "yes":
continue
else:
print('''
GAME OVER''')
break
# elif input != "yes" or "no":
# print("Wrong input, Try again. yes or no ?")
我希望它结束游戏,如果输入是“否”和重新启动游戏,如果输入是“是”如果输入不正确,我希望提示再次出现。你知道吗
您可以使用列表简化代码,然后简化if测试。您可以检查选项的顺序,并在此基础上做出决定。您还可以使测试成为标准,以最小化if语句的数量。这是我改进代码的建议。我希望这有帮助:
只需检查end的值
因为,通过将input()与“yes”进行比较,您已经将end的值设置为布尔值,它将说明用户是否想要结束游戏? 另外,您没有初始化输入变量,最后一个elif条件将始终为true,如注释中所述。你知道吗
您的代码有一些需要解决的问题,以及一些可以简化的地方。我对你的程序做了一些修改,并添加了一些注释来解释这些修改。你知道吗
这些更改还通过使用另一个while循环来检查重新启动游戏的输入是否有效
*请注意,我尚未测试这些更改,可能需要进行一些编辑
相关问题 更多 >
编程相关推荐