Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python输入过滤器_Python_String_Input_Keyword - Fatal编程技术网

Python输入过滤器

Python输入过滤器,python,string,input,keyword,Python,String,Input,Keyword,这是我们的后续行动。我现在想知道如何使我的输入只接受一些单词/数字。如果输入了错误的单词/数字,则会提示您重新键入。如果键入的匹配词是一个可接受的词,它将传递并运行代码的其余部分。 代码: 简单的,而: while True: try: msg = int(input("Enter: ")) except ValueError: print("Not Valid!") continue # do some validatio

这是我们的后续行动。我现在想知道如何使我的输入只接受一些单词/数字。如果输入了错误的单词/数字,则会提示您重新键入。如果键入的匹配词是一个可接受的词,它将传递并运行代码的其余部分。 代码:


简单的
,而

while True:
    try:
        msg = int(input("Enter: "))
    except ValueError:
        print("Not Valid!")
        continue
    # do some validation like if msg.endswith('hi') and stuff like that ex:

    if msg > 40 or msg < 10: # fail if greater than 40 and smaller than 10
        print("Not Valid!")
        continue
    break
为True时:
尝试:
msg=int(输入(“输入:”)
除值错误外:
打印(“无效!”)
持续
#执行一些验证,例如if msg.endswith('hi')和类似的东西,例如:
如果msg>40或msg<10:#如果大于40且小于10,则失败
打印(“无效!”)
持续
打破

提供指向该问题的链接,而不是描述其他问题。您可以单击其他问题下的“共享”按钮,以获得可粘贴到此处的链接。如果没有链接,这些信息只会帮助那些碰巧记住您的其他问题并知道如何找到它的人。对不起,我不确定如何将其应用到我的代码中,我能否在您收集输入并同时执行验证时使用它。否则,如果你只想得到正确的输入,这就是你所需要的。好的,这接近我想要的结果,但它只是拒绝的字符串,这也是我想要的,但我想要的主要是,如果数字大于10,但小于40,它将运行。如果数字不大于10或不大于40,则将失败。然后添加一些逻辑语句和验证。我来编辑一下。
while True:
    try:
        msg = int(input("Enter: "))
    except ValueError:
        print("Not Valid!")
        continue
    # do some validation like if msg.endswith('hi') and stuff like that ex:

    if msg > 40 or msg < 10: # fail if greater than 40 and smaller than 10
        print("Not Valid!")
        continue
    break