Home > OS >  How to create a program that continually accepts input from the user and evaluate all the input
How to create a program that continually accepts input from the user and evaluate all the input

Time:10-08

How can I create a program that continually accepts input from the user.

The input shall be in this format operatornumber like 2.

If the input is not in the format, your program will display:

Invalid input, try again. This will display until a valid input is achieved.

If the input is -0. The program will stop and display the output:

The result of 1-3/5*3 is -1.2

In the example, the inputs of the user are 1, -3, /5, and *3.

CodePudding user response:

num = 0

while((a := input()) != "-0"):
    if (len(a) >= 2) and (a[0] in "/*- "):
        num = eval(f"{num}{a}")
    else:
        print("Invalid input, try again.")
        
print(num)
  • Related