简体   繁体   English

字符串中的数字介于最大和最小之间

[英]digits in a string fit in between max and min

So I am trying to get my string to be verified between 1000 to 9999 and when I try to run this, it comes back saying it is unverified. 所以我试图让我的字符串在1000到9999之间进行验证,当我尝试运行此字符串时,它又回来说它未经验证。 Curious on how to fix this problem so that it can actually read the 4 digits that a user input as a string. 对如何解决此问题感到好奇,以便它可以实际读取用户作为字符串输入的4位数字。 I have it as a string for a reason and it works fine for other parts of my code. 我将其作为字符串存在是有原因的,它对于我的代码的其他部分也能正常工作。 it is just this one specifically is giving me a hard time. 只是这个特别给我带来了困难。

def choiceTwo(digitCode):

    max = 9999
    min = 1000

    digitCode = [int(num) for num in digitCode]
    for x in digitCode:

        if x > max:
            return True

        if x < min:
            return False

def main():

    digitCode = str(input("Please enter a 4 digit code: "))

    if choiceTwo(digitCode) is True:
            print("Number is verified!")

    else:
            print("ERROR not a valid ")

It's probably easier to do this straight in your if statement: 直接在if语句中执行此操作可能会更容易:

if int(digitCode) < int(max) and int(digitCode) > int(min):
    print("Number is verified!")
else:
    print("ERROR not a valid ")

Edit: I made sure that both digitCode, max, and min were all integers since you mentioned that you used them as strings elsewhere in the code. 编辑:我确保digitCode,max和min均为整数,因为您提到在代码中的其他位置将它们用作字符串。

Edit 2: Also, double check your statements inside your function; 编辑2:同样,仔细检查函数中的语句; what happens if x is in between max and min (currently it only returns True if x > max). 如果x在max和min之间(当前仅在x> max时返回True),会发生什么。

Your checking if each number in the 4 digit code is between those two values. 您检查4位代码中的每个数字是否在这两个值之间。 But each number is in the range 0-9 hence this won't be true. 但是每个数字都在0-9范围内,因此这不是正确的。 Well, not between those two values persay your first if condition if x > max is simpy checking if the number is greater than 9999 and then returning True. 好吧, if x > max是simpy,请检查您的第一个if条件if x > max为simpy,然后检查数字是否大于9999 ,然后返回True。 You need to nest your conditions together by using and / or > operators to check for a number between this range. 您需要使用和/或>运算符将条件嵌套在一起,以检查此范围内的数字。

You shouldn't name your two variables min and max as they are built into python and you're overriding functionality. 您不应该将自己的两个变量minmax命名为python,因为它们是内置的,并且您将覆盖功能。 You can also combine your condition. 您也可以结合您的条件。 If you're not wanting your range of accepted values to be inclusive just take of the = in <= in the below condition to exclude 9999 and 1000 如果你不想让你的接受值的范围是包容性只是采取的=<=在下面的条件,排除99991000

You can drop the extra is True in your original if condition since just doing if (expression) will evaluate it's truth value or "truthiness". 您可以在原来的if条件中删除多余is True ,因为只要执行if (expression)将评估它的真值或“真实性”即可。

I added a while loop that will continuously prompt the user to input 4 digit codes until they are indeed of length four and valid digits to prevent errors in your other function and validate the input. 我添加了一个while循环,该循环将不断提示用户输入4位数字代码,直到它们的长度为四位有效数字为止,以防止其他功能出错并验证输入。

You actually don't even need the else: return False since returning nothing will return None and be evaluated as not being True in your if condition, but I left this to keep it close to the original. 您实际上甚至不需要else: return False因为不返回任何内容将返回None并且在您的if条件下被评估为不是 True,但是我保留了此设置以使其与原始条件保持接近。 You could declare min and max to be global (or local in your main()) function, either one, and simply use if _min <= int(digitCode) <= _max if you wanted to be succinct. 您可以将min和max声明为全局函数(或在main()的局部函数中),也可以任意一个, if _min <= int(digitCode) <= _max ,请使用if _min <= int(digitCode) <= _max

def choiceTwo(digitCode):

    _max = 9999
    _min = 1000

    if _min <= digitCode <= _max:
        return True
    else:
        return False

def main():

    digitCode = input("Please enter a 4 digit code: ")
    while not (digitCode.isdigit() and len(digitCode) == 4):
        digitCode = input("Please enter a 4 digit code: ")

    if choiceTwo(int(digitCode)):
        print("Number is verified!")

    else:
        print("ERROR not a valid ")
main()

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

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