Skip to content
Advertisement

Python limit input

I’m having some trouble, I want to limit the input to only integers, positive numbers, and also not zero. Now I have written these two functions and I don’t want to allow the user to put in any value. My code is not recognizing my if-statement. Only the integer/float statement. What should I do?

correct_value = False
while not correct_value:
    try:
        a_1 = int(input("what is the first number in the arithmetic progression?"))
        d = int(input("what is the differential?"))
        g_1 = int(input("what is the first number in the geometrical progression?"))
        q = int(input("what is the ratio?"))
        n= int(input("what is the number of terms?"))

        if a_1 <= 0:
             raise ValueError ("The value can't be negative and not zero") 
        
        correct_value = True
    except ValueError:
            print("It needs to be an integer, try again.")
            
        

Advertisement

Answer

One of the best things about programming is that for any repetitive task on the human side, you can put the burden on the computer instead.

So, if for each pice of data you are: calling the built-in input, converting its result to integer, checking if the value is positive and non zero – the best way to go is to group these functionalities in a single function and then you just call that function – your main application then delegates the part of acquiring clean numbers, and becomes much simpler:

def int_input(prompt_message):
    done = False
    while not done:
        value_str = input(prompt_message)
        try:
            value = int(value_str)
        except ValueError:
            print("Please, type a valid integer number!")
            continue
        if value < 1:
            print("Please type a strictly positive number!")
            continue
        done = True
    return value

This is a bit verbose, on purpose, so it is clear that once you create a function, you can add as many simple steps as needed to do your subtask (including writing further sub-functions to be called).

Once you have that function, your program can be rewritten as:

print("To calculate the sum of the arithmetic and geometric progression, you need to know the first element in the sequence of numbers, the differential, ratio and number of terms.")

a_1 = int_input("what is the first number in the arithmetic progression?")
d = int_input("what is the differential?")
g_1 = int_input("what is the first number in the geometrical progression?")
q = int_input("what is the ratio?")
n= int_input("what is the number of terms?")

# proceed to calculations:
...

And each variable is guaranteed to contain a proper positive integer, with the user being prompted until they type a suitable value, for each of them, at no cost of extra typing for each value.

As for your code: it looks all right, but for the fact that it only checks the value on a_1 for being strictly positive – so youp´d have to modify the if clause to check all the variables. (or add one if clause for each variable) – and still: it would collect all input first, and then check for negative numbers or zero.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement