dimanche 21 juin 2015

loop does not restart after elif condition is met

Trying to create a calculator that will print "Something is wrong with your equation: there is nothing in it" when a user inputs spaces or nothing. But when I test " " I get an IndexError: list index out of range.

I thought the way I wrote the code, if the len(input)<=1 it would restart the while loop, but this is not case.

while count==0:
        userInput = input("Input your equation: ")
        groupedList = userInput.split()
        print("You input:", userInput)
        if userInput == "End":
            print("Goodbye")
            count = 1
        elif len(groupedList)<=1:
            print("Something is wrong with your equation: there is nothing in it")
        for x in groupedList:
            if x == "+" or x == "-" or x == "//" or x == "/" or x == "%" or x == "*" or x == "**":
                groupedList.remove(x)
                z = x
        else:
            if groupedList[0].isalpha() or groupedList[1].isalpha():
                print("Something is wrong with your equation: there is nothing in it")
            else:
                ungroupedList = list(userInput)
                number1 = float(groupedList[0])
                number2 = float(groupedList[1])
                print("The answer to your equation is: ", calculation(number1,number2,z))

Aucun commentaire:

Enregistrer un commentaire