如果条件是 "None" 循环从我想要的其他地方开始
If condition is "None" the loop starts somewhere else than I want
我尝试编写我的第一个基于文本的角色扮演游戏。
第一步是输入您的角色名称,如果您对名称没问题,则键入“y”;如果您想更改名称,则键入“n”。
因此,我希望程序再次要求您输入“y”或“n”,例如,如果您用粗指输入“g”。并且只有在您键入“n”时才允许您重新输入您的姓名
如果您输入“g”,程序将让您直接重新输入您的姓名,而不是这些。
我已经尝试围绕 _yesorno 函数执行“while True or False”循环。
代码如下:
main.py
from Classes.character import character
from functions.yesorno import _yesorno
#character
char = character()
while True:
print("please enter a name for your character")
char.set_name(input())
print("Your name is: " + char.name + ". Are you happy with your choice? Type 'y' for yes, 'n' for no.")
if _yesorno(input()):
break
else:
continue
_yesorno.py
def _yesorno(input:str)->bool:
if input == "y":
return True
elif input == "n":
return False
else:
print("please use y for yes and n for no")
return None
因为我是新手,如果你能解释你的新手友好的答案,而不仅仅是“你的逻辑是错误的”,我会很高兴:D
提前致谢!
if None
等于 if False
。 Python 具有类型的动态类型。
要检查错误输入,您可以执行以下操作:
def _yesorno(input_:str)->bool:
while input_ not in ['y', 'n']:
print("please use y for yes and n for no")
input_ = input()
return input_ == 'y'
该代码直接检查输入而不是它自己。 input_ not in ['y', 'n']
那部分检查你的 input_
是否是数组元素之一。
用户输入 'y' 或 'n' 函数后 return 正确的结果。
'g' 的输入让用户重新输入他们的名字的原因是因为 'None' 被视为 False 因此循环将继续......你可以尝试一个简单的 if 语句一个 while 循环并忽略你的 _yesorno 函数
while(True):
print("please enter a name for your character")
charName = input()
while(True):
print("Your name is: " + charName + ". Are you happy with your choice? Type 'y' for yes, 'n' for no.")
yesorno = input()
if(yesorno == 'y' or yesorno == 'n'):
break
if(yesorno == 'y'):
break
char.set_name(charName)
print("and your name is {0}".format(char.name))
我会用一对递归函数来解决这个问题。一个 运行 直到用户给出有效响应,另一个直到正确设置名称。
请注意,在许多情况下,递归函数可以重写为循环,并且由于 python 缺乏尾调用优化,有些人会不喜欢使用递归。不过我觉得这样做通常没问题。
def get_input_restricted(prompt, allowed_responses):
choice = input(prompt)
if choice in allowed_responses:
return choice
print(f"\"{choice}\" must be one of {allowed_responses}")
return get_input_restricted(prompt, allowed_responses)
def set_character_name():
prospective_name = input("Enter a name for your character: ")
print(f"Your name will be: {prospective_name}.")
confirmation = get_input_restricted("Are you happy with your choice (y|n)? ", ["y", "n"])
if "y" == confirmation:
return prospective_name
return set_character_name()
character_name = set_character_name()
print(f"I am your character. Call me {character_name}.")
我尝试编写我的第一个基于文本的角色扮演游戏。
第一步是输入您的角色名称,如果您对名称没问题,则键入“y”;如果您想更改名称,则键入“n”。
因此,我希望程序再次要求您输入“y”或“n”,例如,如果您用粗指输入“g”。并且只有在您键入“n”时才允许您重新输入您的姓名 如果您输入“g”,程序将让您直接重新输入您的姓名,而不是这些。
我已经尝试围绕 _yesorno 函数执行“while True or False”循环。
代码如下: main.py
from Classes.character import character
from functions.yesorno import _yesorno
#character
char = character()
while True:
print("please enter a name for your character")
char.set_name(input())
print("Your name is: " + char.name + ". Are you happy with your choice? Type 'y' for yes, 'n' for no.")
if _yesorno(input()):
break
else:
continue
_yesorno.py
def _yesorno(input:str)->bool:
if input == "y":
return True
elif input == "n":
return False
else:
print("please use y for yes and n for no")
return None
因为我是新手,如果你能解释你的新手友好的答案,而不仅仅是“你的逻辑是错误的”,我会很高兴:D
提前致谢!
if None
等于 if False
。 Python 具有类型的动态类型。
要检查错误输入,您可以执行以下操作:
def _yesorno(input_:str)->bool:
while input_ not in ['y', 'n']:
print("please use y for yes and n for no")
input_ = input()
return input_ == 'y'
该代码直接检查输入而不是它自己。 input_ not in ['y', 'n']
那部分检查你的 input_
是否是数组元素之一。
用户输入 'y' 或 'n' 函数后 return 正确的结果。
'g' 的输入让用户重新输入他们的名字的原因是因为 'None' 被视为 False 因此循环将继续......你可以尝试一个简单的 if 语句一个 while 循环并忽略你的 _yesorno 函数
while(True):
print("please enter a name for your character")
charName = input()
while(True):
print("Your name is: " + charName + ". Are you happy with your choice? Type 'y' for yes, 'n' for no.")
yesorno = input()
if(yesorno == 'y' or yesorno == 'n'):
break
if(yesorno == 'y'):
break
char.set_name(charName)
print("and your name is {0}".format(char.name))
我会用一对递归函数来解决这个问题。一个 运行 直到用户给出有效响应,另一个直到正确设置名称。
请注意,在许多情况下,递归函数可以重写为循环,并且由于 python 缺乏尾调用优化,有些人会不喜欢使用递归。不过我觉得这样做通常没问题。
def get_input_restricted(prompt, allowed_responses):
choice = input(prompt)
if choice in allowed_responses:
return choice
print(f"\"{choice}\" must be one of {allowed_responses}")
return get_input_restricted(prompt, allowed_responses)
def set_character_name():
prospective_name = input("Enter a name for your character: ")
print(f"Your name will be: {prospective_name}.")
confirmation = get_input_restricted("Are you happy with your choice (y|n)? ", ["y", "n"])
if "y" == confirmation:
return prospective_name
return set_character_name()
character_name = set_character_name()
print(f"I am your character. Call me {character_name}.")