我需要使用与所有内容相同的字符串(或int,bool等)。所以这段代码:
user_input = input()
if user_input in *magic_string_same_as_everything*:
return True
应该返回True所有内容,无论用户输入什么内容到控制台。谢谢你的帮助
编辑: 我明白了,我问得非常严厉。
我想在这个循环中获得3个用户输入:
user_input = ["", "", ""] # Name, class, difficulty
allowed_input = ["", ["mage", "hunter"], ["e", "m", "h"]]
difficulty = {"e": 1, "m": 2, "h": 3}
message = ["Please enter your heroic name",
"Choose character (mage/hunter)",
"Tell me how difficult your journey should be? (e / m / h)"]
print("Welcome to Dungeons and Pythons\n" + 31 * "_")
for i in range(3):
while True:
print(message[i], end=": ")
user_input[i] = input()
if user_input[i] in allowed_input[i]:
break
选择名称没有限制。
我希望,现在我的问题是有道理的。
你可以在没有检查的情况下摆脱if语句和return True
,或者(如果你真的想使用if语句)你输入if(True)
并且它将永远是真的。
非空字符串你想要True吗?只需使用user_input作为bool。
user_input = input()
if user_input:
return True
在你的问题Name
是特殊情况,只需检查这样,其余的输入你可以使用range(1,3)
。
或者切换到使用regular expressions
allowed_input = ["\A\S+", "\A(mage|hunter)\Z", "\A[emh]\Z"]
for i in range(3):
while True:
print(message[i], end=": ")
user_input[i] = input()
if re.match(allowed_input[i], user_input[i]) :
break
这个班轮应该工作。
如果用户输入任何内容,则将其视为输入并打印'True
',但如果用户只是点击'Enter'而不输入任何内容,则返回'No input
'
print ("True" if input("Type something:") else 'No input')
要实现您想要的功能,您可以定义一个检查用户输入值的函数,如果不正确则更正它们。
import re
# for user input, a single line of code is sufficient
# Below code takes 3 inputs from user and saves them as a list. Second and third inputs are converted to lowercase to allow case insensitivity
user_input = [str(input("Welcome to Dungeons & Pythons!\n\nPlease enter username: ")), str(input("Choose character (mage/hunter): ").lower()), str(input("Choose difficulty (e/m/h):").lower())]
print (user_input) # Optional check
def input_check(user_input):
if user_input[0] != '':
print ("Your username is: ", user_input[0])
else:
user_input[0] = str(input("No username entered, please enter a valid username: "))
if re.search('mage|hunter', user_input[1]):
print ("Your character is a : ", user_input[1])
else:
user_input[1] = str(input("Incorrect character entered, please enter a valid character (mage/hunter): ").lower())
if re.search('e|m|h',user_input[2]):
print ("You have selected difficulty level {}".format('easy' if user_input[2]=='e' else 'medium' if user_input[2]=='m' else 'hard'))
else:
user_input[2] = str(input("Incorrect difficulty level selected, please choose from 'e/m/h': "))
return (user_input)
check = input_check(user_input)
print (check) # Optional check
在每个if-else语句中,函数检查每个元素,如果没有找到输入/错误输入(拼写错误等),它会要求用户更正它们并最终返回更新的列表。
正确输入[Out]:欢迎来到Dungeons&Pythons!
Please enter username: dfhj4
Choose character (mage/hunter): mage
Choose difficulty (e/m/h):h
['dfhj4', 'mage', 'h']
Your username is: dfhj4
Your character is a : mage
You have selected difficulty level hard
['dfhj4', 'mage', 'h']
输入错误[Out]:欢迎使用Dungeons&Pythons!
Please enter username:
Choose character (mage/hunter): sniper
Choose difficulty (e/m/h):d
['', 'sniper', 'd']
No username entered, please enter a valid username: fhk3
Incorrect character entered, please enter a valid character (mage/hunter): Hunter
Incorrect difficulty level selected, please choose from 'e/m/h': m
['fhk3', 'hunter', 'm']