elif语句上的语法错误无效

时间:2013-10-10 02:45:07

标签: python python-3.x

我正在编写这个程序,让用户可以选择显示,更改,添加,删除,写入或退出的选项 我在程序中的这个elif语句中一直得到无效的语法错误,我不知道为什么?

DISPLAY = 'D'
CHANGE = 'C'
ADD = 'A'
REMOVE = 'R'
WRITE = 'W'
QUIT = 'Q'

#main function
def main ():
    print()
    print()
    print('\t Wombat Valley Tennis Club')
    print('\t   Member Register')
    print('\t   =======================')
    main_dic = {}
    with open ('wvtc_data.txt','r')as x:
          for line in x:
                line = line.rstrip ('\n')
                items = line.split (':')
                key,value = items[0], items[1:]
                main_dic[key] = values
                choice = input('choice: ')
                while choice != QUIT:
                      choice = get_menu_choice()
                      if choice==DISPLAY:
                            display(main_dic)
                            elif choice==CHANGE:
                                  change(main_dic)
                                  elif choice== REMOVE:
                                        remove (main_dic)
                                        elif choice==WRITE:
                                              write(main_dic)

def get_menu_choice():
      print()
      print("\t\t Main Menu")
      print()
      print('\t<D>isplay member details')
      print('\t<C>hange member details')
      print('\t<A>dd a new member')
      print('\t<R>emove a member')
      print('\t<W>rite updated details to file')
      print('\t<Q>uit')
      print()
      print('Your choice (D, C, A, R, W OR Q)?\t[Note: Only Uppercase]')
      print()
      choice = input("Enter your choice: ")
      while choice < DISPLAY and choice < CHANGE or choice > QUIT:
            choice = input ('Enter your choice: ')
            return choice
def display(main_dic):
      name = input('Type the name of the member:')
      print()
      print (main_dic.get(name, 'not found!'))
      print()
      print()

def change(main_dic):
      name=input("Enter the member number")
      print()
      print(main_dic.get(name,'not found!'))
      print()
      NAME = 1
      EMAIL = 2
      PHONE = 3
      print('Please select')
      print('==============')
      print('Change Name<1>')
      print('Change E-mail<2>')
      print('Change Phone Number<3>')
      print()
      if choose == NAME:
            new_name = input('Enter the name: ')
            print()
            print("Data has been changed")
            main_dic[name][0]=new_name
            print (mem_info[name])
            print()
            elif choose == EMAIL:
                  new_email=input('Enter the new email address:')
                  print ('Email address has been changed')

                  #change the data
                  main_dic[name][1]=new_email
                  print(mem_info[name])
                  print()
                  elif choose == PHONE:
                        new_phone = int (input('Enter the new phone number'))
                        print ("phone number has been changed")
                        main_dic[name][2]=new_phone
                        print(main_dic[name])
                        print
def write(main_dic):
      print()
      name = input ("Enter the name of a member:")
      print()
      print (mem_info.get(name,'Not found!'))
      print()
main()




























main()

此外,我们也非常感谢您提供此代码的任何帮助或建议。

1 个答案:

答案 0 :(得分:3)

这是格式化问题。 elif必须与它们所属的if在同一列中开始。

实施例

if something:
    do_somtething()
elif something_else:
    do_the_other_thing()