Python 无法将“非类型”对象隐式转换为str
因此,我目前正在创建一个简单的石头剪纸游戏,我遇到了一个小障碍,我为用户选择了一个功能,我只允许他们为游戏选择三个有效选项。然而,每当他们选择不允许的东西时,我就会遇到一个奇怪的问题。这是我的职责Python 无法将“非类型”对象隐式转换为str,python,python-3.x,typeerror,Python,Python 3.x,Typeerror,因此,我目前正在创建一个简单的石头剪纸游戏,我遇到了一个小障碍,我为用户选择了一个功能,我只允许他们为游戏选择三个有效选项。然而,每当他们选择不允许的东西时,我就会遇到一个奇怪的问题。这是我的职责 def userChoice(): choice = input("Rock (R), Paper (P), Scissors (S)? ") if choice.upper() == 'R' or choice.lower() == 'rock': choice = 'Rock'
def userChoice():
choice = input("Rock (R), Paper (P), Scissors (S)? ")
if choice.upper() == 'R' or choice.lower() == 'rock':
choice = 'Rock'
return choice
elif choice.upper() == 'P' or choice.lower() == 'paper':
choice = 'Paper'
return choice
elif choice.upper() == 'S' or choice.lower() == 'scissors':
choice = 'Scissors'
return choice
else:
print("This was an invalid option, please try again.")
userChoice()
user = userChoice()
print("Your choice is: " + user)
我遇到的问题是,当用户选择了他们不应该选择的东西时,例如“L”,它会显示错误消息并重新启动函数,允许他们再次选择。但是,当他们下次选择一个正确的值时,我会得到以下错误
回溯最近一次呼叫上次:
文件python,第23行,在
TypeError:无法将“非类型”对象隐式转换为str
我知道如果你不向函数返回一个值,它会返回默认值'None',这就是我遇到的类型错误。然而,我不明白的是,为什么当我在第一个不正确的值之后运行第二次时,当我在其中放入正确的值时,它仍然采用“None”值,而不是我分配的新的正确值
有没有关于为什么会发生这种情况以及我如何修复它的帮助
谢谢
Liam递归调用该方法时,需要返回该值:
def userChoice():
choice = input("Rock (R), Paper (P), Scissors (S)? ")
if choice.upper() == 'R' or choice.lower() == 'rock':
choice = 'Rock'
return choice
elif choice.upper() == 'P' or choice.lower() == 'paper':
choice = 'Paper'
return choice
elif choice.upper() == 'S' or choice.lower() == 'scissors':
choice = 'Scissors'
return choice
else:
print("This was an invalid option, please try again.")
# return this!
return userChoice()
user = userChoice()
print("Your choice is: " + user)
如果返回失败,那么该函数的结果将永远不会返回。因此,返回的结果是None,您将看到错误
还有一些其他的注释。没有真正的理由将正确的选择存储在choice中。您还可以通过在收到值时设置一次选择的大小写,坚持使用该值,并始终使用该值来简化检查:
def userChoice():
choice = input("Rock (R), Paper (P), Scissors (S)? ").lower()
if choice == 'r' or choice == 'rock':
return 'Rock'
elif choice == 'p' or choice == 'paper':
return 'Paper'
elif choice == 's' or choice == 'scissors':
return 'Scissors'
else:
print("This was an invalid option, please try again.")
return userChoice()
我知道我回复有点晚了,但是@Jamie Counsell是正确的,因为您需要将递归调用返回给userChoice;但是,您可以通过使用regex进一步增强您的程序,这样它就不需要这样的特定条目。作为奖励,将其用于利用上限或下限的平等性检查,效率更高。下面是一个例子
from re import match
def userChoice():
choice = ""
while len(choice) < 1:
choice = input("Rock (R), Paper (P), or Scissors (S)?\n")
if match(r"(?i)^p(aper)?\Z", choice) is not None:
return "Paper"
elif match(r"(?i)^r(ock)?\Z", choice) is not None:
return "Rock"
elif match(r"(?i)^s(cissors)?\Z", choice) is not None:
return "Scissors"
else:
print(choice + " is not a valid option, please try again.")
return userChoice()
user = userChoice()
print("Your choice is: " + user)
下面是一个关于它如何工作的速成课程:
matchr,str不是None如果匹配则返回True
?i表示检查不区分大小写
^表示str的开始
str?表示str的0-1
\Z表示str的结尾。简单的解决方法是将变量转换为字符串。
我使用了strvariable来转换变量。你可以随心所欲。现在,在强制转换时,指向nothing的变量值将开始指向None。异常将消失您需要在最后返回userChoice。否则你的函数将不返回任何值。那太好了,谢谢你,它工作得很好。谢谢你的解释!