What's wrong with my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What's wrong with my code?

def add(x,y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): return x // y print("Enter choice(+/-/×/÷): ") while True: choice = input("Enter choice(+/-/×/÷): ") if choice in ('+','-', '×', '÷',): num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) if choice == '+': print(num1, "+", num2, "=", add(num1, num2)) elif choice == '-': print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '×': print(num1, "×", num2, "=", multiply(num1, num2)) elif choice == '÷': print(num1, "÷", num2, "=", divide(num1, num2)) break else: print("Invalid Input")

11th May 2021, 11:49 AM
Wan Khaira Wardiya
Wan Khaira Wardiya - avatar
2 Answers
+ 9
Just remove that while statment and break from the coad. def add(x,y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): return x // y choice = input("Enter choice(+,-,×,÷): ") if choice in ('+','-', '×', '÷',): num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) if choice == '+': print(num1, "+", num2, "=", add(num1, num2)) elif choice == '-': print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '×': print(num1, "×", num2, "=", multiply(num1, num2)) elif choice == '÷': print(num1, "÷", num2, "=", divide(num1, num2)) else: print("Invalid Input")
11th May 2021, 12:48 PM
Kǟrɨsɦmǟ ❥
Kǟrɨsɦmǟ ❥ - avatar
+ 1
Wan Khaira Wardiya Karisharma, have reasone. Because the glich while Is infinite. Example: while True: choice = input("…") if choice in ("…"): break else: continue …
8th Sep 2021, 8:07 PM
CGO!
CGO! - avatar