jeudi 26 juillet 2018

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.

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.

Here's a part of my code, please, suggest, what is wrong here.

while True:
    try:
        x = float(input("Enter your number\n"))
        y = float(input("Enter another number\n"))
        operator = input("Enter the operation\n")
        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 number\n"))
            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 number\n"))

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.

Aucun commentaire:

Enregistrer un commentaire