"""
This program presents a menu to the user and based upon the selection made
invokes already existing programs respectively.
"""
import sys
def get_numbers():
"""get the upper limit of numbers the user wishes to input"""
limit = int(raw_input('Enter the upper limit: '))
numbers = []
# obtain the numbers from user and add them to list
counter = 1
while counter <= limit:
numbers.append(int(raw_input('Enter number %d: ' % (counter))))
counter += 1
return numbers
def main():
continue_loop = True
while continue_loop:
# display a menu for the user to choose
print('1.Sum of numbers')
print('2.Get average of numbers')
print('X-quit')
choice = raw_input('Choose between the following options:')
# if choice made is to quit the application then do the same
if choice == 'x' or 'X':
continue_loop = False
sys.exit(0)
"""elif choice == '1':
# invoke module to perform 'sum' and display it
numbers = get_numbers()
continue_loop = False
print 'Ready to perform sum!'
elif choice == '2':
# invoke module to perform 'average' and display it
numbers = get_numbers()
continue_loop = False
print 'Ready to perform average!'"""
else:
continue_loop = False
print 'Invalid choice!'
if __name__ == '__main__':
main()
只有當我輸入'x'或'X'作爲輸入時,我的程序纔會處理。對於其他投入,該計劃剛剛退出。我已經評論了elif部分,並且只運行了if和else子句。現在引發語法錯誤。我究竟做錯了什麼?python if - elif-else的用法和說明
你的語法錯誤,從'其他地方發過來:'行正太一個空格縮進。 –