gpt4 book ai didi

Python - 调用自身的方法

转载 作者:行者123 更新时间:2023-12-01 03:08:30 26 4
gpt4 key购买 nike

我正在尝试学习Python。我只是做了一个简单的石头剪刀布游戏来练习。我遇到一个小问题。

当每个玩家选择相同的项目时,游戏以平局结束。

enter image description here

当玩家犯错并必须重新选择时,变量为空。请注意,玩家 1 说“无”。

enter image description here

就是这个方法。问题出现在else分支中。

def play1():
player1_choice = input("Player 1 - Go: ")
if (check(player1_choice)):
return player1_choice
else:
print(error_msg)
play1() # Something is wrong here.

我做错了什么?我该如何修复它?谢谢

最佳答案

如非必要,请勿使用递归。试试这个:

def play1():
while True:
player1_choice = input("Player 1 - Go: ")
if (check(player1_choice)):
return player1_choice
else:
print(error_msg)

关于Python - 调用自身的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43099120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com