i'm having problem with my code. Not sure how to fix it. My program repeatedly prompts for the supermarket aisle numbers to visit. When 0 is entered, the input process will terminate. Then the program computes the minimum distance based on the best location to place the shopping basket and the number of aisles I need to pick items from. The program then displays the minimum distance. The following are my code:
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")
Example runs
Run 1 Enter number of aisles in supermarket: 100
Enter the aisle number to visit: 1
Enter the aisle number to visit: 8
Enter the aisle number to visit: 5
Enter the aisle number to visit: 4
Enter the aisle number to visit: 10
Enter the aisle number to visit: 0
The minimum distance is 26 units
Run 2
Enter number of aisles in supermarket: 100
Enter the aisle number to visit: 1
Enter the aisle number to visit: 0
The minimum distance is 0 units
Run 3
Enter number of aisles in supermarket: 100
Enter the aisle number to visit: 0
Not visiting any aisle
Here is the workaround which I suggest you
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)
I have modified few lines of the program, but only just a bit. Now I hope you can understand what was the reason for the issue.
While I'm not sure the output is exactly what you are looking for, this is an alternative.
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}")
UPDATED Here is a more streamlined way of doing it. I also made sure the user doesn't enter duplicate aisle numbers and not more aisles than there are in the store. Most importantly, the computation step is much simplified.
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}")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.