继续提示用户输入,直到输入0

2024-09-29 23:18:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码有问题。我不知道怎么修。我的程序反复提示要访问的超市过道号。输入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

不去任何走道


Tags: theto代码in程序距离numberinput
3条回答

这是我建议你的解决办法

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: "))
                if visitAisle == 0:
                    break
                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
        if visitAisle == 0:
                    break
    print("The minimum distance is ", minimumDistance)

我修改了几行程序,但只修改了一点。现在我希望你能理解问题的原因

更新了以下是一种更简化的方法。我还确保用户不会输入重复的过道编号,并且过道数量不会超过商店中的过道数量。最重要的是,计算步骤大大简化

totalAisles = int(input("Enter the number of aisles in supermarket: "))
aisle = []
visitAisle = None

while len(aisle) < totalAisles:
    visitAisle = int(input("Enter the aisle number to visit: "))
    if visitAisle == 0:
        if aisle:
            break
        else:
            print("Not visiting any aisle")
            break
    if visitAisle not in aisle and visitAisle != 0:
        aisle.append(visitAisle)
    else:
        print("You already entered that aisle number. Pick another one.")

#to find the best aisle to place the basket
if aisle:
    highestAisle = max(aisle)
    lowestAisle = min(aisle)
    basket = (highestAisle + lowestAisle) //2

    #traveling back and forth from the basket to each isle
    minimumDistance = 0
    for visitAisle in aisle:
        minimumDistance += abs(basket - visitAisle) * 2
    print(f"\nThe minimum distance is {minimumDistance} units if you park the cart at aisle {basket}")

虽然我不确定输出是否正是您想要的,但这是一个替代方案

totalAisles = int(input("Enter the number of aisles in supermarket: "))
enterStore = (input("Do you want to enter the store (yes/no)?: "))
if enterStore == "no":
    print("Not visiting the supermarket today!")
minimumDistance = 0
aisle = []
while enterStore != "no":
    visitAisle = int(input("Enter the aisle number to visit: "))
    if visitAisle == 0:
        break
    else:
        aisle.append(visitAisle)
        highestAisle = max(aisle)
        lowestAisle = min(aisle)
        basket = int(round(highestAisle + lowestAisle) / 2)
        minimumDistance += abs(basket - visitAisle) * 2

print(f"The minimum distance is {minimumDistance} units, ideal place for basket is {basket}")

相关问题 更多 >

    热门问题