我的代码有问题。我不知道怎么修。我的程序反复提示要访问的超市过道号。输入0时,输入过程将终止。然后,程序根据放置购物篮的最佳位置和我需要从中挑选物品的过道数量计算最小距离。然后程序显示最小距离。以下是我的代码:
totalAisles = int(input("Enter the number of aisles in supermarket: "))
visitAisle = int(input("Enter the aisle number to visit: "))
if visitAisle == 0:
print("Not visiting any aisle")
else:
aisle = []
while True:
visitAisle = int(input("Enter the aisle number to visit: "))
if visitAisle == 0:
break
else:
for i in range(totalAisles):
visitAisle = int(input("Enter the aisle number to visit: "))
aisle.append(visitAisle)
#to find the best aisle to place the basket
highestAisle = max(aisle)
lowestAisle = min(aisle)
basket = round(highestAisle + lowestAisle) / 2
minimumDistance += abs(basket - visitAisle) * 2
print(f"The minimum distance is {minimumDistance} units")
示例运行
运行1 输入超市中的过道数:100
输入要访问的通道号:1
输入要访问的通道号:8
输入要访问的通道号:5
输入要访问的通道号:4
输入要访问的通道号:10
输入要访问的通道号:0
最小距离为26个单位
运行2
输入超市中的过道数:100
输入要访问的通道号:1
输入要访问的通道号:0
最小距离为0个单位
运行3
输入超市中的过道数:100
输入要访问的通道号:0
不去任何走道
这是我建议你的解决办法
我修改了几行程序,但只修改了一点。现在我希望你能理解问题的原因
更新了以下是一种更简化的方法。我还确保用户不会输入重复的过道编号,并且过道数量不会超过商店中的过道数量。最重要的是,计算步骤大大简化
虽然我不确定输出是否正是您想要的,但这是一个替代方案
相关问题 更多 >
编程相关推荐