def Data():
while True:
try:
PetTotal = int(input("Please enter the number of pets: "))
except ValueError:
print("Sorry, I didn't understand that.")
continue
else:
PetBreeds = []
Ages = []
while True:
try:
Gender = int(input("Enter gender option: "))
except ValueError:
print("Sorry, I didn't understand that.")
continue
if Gender > 2 or Gender < 1:
print("Wrong!")
continue
else:
Count = 0
while Count != PetTotal:
Breed = str(input("Breed: "))
Age = float(input("Age: "))
if Gender == 1:
PetBreeds.append(Breed)
Ages.append(Age)
Count = Count + 1
else:
PetBreeds.append(Breed)
Ages.append(Age)
Count = Count + 1
PetInfo = dict(zip({PetBreeds, Ages))
return PetBreeds, Gender;
我在代码中添加了一些输入验证,因为我正在努力理解它。但是,我的代码不再正常工作。当PetTotal
高于2时,它只循环一次,因此只能输入一种宠物类型(而不是两种)。我确定答案很明显,但我无法弄清楚......任何帮助都非常受欢迎!
答案 0 :(得分:1)
无论在try case中输入的值如何,else部分只执行一次,因此尝试应用for循环以便else部分执行n次
for i in range(0,PetTotal):
#else part here
答案 1 :(得分:1)
方法select relatives_count(id) from category
中的最后一行 - 这将导致最外面的循环(控制要处理多少宠物的循环)退出。
我认为您希望代码与
类似return