1

I spent 3+ hours, trying to make it work, but I failed so far. I couldn't make a while loop run for the operators. I added a while loop for the operator, but then it only returns to the second while loop which asks me to input the first and second number again while I already inputted the correct first and second number. I hope my question makes sense. Thanks very much!

# a better way
#stackOverFlow Reference Page:
#     https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response

print("My first p calculator")
print("+ for Addition")
print("- for subtraction")
print("* for Multiplication")
print("/ for Division")
print("% for remainder")
    
while True:
    result = 0
    while True:
        try:
            first_num = float(input("Enter first number:"))
        except ValueError:
            print("Sorry, enter a number:")
            continue
        else:
            break
    while True:
        try:
            second_number = float(input("Enter 2nd number: "))
        except ValueError:
            print("Sorry, enter a number: ")
            continue
        else:
            break
   
    
 # How to create a while loop for the below operator to
# ask to input the correct operator, if I keep inputting wrong operators.
# Thanks Very Much!

    operation = input("Enter Operator: ")
        
    if operation == "+":
        result = first_num + second_number
                   
    elif operation == "-":
        result = first_num - second_number
                     
    elif operation == "*":
        result = first_num * second_number
                    
    elif operation == "/":
        result = first_num / second_number
    elif operation == "%":
        result = first_num % second_number

# This will RUN, but when operator is inputted WRONG, it will go back
# to ask me to enter first number and second number.(the 2nd While Loop I guess).
# I want it to ask for the input of Operator Again and Again UnTil the input is the correct operator.
# NOT go back to ask first number and second number.
# I tried to create the third while loop for the operators
# but I couldn't get it to run. only when back to 2nd while Loop.
#I thought it was indentation error, but I tried, still couldn't get it to work.
        
    
    
        
        
        
    print(result)

                  

4 Answers4

3

I would use another while loop as follows:

while True
    operation = input("Enter Operator: ")
        
    if operation == "+":
        result = first_num + second_number
    elif operation == "-":
        result = first_num - second_number
                     
    elif operation == "*":
        result = first_num * second_number
                    
    elif operation == "/":
        result = first_num / second_number
    elif operation == "%":
        result = first_num % second_number
    else:
        continue
    break

You can also make the code simpler by using the operator module.

import operator
ops = {
    "+": operator.add,
    "-": operator.sub,
    "*": operator.mul,
    "/": operator.truediv
}

while True
    operation = input("Enter Operator: ")
    try:
        result = ops[operation](first_num, second_number)
    except KeyError:
        continue
    else:
        break
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
0

You are missing a break command to end the parent while:

print("My first p calculator")
print("+ for Addition")
print("- for subtraction")
print("* for Multiplication")
print("/ for Division")
print("% for remainder")
    
while True:
    result = 0
    while True:
        try:
            first_num = float(input("Enter first number:"))
        except ValueError:
            print("Sorry, enter a number:")
            continue
        else:
            break
    while True:
        try:
            second_number = float(input("Enter 2nd number: "))
        except ValueError:
            print("Sorry, enter a number: ")
            continue
        else:
            break
   
    
 # How to create a while loop for the below operator to
# ask to input the correct operator, if I keep inputting wrong operators.
# Thanks Very Much!

    operation = input("Enter Operator: ")
        
    if operation == "+":
        result = first_num + second_number
                   
    elif operation == "-":
        result = first_num - second_number
                     
    elif operation == "*":
        result = first_num * second_number
                    
    elif operation == "/":
        result = first_num / second_number
    elif operation == "%":
        result = first_num % second_number

# This will RUN, but when operator is inputted WRONG, it will go back
# to ask me to enter first number and second number.(the 2nd While Loop I guess).
# I want it to ask for the input of Operator Again and Again UnTil the input is the correct operator.
# NOT go back to ask first number and second number.
# I tried to create the third while loop for the operators
# but I couldn't get it to run. only when back to 2nd while Loop.
#I thought it was indentation error, but I tried, still couldn't get it to work.
        

    print(result)
    break
GAEfan
  • 11,244
  • 2
  • 17
  • 33
0

From what I understood from your question one solution would be to create a array with all the operators and check if the operation entered by the user is inside that array, if not, then loop again asking for a new input, and if the operator is found inside the array, then just go through the if statements to perform the operation.

operators = ["+", "x", "-", "/", "%"]

operation = input("Enter Operator: ")

while(operation not in operators):
    operation = input("Enter Operator: ")

#If loops here
PSM
  • 429
  • 4
  • 15
0

I'd create a dict of the operators and then have my while ... input loop check to make sure the entered string is in the dict:

operators = {
    '+': float.__add__,
    '-': float.__sub__,
    '*': float.__mul__,
    '/': float.__truediv__,
    '%': float.__mod__,
}

while True:
    operator = input("Enter Operator: ")
    if operator in operators:
        break

result = operators[operator](first_num, second_number)
Samwise
  • 68,105
  • 3
  • 30
  • 44