0

Looking to validate that the user is indeed inputting an int and not a string for the values and also looking to make sure they are using one of the operation symbols for the opeeration and not anything else. If they do enter otherwise, want to be able to display an error and loop to the beginning of the question and start over.

def redo():
    retry=input("would you like to start over? \n enter 'y' for yes or 'n' for no")
    if retry=='y':
        solve()
    elif retry=='n':
        print("thanks for playing!")
def solve():
        a=int(input("Enter the first value: "))
      
        b=int(input("enter the second value: "))
        op=input('Enter the operation, \n you may enter either a +, -, *, %, or a ^: ')
        if op=='+':
             print(f'the value of {a} plus {b} is : {add(a,b)}')

        elif op=='-':
             print(f'the value of {a} minus {b} is : {sub(a,b)}')

        elif op == '*':
             print(f'the value of {a} multiplied to {b} is : {mult(a,b)}')

        elif op=='/':
                print(f'the value of {a} to the divided by {b} is : {div(a,b)}')

        elif op =='^':
                print(f'the value of {a} to the power of {b} is : {exp(a,b)}')
        else:   
             print('your operation could not be understood, please choose from the list ')
                
        redo()

def add(a,b):
    return int(a) +int(b)
def sub(a,b):
    return int(a) - int(b)
def mult(a,b):
    return int(a) *int(b)
def div(a,b):
    return int(a)/int(b)
def exp(a,b):
    return int(a) ** int(b)

solve()
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • You can use ```try-exception``` – ksohan Jan 05 '22 at 04:33
  • Welcome to Stack Overflow! Check out the [tour]. I closed your question because there's an existing question that covers exactly this topic. If you have any further questions after reading it, LMK by @-tagging me in a comment. And please in the future, try your own research before asking a new question. See [ask] for more tips. – wjandrea Jan 05 '22 at 04:56

0 Answers0