Python Forum

Full Version: What is wrong with this code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need help with this calculator code

print("Basic Python Calculator\n")
print("Created With the Sololearn Python Course\n")

while True:
    print("Options:")
    print("Enter '+' to add two numbers")
    print("Enter '-' to subtract two numbers")
    print("Enter '*' to add multiply numbers")
    print("Enter '/' to divide two numbers")
    print("Enter 'quit' to end the program")
    user_input = input(": ")
    
    if user_input == "quit":
        break
    elif user_input == "+":
        num1 = float(input("Enter a number: "))
        num2 = float(input("Enter another number: "))
    result1 = str(num1 + num2)
    print("The result is: " + result1)
        
    elif user_input == "-":
        num3 = float(input("Enter a number: "))
        num4 = float(input("Enter another number: "))
    result2 = str(num3 - num4)
    print("The result is: " + result2)
    
    elif user_input == "*":
        num5 = float(input("Enter a number: "))
        num6 = float(input("Enter another number: "))
    result3 = str(num5 * num6)
    print("The result is: " + result3)
    
    elif user_input == "/":
        num7 = float(input("Enter a number: "))
        num8 = float(input("Enter another number: "))
    result4 = str(num7 / num8)
    print("The result is: " + result4)
    
    else:
        print("ERROR: Unknown Input")
give us a clue.
what are the symptoms?
provide a test case that fails.
Oddly enough, I responded to this on Codementor. It's the indenting that's the problem. Lines 18, 19, 24, 25, 30, 31, 36, and 37 need to be indented one more level because those are part of the if/elif blocks.
Quote:Oddly enough
Not odd, this is the way python distinguishes a block of code, the same as 'C' uses brackets.
Without indentation of line 18, the return would be outside of the 'brackets' (Analogy).
This should have given you an error traceback that would have immediately pointed to the problem.
? The odd part is that I came across the exact same code twice in 12 hours...