Home > Blockchain >  Def function doesn't return any output even if the correct input is supplied
Def function doesn't return any output even if the correct input is supplied

Time:10-09

I'm new in programming, using Python 3.9. So, I'm doing a simple math program from what I understand on reading and tutorials.

here's the code:

print('\n Simple Math ')


def num():

    if choice == 1:
        sum = first_num   second_num
        print('Sum is: ', sum)
    elif choice == 2:
        diff = first_num - second_num
        print('Difference is: ', diff)
    elif choice == 3:
        mul = first_num * second_num
        print('Product is: ', mul)
    elif choice == 4:
        div = first_num / second_num
        print('Quotient is: ', div)


while True:
    print('\n 1. Add: \n 2. Subtract: \n 3. Multiply: \n 4. Divide: \n')
    choice = int(input('Select an operator: '))
    if choice > 4 or choice < 1:
        choice = int(input('Input only valid selection from above! \nSelect again: '))
        first_num = float(input('Enter first number: '))
        second_num = float(input('Enter second number: '))
        num()
    break

My problem above is, when I added that "Input only valid selection from above!" portion if the user inputs beyond the number of the selection, it doesn't do anything when I try to input from 1 to 4. But if I input 5, it works good.

I already tried to put the first_num and second_num outside the while loop or above the def, tried to put num() before the while, still gives me an error. I'm doubting the portion "if choice > 4 or choice < 1:" that makes my code returns nothing when entering the right selections. Tho I'm kinda stuck and don't know what to do next.

Appreciate any help... thanks

Pardon me, I'm really an absolute beginner...

  • Related