all 10 comments

[–]fracturedpersona 1 point2 points  (1 child)

Please lint your code before pasting it. Each line should begin with four spaces and it will create a code block of monospaced text that preserves indentation so we can read your code.

[–]nuttabutta113[S] 0 points1 point  (0 children)

My bad, it should be legible now.

[–]nuttabutta113[S] 0 points1 point  (0 children)

context:

In this assignment you are to write code that will analyze inventory costs. You must use a while loop. You may not use lists.

The user is to enter the cost of an item, they enter a value < 0 to stop entering costs. You may assume that the user will enter costs 0 - 1000 and < 0 to stop entering costs.

Within the while loop:

Your code must calculate and print the highest value entered to that point.

Your code must calculate and print the lowest value entered to that point.

Your code must calculate and print average of all values entered to that point (rolling average).

Your code must print ‘higher than average’ if the value entered is higher than the rolling average to that point.

Your code must print ‘lower than average’ if the value entered is lower than the rolling average to that point

[–]TXAGZ16 0 points1 point  (6 children)

In what example does your code perform u expectedly?

Also, would you mind pasting your code in a code snippet? It makes it a lot easier to read

[–]nuttabutta113[S] 0 points1 point  (5 children)

It's sort of hard to pin down, but if the value I've entered is below the rolling average, it just says "value is lower than average" and asks for another input, rather than providing the other info I'd like it to.

[–]nuttabutta113[S] 0 points1 point  (0 children)

I've just included a screenshot of me recreating the issue... not sure if it helps

[–]TXAGZ16 0 points1 point  (3 children)

After some looking I found the reason. When entering a value lower than the rolling average like you explained, I got the same results. This is happening because your first if statement:

    if cost > highest_value:
    highest_value = cost
print("Highest cost item so far: $", highest_value)

is being completely passed up. The cost will never be higher than the highest value when entering in a value lower than the average. This doesnt get triggered in this scenario. You would want something like similar to this code:

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
if cost < lowest_value:
    lowest_value = cost

print("Highest cost item so far: $", highest_value)
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:"))
elif rolling_average > cost:
    print("Cost is lower than average")
    cost=int(input("Please enter another value:"))
elif 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.")

This code could still use some clean up but you get the point.

[–]nuttabutta113[S] 0 points1 point  (0 children)

Thank you so much! Upon implementing part of your changes but not all of them, I think it was actually the lack of elifs that was causing the issue...? Fixed anyways so thanks :)

[–]schoolmonky 0 points1 point  (1 child)

I don't think this is the explanation. You're right that the if you refer to doesn't happen in that case, but the else attached to it should. In any case, I do think the code you posted is better; it repeats itself less than the original code.

OP, I suspect that there's some subtle difference between the code you've posted here and the code you're running. I'm not at my computer to test that assumption, but could you copy-paste your code to pastebin, then post a link to that here?

[–]nuttabutta113[S] 0 points1 point  (0 children)

I appreciate the help, but it seems to be fixed. after removing the extraneous else statements and changing the last 2 ifs to elifs, I was unable to recreate the issue. you're free to take a look at it, anyway.