Python seems to ignore “elif” part

Multi tool use
Python seems to ignore “elif” part
I've decided to make a simple calculator. It asks the user for two numbers, what does he want to do with them, and then asks how many numbers there actually is.
I've come up with nothing better than do one if
-statement when there are 2 numbers, one elif
when there are 3 of them, and one else
part, when it just asks you for more numbers until you type "end" and then calculates.
if
elif
else
The thing is, it seems to completely ignore elif
part, it doesn't even asks the question there. Everything else works just fine. And i just can't see what's wrong there.
elif
Here's a part of my code, please, suggest, what is wrong here.
while True:
try:
x = float(input("Enter your numbern"))
y = float(input("Enter another numbern"))
operator = input("Enter the operationn")
check = float(input("How many numbers are there?n"))
if check == 2:
if operator == "+" :
print("Result = " + str(x + y))
elif operator == "-" :
print("Result = " + str(x - y))
elif operator == "*" :
print("Result = " + str(x * y))
elif operator == "/" :
print("Result = " + str(x / y))
elif check == 3:
z = float(input("Enter the third numbern"))
if operator == "+" :
print("Result = " + str(x + y + z))
elif operator == "-" :
print("Result = " + str(x- y - z))
elif operator == "*" :
print("Result = " + str(x * y * z))
elif operator == "/" :
print("Result = " + str(x / y / z))
else:
num = float(input("Enter a numbern"))
And the code goes on. I just can't get why if
works and elif
doesn't, it just continues to else
. Thank you for any help you can provide an and sorry for my bad English.
("Except" is there, but later in the code)
if
elif
else
Shouldn't the whole try block be indented?
– swhat
35 mins ago
Never compare floats with
==
. You should make check
an int rather than a float.– Aran-Fey
34 mins ago
==
check
Of course, hare int is perfectly fine, and float is an overkill. But I disagree with the rest of the comment: Comparing floats is fine when they have no error in them. In this case the float is created from an integer, and float(3) == 3 works just fine. The problem with floats comes when inaccurate operations kick in like 1.2 or 1.0/3. But converting a small int to float is 100% accurate.Unfortunately, many programmers don't know how to deal with float inaccuracy, so it is better to avoid the issue of == altogether.
– Michael Veksler
30 mins ago
I didn't try every branch, but it seemed to work for me. What did you try?
– swhat
28 mins ago
2 Answers
2
while True:
x = float(input("Enter your numbern "))
y = float(input("Enter another numbern "))
operator = input("Enter the operationn ")
check = float(input("How many numbers are there?n "))
if check == 2:
if operator == "+" :
print("Result = " + str(x + y))
elif operator == "-" :
print("Result = " + str(x - y))
elif operator == "*" :
print("Result = " + str(x * y))
elif operator == "/" :
print("Result = " + str(x / y))
elif check == 3:
z = float(input("Enter the third numbern "))
if operator == "+" :
print("Result = " + str(x + y + z))
elif operator == "-" :
print("Result = " + str(x- y - z))
elif operator == "*" :
print("Result = " + str(x * y * z))
elif operator == "/" :
print("Result = " + str(x / y / z))
else:
num = float(input("Enter a numbern "))
Seems to work when I ran it in idle. I'm not sure if the input was working on your side but when I tried it I couldn't add any input because you need to leave a space after the newline character. I removed the try because it was probably silently catching your errors, causing it to skip the rest of the try block.
The code he gave was incomplete, so we do not see the closing of the try statement
– dheiberg
19 mins ago
Yea it was, I couldnt get it to run at first because of that
– Clint
18 mins ago
Sry for that, except block was there, but latter on the code, and the code itself was pretty big, so i didn't post it all.
– LionKing
4 mins ago
Sorry to bother you all. It works perfectly fine on idle, though when i run it on Visual Studio "elif" part is ignored. I'm not really sure why. Probably a lesson for me to run things on ilde when i face problems like that. Still can't get why Visual Studio does that...
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you give us an example of some of your input and outputs/expected output?
– dheiberg
36 mins ago