Python Forum
What is wrong with this code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is wrong with this code
#1
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")
Reply
#2
give us a clue.
what are the symptoms?
provide a test case that fails.
Reply
#3
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.
Reply
#4
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.
Reply
#5
? The odd part is that I came across the exact same code twice in 12 hours...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 512 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 882 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,596 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,299 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,871 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,628 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,556 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,646 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,538 Jun-09-2021, 10:01 AM
Last Post: topfox
  What is wrong with my code??? MrLeads 15 5,120 Sep-16-2020, 02:00 PM
Last Post: MrLeads

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020