【问题标题】:Get data from Abstract to Encapsulation从抽象获取数据到封装
【发布时间】:2019-05-23 17:39:18
【问题描述】:

我无法从我设置为抽象的类中获取数据。在这堂课中,如果我按'1',它应该让我去我可以去的部分

  • 输入名字和姓氏:
  • 输入地址:
  • 输入电话:
  • 输入电子邮件地址:

我真的不知道从哪里开始。

    class Student: # abstract
       def student_submenu (a,select_num):
         a.select_num = select_num

       def ss_1 (z):

         print("\n")
         print(" ===== | SUBMENU - STUDENTS | ===== ")
         print("||                                 ||")
         print("=====================================")
         print(" 1 - ADD NEW STUDENT")
         choice2 = input()
            while choice2 not in ['1', '2', '3', '4', '5']:
            print("Invalid Please try again")
            return select_num.student_submenu()



      def ss_2 (z1):
        if(choice2 == '1'):
        firstlast = input("Enter First name and last name: ")
        address = input("Enter Address: ")
        phone = input("Enter phone: ")
        email = input("Enter Email-Address: ") 
        print("\n")

        print(firstlast)
        print(address)
        print(phone)
        print(email)

  class Main:  #encapsulation
      def main_page(self):
        print("===== | MAIN MENU |=====")
        print("||                    ||")
        print("========================")
        print("1 - STUDENT ")
        print("2 - SUBJECT ")
        print("3 - QUIT ")
        allchoice = ['1', '2', '3'];
        choice = input()



       while choice not in allchoice:
         print("Invalid Please try again")
         choice = input()

       if choice == '1':
         a = Student()
         a.student_submenu

      elif choice == '2':
         print("back world")
      else:
        quit()

  mainout = Main()
  mainout.main_page()

当我运行时,我收到如下错误:

line 2
class
    ^
SyntaxError: invalid syntax

【问题讨论】:

  • 您好,欢迎来到 SO。您的缩进都搞砸了,不确定它是来自您的复制粘贴到 SO 中还是来自您的代码。这可能就是您收到错误的原因。我建议对代码块使用三重`,尤其是在使用 Python 时。

标签: python abstract


【解决方案1】:

我相信您的问题是由于缩进造成的。在 Python 中,缩进非常重要,必须保持一致。模块级别的任何东西都不能缩进,缩进的时候要保持一致(一般默认4个空格)

【讨论】:

    【解决方案2】:

    扩展该问题的另一个答案:您使用的缩进极为不一致且不正确。如果你使用不正确的缩进,你最终会出现很多错误,包括你得到的那个。

    每个新块都应该缩进 4 个空格(制表符也可以),并且在全局范围内定义的所有内容(在这种情况下是您的 2 个类和最后 2 行代码)根本不应该缩进。

    尝试将您的代码更改为:

    class Student: # abstract
        def student_submenu (a,select_num):
            a.select_num = select_num
    
        def ss_1 (z):
            print("\n")
            print(" ===== | SUBMENU - STUDENTS | ===== ")
            print("||                                 ||")
            print("=====================================")
            print(" 1 - ADD NEW STUDENT")
            choice2 = input()
            while choice2 not in ['1', '2', '3', '4', '5']:
               print("Invalid Please try again")
            return select_num.student_submenu()
    
    
    
        def ss_2 (z1):
            if(choice2 == '1'):
                firstlast = input("Enter First name and last name: ")
                address = input("Enter Address: ")
                phone = input("Enter phone: ")
                email = input("Enter Email-Address: ") 
    
                print("\n")
                print(firstlast)
                print(address)
                print(phone)
                print(email)
    
    class Main:  #encapsulation
        def main_page(self):
            print("===== | MAIN MENU |=====")
            print("||                    ||")
            print("========================")
            print("1 - STUDENT ")
            print("2 - SUBJECT ")
            print("3 - QUIT ")
            allchoice = ['1', '2', '3'];
            choice = input()
    
    
    
            while choice not in allchoice:
                print("Invalid Please try again")
                choice = input()
                if choice == '1':
                    a = Student()
                    a.student_submenu
                elif choice == '2':
                    print("back world")
                else:
                    quit()
    
    mainout = Main()
    mainout.main_page()
    

    另见:

    【讨论】:

      猜你喜欢
      • 2017-11-29
      • 2018-06-29
      • 2012-05-24
      • 2021-07-24
      • 2019-11-28
      • 2012-02-16
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      相关资源
      最近更新 更多