0

help me realize what's wrong with this code?

i'm just trying to practice the simple calculator project from python module. and this happens: this is the output console: Options: enter 'add' to add two numbers enter 'subtract' to subtract two numbers enter 'multiply' to multiply two numbers enter 'divide' to divide two numbers enter 'quit' to end the program : enter a number: Traceback (most recent call last): File "..\Playground\", line 21, in <module> num1 = float(input("enter a number: ")) EOFError: EOF when reading a line and this is the code: while True: print("Options: ") print("enter 'add' to add two numbers ") print("enter 'subtract' to subtract two numbers") print("enter 'multiply' to multiply two numbers") print("enter 'divide' to divide two numbers") print("enter 'quit' to end the program") user_input = input(": ") user = user_input if user == "quit": break elif user == 'add': num1 = float(input("enter a number: ")) num1 = float(input("enter another number: ")) result = str(num1 + num2) print("the answer is", result) elif user == "subtract": num1 = float(input("enter a number: ")) num1 = float(input("enter another number: ")) result = str(num1 - num2) print("the answer is", result) elif user == "multiply": num1 = float(input("enter a number: ")) num1 = float(input("enter another number: ")) result = str(num1 * num2) print("the answer is", result) elif user == "divide": num1 = float(input("enter a number: ")) num1 = float(input("enter another number: ")) result = str(num1 / num2) print("the answer is", result) else: print("unknown input")

27th Oct 2019, 1:24 AM
Muhammad Saleh Alatas
Muhammad Saleh Alatas - avatar
4 odpowiedzi
+ 2
You only have one mistake in your code, you forgot and assigned both to num1, assign the second input to num2: num1 = float(input("enter a number: ")) num2 = float(input("enter another number: ") Now about the error, it has nothing to do with your code, it's just how sololearn input works. If you were to try and run that code on your machine it would work perfectly (with that little change I told you to do). Sololearn takes all inputs before running the code, which is why it doesn't work. Read more about sololearn input here: https://www.sololearn.com/discuss/770763/?ref=app https://www.sololearn.com/discuss/1315621/?ref=app https://www.sololearn.com/discuss/628061/?ref=app https://www.sololearn.com/discuss/1956873/?ref=app https://www.sololearn.com/discuss/1544816/?ref=app https://www.sololearn.com/discuss/945510/?ref=app https://www.sololearn.com/discuss/863989/?ref=app https://www.sololearn.com/discuss/1407007/?ref=app https://www.sololearn.com/discuss/1588387/?ref=app
27th Oct 2019, 1:36 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Please write this code in code playground then share it, it's hard to read and test on a plain text.
27th Oct 2019, 1:29 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
@aymane boukrouh thanks for your help, but sadly the code is still do'nt work...
27th Oct 2019, 2:11 AM
Muhammad Saleh Alatas
Muhammad Saleh Alatas - avatar
0
Muhammad Saleh Alatas what's the problem ? As I said, it won't work because the input method in sololearn is different, so it will ALWAYS output an error at then end, try to run it on your machine, it worked for me, I've done ma.y tests
27th Oct 2019, 10:41 AM
Aymane Boukrouh
Aymane Boukrouh - avatar