python - 使用 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()
解决方案
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)
推荐阅读
- php - 动态我的个人资料页面 php 不断给出错误
- python - 将现有地块合并为一个图形
- kiwi-tcms - 在 KiwiTCMS 中设置时区
- c# - 禁用 EF Core“QuerySplittingBehavior.SingleQuery”警告
- laravel - 获取针对外键的最新记录并检查其特定列的值是否为 NULL
- amazon-web-services - AWS lambda:如何在 lambda 中运行 aws cli 命令
- java - 如何使用 Jackson 启用 JSR310 对 LocalDate 的支持?
- r - DT::DataTables 中第一列的黑色背景和白色字体
- html - 更改日期时如何显示总和的一行?
- azure-storage - 使用数据工厂根据 Datetimestamp 列从存储帐户中选择文件