Pls What is wrong with this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Pls What is wrong with this code

choice = input("What Operation do you want to perform(Add,Subtract,Divide,Multiply)") add_x = float(input("Enter Number 1:") add_y = float(input("Enter Number 2:") subtract_x = float(input("Enter Number 1:") subtract_y = float(input("Enter Number 2:") divide_x = float(input("Enter Number:") divide_y = float(input("Enter Number 2:") multiply_x = float(input("Enter Number 1:") mulitiply_y = float(input("Enter Number 2:") if choice == "Add": result = (add_x + add_y) print(z) elif choice == "Subtract": result = (subtract_x - subtract_y) print(z) elif choice == "Divide": result = (divide_x / divide_y) print(z) elif choice == "Multiplication": result = (multiply_x * multiply_y) print(z)

8th Feb 2020, 10:12 PM
Oluwaseyifunmi Dosunmu
Oluwaseyifunmi Dosunmu - avatar
12 Answers
+ 3
Isabel You are right about 2 and 3. As for 1, after input of choice the code continues with reading the numbers, so when reaching the if/elif it will have all the values it needs. As a recomandation: is to move the input of numbers inside the cases so that he reads only 2 numbers. It doesn't need to read 8 numbers and to calculate only with two. Edit: Or to read only two numbers before and to perform on those the chosen operation.
8th Feb 2020, 10:42 PM
Mihai Apostol
Mihai Apostol - avatar
+ 3
Isabel You're welcome. We solved the problem and agreed on everything. Though it seems the OP "left the building" a while ago.
9th Feb 2020, 3:47 PM
Mihai Apostol
Mihai Apostol - avatar
+ 2
There are 3 possible issues: 1. After the first input prompt, after readers choose add, subtract, etc, your code responds with result = (equation). You are missing a step in between, where you ask readers to provide values for add_x, etc, so the code is trying to calculate information that it does not have. 2. All of your float(input)) are missing the second ) on the right side. 3. If you use “result =“ to save the results of your calculations, your code will not be able to print(z). You have never told it what z means. (All of these are problems I have all the time. So much of learning to code is learning to pay attention to details!)
8th Feb 2020, 10:35 PM
Isabel
Isabel - avatar
+ 2
You only need three inputs: 1. the choice... 2. the first number... 3. the second number.
8th Feb 2020, 11:27 PM
rodwynnejones
rodwynnejones - avatar
+ 2
Mihai I copied the code above into Code Playground and got “error parsing EOF” when it tried to read the float variables. Is it because Code Playground doesn’t support a second dialog box for user input?
9th Feb 2020, 5:10 AM
Isabel
Isabel - avatar
+ 2
Isabel Short answer: yes, in Playground all inputs are delivered before execution of code. Long answer: After adding missing paranthesis and replacing variables z with result or result with z, it should: In Playground: Input all data on separate lines Eg. Add 1 2 3 4 5 5 6 7 8 and then SUBMIT In IDE: Input data as prompted. Eg. Add return; 1 return; 2 return; 3 return;.....8 return return as in enter key
9th Feb 2020, 6:35 AM
Mihai Apostol
Mihai Apostol - avatar
+ 2
Thank you, Mihai I stripped the code down to this and it did run on Code Playground. choice = input("What Operation?") x = float(input("Enter Number 1:")) y = float(input("Enter Number 2:")) if choice == "Add": z = (x + y) print(z) elif choice == "Subtract": z = (x - y) print(z) elif choice == "Divide": z = (x / y) print(z) elif choice == "Multiplication": z = (x * y) print(z)
9th Feb 2020, 3:36 PM
Isabel
Isabel - avatar
+ 1
You are missing some paranthesis when reading the numbers.
8th Feb 2020, 10:15 PM
Mihai Apostol
Mihai Apostol - avatar
+ 1
You are storing the results in the result variable but you are printing z. And I recomend to move the input statements 2 by 2 into the if/elif cases.
8th Feb 2020, 10:26 PM
Mihai Apostol
Mihai Apostol - avatar
0
Is that all
8th Feb 2020, 10:18 PM
Oluwaseyifunmi Dosunmu
Oluwaseyifunmi Dosunmu - avatar
0
I don't know much about Python, but as I understand your code correctly, it should be print(result).
10th Feb 2020, 5:27 PM
KateJ
KateJ - avatar
0
Hello KateJ. There are many issues in the code in the post/question. You can sort the answers by date and read them. In the end you will find a working code snippet by Isabel.
10th Feb 2020, 5:54 PM
Mihai Apostol
Mihai Apostol - avatar