【问题标题】:how can i append these entries to a list?如何将这些条目附加到列表中?
【发布时间】:2018-11-04 07:38:06
【问题描述】:
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 列表的次数吗?谢谢!

【问题讨论】:

  • 您应该查看有关 python for-loops 的文档! :)

标签: python list loops for-loop while-loop


【解决方案1】:

这是你想要的吗:

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 列表 append 函数,然后创建一个名为 c 的变量并将其分配给 0,然后创建一个 while 循环来检查 c 是否小于 number 是否是代码运行并在代码的末尾我将 1 添加到 c 然后它检查 c 是否仍然小于数字,如果不是,它不会再次运行代码

【讨论】:

    猜你喜欢
    • 2019-08-25
    • 2013-12-24
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多