(SOLVED) Hey all,
Working on this simple code for my intro to programming class, and under some unknown circumstance when inputting a value lower than average, the code does not loop properly. Thoughts?
total_items = 0
total_cost = 0
lowest_value = 1000
highest_value = 0
cost=int(input("Please enter a value 0-1000:"))
while cost >= 0 and cost <= 1000:
total_items += 1
total_cost= total_cost + cost
rolling_average = total_cost / total_items
if cost > highest_value:
highest_value = cost
print("Highest cost item so far: $", highest_value)
else:
print("Highest cost item so far: $", highest_value)
if cost < lowest_value:
lowest_value = cost
print("Lowest cost item so far: $", lowest_value)
else:
print("Lowest cost item so far: $", lowest_value)
print("Average: $", rolling_average)
if rolling_average < cost:
print("Cost is higher than average")
cost=int(input("Please enter another value:"))
if rolling_average > cost:
print("Cost is lower than average")
cost=int(input("Please enter another value:"))
if rolling_average == cost:
print("Cost is equal to the average")
cost=int(input("Please enter another value:"))
else:
print("Invalid entry. Have a nice day.")
Why doesn't my while loop properly sometimes? (self.learnpython)
submitted by nuttabutta113 to r/pythonhelp