Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conditional handling in Python

The script I coded has 3 outcomes, it either solves the factorial of a number, or it gives a message that the value input is not positive, or it will give a message that it is not a number, but the not a number outcome does not work.

user_number_input = input('Insert number: ')#This asks the user to input a number.
uni_int = int(user_number_input)


if uni_int < 0: #This checks if the number is negative, it will print an error message.
        print('Not a positive number!')
elif uni_int > 0:
    
    factorial_value = 1 #This is to ensure the calculation starts at 1 and not 0.
    while uni_int > 1: #Multiply number_inserted until 1 (n! = n x (n-1) x (n-2)...x 2 x 1)
         factorial_value *= uni_int #This is where the multiplication takes place, eg: 5 * 5 - 1 * 5 - 2...
         uni_int -= 1 #This ensures we move closer to 1, like within the formula (n! = n x (n-1) x (n-2)...x 2 x 1)<--- 1 is the end.
    print(factorial_value)
elif user_number_input != int and user_number_input == ' ':
   print('Not a number!')

I tried to rearrange the code so that the first conditional would check if the value is not a numbe, but then the last conditional does not work, which would be to check if the value is a positive number.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You will have ValueError when have input like "s12" in your second line. For avoid this problem use try – except firstly try if your input can be converted int or not. If not your output will be "Not a number", if can be convert to int then will follow other statements. I hope you can understand and it can help you. If you have any other question feel free to ask.

user_number_input = input('Insert number: ')#This asks the user to input a number.

try:
    uni_int = int(user_number_input)

    if uni_int < 0: #This checks if the number is negative, it will print an error message.
            print('Not a positive number!')
    elif uni_int > 0:
        
        factorial_value = 1 #This is to ensure the calculation starts at 1 and not 0.
        while uni_int > 1: #Multiply number_inserted until 1 (n! = n x (n-1) x (n-2)...x 2 x 1)
            factorial_value *= uni_int #This is where the multiplication takes place, eg: 5 * 5 - 1 * 5 - 2...
            uni_int -= 1 #This ensures we move closer to 1, like within the formula (n! = n x (n-1) x (n-2)...x 2 x 1)<--- 1 is the end.
        print(factorial_value)

except ValueError: 
   print('Not a number!')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading