Why isn't my calculator code working? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why isn't my calculator code working?

Ok I've revised it a little but it seems that it doesn't register c = input() therefore not continuing the rest of the code. def calc(): print("Press 1 for addition") print("Press 2 for subtraction") print("Press 3 for multiplication") print("Press 4 for division") c = input() if c == 1: print("Enter a number") x = input() print("Enter another number") y = input() return x + y elif c == 2: print("Enter a number") x = input() print("Enter another number") y = input() return x - y elif c == 3: print("Enter a number") x = input() print("Enter another number") y = input() return x * y elif c == 4: print("Enter a number") x = input() print("Enter another number") y = input() return x / y calc()

14th Dec 2016, 8:31 PM
Adyn Glowniak
3 Answers
+ 1
instead of return try print()
14th Dec 2016, 9:20 PM
Uran Kajtazaj
Uran Kajtazaj - avatar
0
You are asking for input, but not assigning that input to a variable. It should be like this: variableName = input(); then, the variableName would be equal to anything you type. Your code should look like this: def calc(): print("Press 1 for addition") print("Press 2 for subtraction") print("Press 3 for multiplication") print("Press 4 for division") c = input() if c == 1: print("Enter a number") x = input(); print("Enter another number") y = input() return x + y elif c == 2: print("Enter a number") x = input(); print("Enter another number") y = input() return x - y elif c == 3: print("Enter a number") x = input() print("Enter another number") y = input() return x * y elif c == 4: print("Enter a number") x = input() print("Enter another number") y = input(); return x / y calc()
14th Dec 2016, 8:28 PM
Uran Kajtazaj
Uran Kajtazaj - avatar
- 1
This code looks like it should works but beyond the choice menu it doesn't continue. After the user inputs c the code does not execute the rest. def calc(): print("Press 1 for addition") print("Press 2 for subtraction") print("Press 3 for multiplication") print("Press 4 for division") c = input() if c == 1: print("Enter a number") x = input() print("Enter another number") y = input() return x + y elif c == 2: print("Enter a number") x = input() print("Enter another number") y = input() return x - y elif c == 3: print("Enter a number") x = input() print("Enter another number") y = input() return x * y elif c == 4: print("Enter a number") x = input() print("Enter another number") y = input() return x / y calc()
14th Dec 2016, 8:33 PM
Adyn Glowniak