0

Help, I'm trying to make a calculator but it's not working

user_input1=float(input("")) #defining first Number operation=(input("")) #multiply, divide, add, subtract user_input3=float(input("")) #defining second number if operation==("*"): #if/elif statements to read what operation the user has inputed a= user_input3 * user_input1 elif operation==("+"): a=user_input1 + user_input3 elif operation==("-"): a=user_input1 - user_input3 #if statement is correct it defines a as the operation elif operation==("÷"): a=user_input1 // user_input3 print(user_input1,operation,user_input3,"=",a) For some reason when I divide it says that a isn't defined when it's clearly defined.

24th Aug 2016, 2:57 AM
Darth Vador
Darth Vador - avatar
6 Respuestas
+ 3
try to change the division symbol to slash "/" It's hard to find "÷" symbol on the phone, and there is no "÷" on the PC keyboard. maybe it's some special symbol and python doesn't recognise it Update: found what's wrong. run this code and type ÷ when prompted: a = input('type ÷ : ') print('inner: ÷') print('outer:'+a) and you will see that when you type ÷ symbol as input, it stays the same, but python (maybe only in this app, and maybe everywhere) doesn't seem to know this symbol when printing it, replacing it with double ~. somehow the codes of ÷ is different in this two cases.
24th Aug 2016, 7:35 AM
Demeth
Demeth - avatar
+ 1
Try this simpler alternative of a calculator: print("Calculator") x = input("") xy = input("") y = input("") if xy == "+": print(x+y) elif xy == "-": print(x-y) elif xy == "*": print(x*y) elif xy == "/": print(x/y) else: print("SYNTAX ERROR") Plus, does anyone knows how to output to one line?
24th Aug 2016, 11:16 AM
Mahmoud Nassif
Mahmoud Nassif - avatar
0
is it only when u divide ? or with every operation
24th Aug 2016, 4:31 AM
John
0
Just dividing, which is weird because it's the same as every other operation.
24th Aug 2016, 6:00 AM
Darth Vador
Darth Vador - avatar
0
The idea is if the user input's '÷' then it would define 'a' as dividing the first number by the second number and the next line was printing the outcome. But literally for only dividing it doesn't define a for some reason.
24th Aug 2016, 6:03 AM
Darth Vador
Darth Vador - avatar
0
The symbol for dividing is '//' and I've tested it by dividing two numbers with that symbol and it worked. For some reason it doesn't recognize 'a' as a defined function when you can see (on the last elif) it defines 'a' as the division between user_input1 and user_input2. The idea is if "operation" is inputed as "÷" it then defines 'a' as user_input1 // user_input2 and thus prints it as such. The division symbol itself is irrelevant because it's a string. The error message says 'a' isn't defined. Maybe it just doesn't work in mine? Idk. Try using it on yours maybe and change the "÷" to your division symbol since it may be different from mines and see if it works. As long as it works at all and I'm not being a dummy I'm good lol. Update: @Dmitriy Silkin, IT WORKED?!? I DON'T UNDERSTAND, HOW DOES CHANGING THE SYMBOL WORK??? THANK YOU SO MUCH I honestly don't understand though, how does it work? That shouldn't work because of that and if it does then how??? I don't get it lol. Thank you so much, again, this bug has been really bothering me.
24th Aug 2016, 8:02 AM
Darth Vador
Darth Vador - avatar