首页 > 解决方案 > 使用 if 语句将对象实例附加到列表并在另一个分支中打印

问题描述

我创建了一些关于宠物的类,以下代码是我的 main() 函数的一部分,首先,要求用户选择他们想做的一件事。也就是说,如果使用输入“1”,他们将添加一些宠物实例。同时,我想将宠物实例的部分信息附加到一个列表中。然后,如果用户选择阅读此信息。我想在另一个 if 语句分支中打印它。那是用户输入'2'的时候。当我已经生成一些宠物实例后输入 2 时,就会出现问题。名为 l_weight 的列表总是无效的。我该如何解决?我已经尝试使用全局列表但不起作用

def main():
l_weight=[]
print("========menu=========")
print("1. to add a pet")
print("2. print current weight for all pet")
print("3. print all pets and owners")
print("4. to exist system")
a=int(input("you selection(just input the number before each function)"))
while(True):
    if a==1: 
        a=int(input("please select what type of pet would be added: 1-- mammals 2--fish 3--amphibians"))
        name = input('please enter the  name of pet:')
        dob = input('please enter the dob of pet:(year,month,day)')
        bw = input('please enter the birth weight:')
        name = input('please enter the owner name:')
        address = input('please enter the onwer address:')
        if a==1:
            ls = input('please enter the litter size:')
            hs = input('pet has claws(yes or no):')
            op=mammals(name,dob,bw,name,address,ls,hs)
            print(op)
            l_weight.append(op.get_current_weight)      
        elif a==2:
            sc = input('please enter the scale condition:')
            sl = input('please enter the scale length:')
            op =fish(name,dob,bw,name,address,sc,sl)
            print(op)
            l_weight.append(op.get_current_weight)
        elif a==3:
            iv = input('is venomous(yes or no):')
            op =amphibians(name,dob,bw,name,address,iv)
            print(op)
            l_weight.append(op.get_current_weight)
        else:
            print(' input wrong vaule,please choose a number from 1,2 or 3')
        return main()
    elif a==2:  
        for i in l_weight:
            print(i)
        
        return main()

标签: pythonlistvariables

解决方案


l_weight() 不附加的原因是因为在您的代码中,您命名了列表l_weight,然后在其余代码中将其编写为l_weigh

它应该是:

def main():
  l_weight=[]

  print("========menu=========")
  print("1. to add a pet")
  print("2. print current weight for all pet")
  print("3. print all pets and owners")
  print("4. to exist system")
  a=int(input("you selection(just input the number before each function)"))

  while(True):
      if a==1: 
          a=int(input("please select what type of pet would be added: 1-- mammals 2--fish 3--amphibians"))
          name = input('please enter the  name of pet:')
          dob = input('please enter the dob of pet:(year,month,day)')
          bw = input('please enter the birth weight:')
          name = input('please enter the owner name:')
          address = input('please enter the onwer address:')

          if a==1:
              ls = input('please enter the litter size:')
              hs = input('pet has claws(yes or no):')
              op=mammals(name,dob,bw,name,address,ls,hs)
              print(op)
              l_weight.append(op.get_current_weight)      
          elif a==2:
              sc = input('please enter the scale condition:')
              sl = input('please enter the scale length:')
              op =fish(name,dob,bw,name,address,sc,sl)
              print(op)
              l_weight.append(op.get_current_weight)
          elif a==3:
              iv = input('is venomous(yes or no):')
              op =amphibians(name,dob,bw,name,address,iv)
              print(op)
              l_weight.append(op.get_current_weight)
          else:
              print(' input wrong vaule,please choose a number from 1,2 or 3')
      elif a==2:  
          for i in l_weight:
              print(i)

推荐阅读