Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用if语句将对象实例附加到列表中,并在另一个分支中打印它_Python_List_Variables - Fatal编程技术网

Python 使用if语句将对象实例附加到列表中,并在另一个分支中打印它

Python 使用if语句将对象实例附加到列表中,并在另一个分支中打印它,python,list,variables,Python,List,Variables,我创建了一些关于pet的类,下面的代码是我的main()函数的一部分,首先,让用户选择一件他们想做的事情。也就是说,如果使用输入“1”,他们将添加一些宠物实例。同时,我想将pet实例的部分信息附加到列表中。然后,如果用户选择阅读此信息。我想在另一个if语句分支中打印它。此时,用户输入“2”。当我在已经生成一些pet实例之后输入2时,就会出现问题。名为l_weight的列表始终无效。我怎样才能修好它?我已经尝试使用全局列表,但无法使用 def main(): l_weight=[] print(&

我创建了一些关于pet的类,下面的代码是我的main()函数的一部分,首先,让用户选择一件他们想做的事情。也就是说,如果使用输入“1”,他们将添加一些宠物实例。同时,我想将pet实例的部分信息附加到列表中。然后,如果用户选择阅读此信息。我想在另一个if语句分支中打印它。此时,用户输入“2”。当我在已经生成一些pet实例之后输入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()
l_-weight()没有追加的原因是,在代码中,您将列表命名为
l_-weight
,然后在代码的其余部分将其写为
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')
      elif a==2:  
          for i in l_weight:
              print(i)

谢谢,我改正了。但它仍然不起作用。我认为每次用户输入后,l_权重列表都会清晰可见。我想重复运行main函数,直到用户想要退出系统,所以在每个if语句之后,它都会返回main(),没问题!将菜单部分包括到while循环中,您可以这样做。当然,您必须创建条件来检查用户是否在4中输入以退出。现在您只需检查菜单选项1和2