简体   繁体   English

检查raw_input的字典

[英]Check raw_input for dictionary

I looked on stackoverflow to solve my problem but from all explanations about loops and checks I don't understand why my code isn't working. 我查看了stackoverflow来解决我的问题,但是从关于循环和检查的所有解释中,我不理解为什么我的代码无法正常工作。 So I want to build a dictionary (totally new to Python btw) and I read that I can also check if the input is in the dicitonary module but that is actually not what I want to do here. 所以我想建立一个字典(对Python btw来说是全新的),我读到我也可以检查输入是否在dicitonary模块中,但这实际上不是我要在这里做的。 I just want to see if the raw_input contains at least one number in the string (not if the string only contains numbers) and if the length of the input string is at least 2. If the input passes those checks it should move on (the rest of this dictionary will come later. For now I only want to understand what I did wrong with my check) Here's my code, help would be very much appreciated! 我只想看看raw_input是否在字符串中至少包含一个数字(如果字符串仅包含数字则不是),并且输入字符串的长度至少为2。如果输入通过了这些检查,它应该继续前进(这本词典的其余部分将在以后发布。现在,我只想了解我在检查中做错了什么)这是我的代码,非常感谢您的帮助!

def check():
    if any(char.isdigit() for char in original):
        print ("Please avoid entering numbers. Try a word!")
        enter_word()
    elif len(original)<1:
        print ("Oops, you didn't enter anything. Try again!")
        enter_word()

    else:
        print ("Alright, trying to translate:")
        print ("%s") %(original)

def enter_word():
    original = raw_input("Enter a word:").lower()
    check()

enter_word()

Edit: Works now perfectly with the following code: 编辑:现在可以完美地使用以下代码:

def check(original):
    if any(char.isdigit() for char in original):
        print "Please avoid entering numbers. Try a word!"
        enter_word()
    elif len(original) < 1:
        print "Oops, you didn't enter anything. Try again!"
        enter_word()
    else:
        print "Alright, trying to translate:"
        print "{}".format(original)

def enter_word():
    original = raw_input("Enter a word:").lower()
    check(original)

enter_word()

You need to pass the input original to your check() function: 您需要将original输入传递给您的check()函数:

def check(original):
    if any(char.isdigit() for char in original):
        print("Please avoid entering numbers. Try a word!")
        enter_word()
    elif len(original) < 1:
        print("Oops, you didn't enter anything. Try again!")
        enter_word()
    else:
        print("Alright, trying to translate:")
        print("{}".format(original))

def enter_word():
    original = input("Enter a word:").lower()
    check(original)

enter_word()

In addition to this, you had some syntax errors in your code. 除此之外,您的代码中还有一些语法错误。 Since you used print() instead of print I assume you are using Python3. 由于您使用了print()而不是print所以我假设您正在使用Python3。 However, to read user input you used raw_input() which was the way to do in Python2 and became input() in Python3. 但是,要读取用户输入,您使用了raw_input() ,这是在Python2中完成的方式,在Python3中成为了input() I fixed this. 我修好了 Another thing I fixed was the string formatting of the print() statement in the else branch. 我修复的另一件事是else分支中print()语句的字符串格式。 You might take a look at the string format mini-language . 您可能会看一下mini-language字符串格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM