Python 3.x Python:字符串类似于所有内容

Python 3.x Python:字符串类似于所有内容,python-3.x,string-comparison,Python 3.x,String Comparison,我需要使用字符串(或int,bool等),这将是一样的一切。所以这个代码: user_input = input() if user_input in *magic_string_same_as_everything*: return True 应该返回True everythine,无论用户在控制台中键入什么。 谢谢你的帮助 编辑: 我明白了,我问得很糟 我正在尝试在此for循环中获得3个用户输入: user_input = ["", "", ""] # Name, class, d

我需要使用字符串(或int,bool等),这将是一样的一切。所以这个代码:

user_input = input()
if user_input in *magic_string_same_as_everything*:
    return True
应该返回True everythine,无论用户在控制台中键入什么。 谢谢你的帮助

编辑:
我明白了,我问得很糟

我正在尝试在此for循环中获得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语句而
返回True
,或者(如果您真的想使用if语句)键入
if(True)
,它将始终为True。

初始响应 这一行应该行得通

如果用户输入任何内容,它将作为输入并打印“
True
”,但如果用户只点击“回车”而不键入任何内容,它将返回“
无输入”

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语句中,函数检查每个元素,如果没有发现输入/不正确的输入(拼写错误等),它会要求用户纠正它们并最终返回更新的列表

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
测试输出 具有正确的条目 欢迎来到地下城和蟒蛇

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']
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']
输入不正确 欢迎来到地下城和蟒蛇

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']
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']

您希望非空字符串为True吗? 只需使用用户输入作为bool

user_input = input()
if user_input:
     return True
在您的问题中,
Name
是特例,只需像这样检查它,对于其余的输入,您可以使用
range(1,3)

或者切换到使用


为什么不删除
if
语句,并在获得输入后仅使用
return True
?不确定我是否理解正确,您希望接受用户输入并返回True,而不考虑任何其他条件?您可能对此感兴趣:return仅适用于函数。如果语句没有任何要返回的内容。在这种情况下应该使用print,或者将它赋给一个变量。你是对的,我认为他实际上想要返回True,并且在函数中有这个代码