+ 1

I tried to make a calculator, it DOES WORK NOW! :D

x=input('Enter a number?\n') y=input("Enter another one!\n") z=input('1 to + \n 2 to - \n 3 to * \n 4 to /\n') if z=='1': print(int(x)+int(y)) if z=='2': print(int(x)-int(y)) if z=='3': print(int(x)*int(y)) if z=='4': print(int(x)/int(y)) IT WORKED :D

22nd Mar 2017, 5:06 PM
Sammy Bunckly
Sammy Bunckly - avatar
6 Respuestas
+ 1
Your variables are strings right now... try if z='1' print (int(x)+int(y)) then go through it with choosing 2 numbers and make sure you use 1 operator...it'll work. That should point you in the right direction.
22nd Mar 2017, 4:47 PM
Garan Weber
Garan Weber - avatar
0
print (int(x)+int(y)) what is the diffrence?
22nd Mar 2017, 4:57 PM
Sammy Bunckly
Sammy Bunckly - avatar
0
x=input('Enter a number?\n') y=input("Enter another one!\n") z=input('1 to + \n 2 to - \n 3 to * \n 4 to /\n') if z==1: print(int(x)+int(y)) if z==2: print(int(x)-int(y)) if z==3: print(int(x)*int(y)) if z==4: print(int(x)/int(y)) is the new code, it's still blank
22nd Mar 2017, 5:02 PM
Sammy Bunckly
Sammy Bunckly - avatar
0
int(x) converts your x variable from a string to an integer. The best way to resolve your issue right now...would be to convert all of your inputs to an integer at the time of entry. i.e. x=int(input()) If you do that with each of your 3 variables, then the rest of your code should work.
22nd Mar 2017, 5:02 PM
Garan Weber
Garan Weber - avatar
0
The trouble with this code, is that your z variable is still a string when you are comparing it, you are comparing it to an Integer (whole number) if you do this if z=='1' then you are saying: If z is equal to the string 1 (the quotes make it a string) x=input('Enter a number?\n') y=input("Enter another one!\n") z=input('1 to + \n 2 to - \n 3 to * \n 4 to /\n') if z==1: print(int(x)+int(y)) if z==2: print(int(x)-int(y)) if z==3: print(int(x)*int(y)) if z==4: print(int(x)/int(y)) is the new code, it's still blank
22nd Mar 2017, 5:08 PM
Garan Weber
Garan Weber - avatar
0
The new code works. I also put '' around the numbers like if z=='2':
22nd Mar 2017, 5:14 PM
Sammy Bunckly
Sammy Bunckly - avatar