dimanche 9 juillet 2017

"if", "elif", "else" syntax error

I'm trying to understand how to use if, elif and else in python. I'm trying to print ascending or descending order taking initial number from the user. But I'm not able to execute the last block "#main" due to syntax error at "elif". I've tried correcting the indent, used CMD to execute .py file but error not resolved. I mostly suspect indent is the issue, but if there is more than that I don't know. Could you guys help me out here. Also if you could share best practices while using "if", "elif", "else", it would be great help.

I'm using Windows 10 error: "invalid syntax" and highlights "elif" in last block
Python ver: 3.6.1 using IDLE that came with Python installation.

# function to print from n to 0
def descend(a):
    while a >=0:
        print (a)
        a=a-1
    return a
# function to print 0 to n
def ascend(a):
    while a <=0:
        print (a)
        a=a+1
    return a
# function to enter n value
#def value (a):
 #   number = input ("enter number ")
  #  num = int(number)
   # return a
# function for menu
def menu():
    print ("Choose options below")
    print ("1. ascending order")
    print ("2. descending order")
    print ("3. quit")
    return input ("chose your option:")

#main
loop = 1
choice = 0
while loop==1:
    choice = menu()
    if choice == 1:
        ascend((input("a number"))
    elif choice == '2':
         descend(input("a number"))
    else choice == '3':
         loop==0

Aucun commentaire:

Enregistrer un commentaire