【发布时间】:2021-06-30 21:43:14
【问题描述】:
#1
def hit_stay():
hit_stay = ''
while True:
hit_stay = input('Would you like to Hit or Stay?')
if hit_stay in ['hit','Hit','stay','Stay']:
hit_stay = hit_stay.capitalize()
return hit_stay
else:
print('Please enter a valid word)
#2
When I use the code and call the function it works the first time
hit_stay = hit_stay()
#3
Then I print the choice
print(hit_stay)
但是,如果我再次尝试拨打 2 号以获得不同的选择,它会显示“str”不可调用 我试图让用户做出选择,以便稍后在我的代码中使用该选择。 我发现如果再次运行 1 号然后 2 号一切正常,但我需要能够 稍后调用此函数并获得新的答案。
【问题讨论】:
-
在你将其他东西[在这种特殊情况下 - 返回值]分配给具有相同名称的变量之后,你不能再次调用该函数,例如我有一个名为 XYZ 的函数,我做了XYZ = XYZ();现在 XYZ 将不再包含函数,而是从 XYZ 返回的值,您必须将行重命名为
hit_stay = hit_stay()到其他名称,而不是hit_stay。 -
@Jonathan1609 您应该将此作为答案发布。现有答案实际上并未回答问题。
标签: python-3.x string error-handling