number = int(input("Enter the number of parcels you want to input: "))
width = int(input("Enter the width (cm)of the parcel: "))
length = int(input("Enter the length (cm)of the parcel: "))
height = int(input("Enter the height (cm)of the parcel: "))
weight = int(input("Enter the weight (kg) of the parcel: "))
width_list = []
length_list = []
height_list = []
weight_list = []
parcel_number = []
parcel_number.append(number)
if width > 80 or width < 1:
print("Only widths between 1-80 cm is acceptable")
elif height > 80 or height < 1:
print("Only heights between 1-80 cm is acceptable")
elif length > 80 or length < 1:
print("Only lengths between 1-80 cm is acceptable")
elif weight > 10 or weight < 1:
print("Only allowed weights between 1-10 kg")
else:
print("Parcel is acceptable")
首先我要求用户添加包裹的测量值。然后我需要检查每个包裹是否在1-80厘米的正确尺寸和1-10千克之间的重量之间,然后如果用户输入超出限制的测量值,则需要打印错误。如果包裹是可以接受的,我应该将每个测量值附加到下面的列表中,但我不知道如何使用parcel_number的接受。如果可能的话,你可以帮我把代码放在一个循环中,所以我运行它的次数附加到parcel_number列表?谢谢!
答案 0 :(得分:0)
这就是你想要的:
c=0
number = int(input("Enter the number of parcels you want to input: "))
width_list = []
length_list = []
height_list = []
weight_list = []
parcel_number = []
parcel_number.append(number)
while c < number:
width = int(input("Enter the width (cm)of the parcel: "))
length = int(input("Enter the length (cm)of the parcel: "))
height = int(input("Enter the height (cm)of the parcel: "))
weight = int(input("Enter the weight (kg) of the parcel: "))
if width > 80 or width < 1:
print("Only widths between 1-80 cm is acceptable")
elif height > 80 or height < 1:
print("Only heights between 1-80 cm is acceptable")
elif length > 80 or length < 1:
print("Only lengths between 1-80 cm is acceptable")
elif weight > 10 or weight < 1:
print("Only allowed weights between 1-10 kg")
else:
width_list.append(width)
length_list.append(length)
height_list.append(height)
weight_list.append(weight)
print("Parcel is acceptable")
c += 1
print(width_list)
print(length_list)
print(height_list)
print(weight_list)
输出:
Enter the number of parcels you want to input: 2
Enter the width (cm)of the parcel: 33
Enter the length (cm)of the parcel: 55
Enter the height (cm)of the parcel: 33
Enter the weight (kg) of the parcel: 2
Parcel is acceptable
Enter the width (cm)of the parcel: 45
Enter the length (cm)of the parcel: 34
Enter the height (cm)of the parcel: 23
Enter the weight (kg) of the parcel: 3
Parcel is acceptable
[33, 45]
[55, 34]
[33, 23]
[2, 3]
我只是简单地使用python list append
函数然后创建一个名为c的变量并将其赋值为0,然后创建一个while循环来检查c
是否小于{{1}如果它是代码运行并且在代码的末尾我将1添加到c然后它检查number
是否仍然小于数字,如果它不是它不再运行代码