Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Meghan Garrity
Meghan Garrity
2,751 Points

I need to write a program that uses a while loop and a sentinel value of zero and need help writing it.

The program needs to execute as a store cashier and determine the total of a number of purchases. The number of items being purchased can vary, and this should not be initially set by user input. Instead, use a while loop and a sentinel value of zero to indicate that no more items are to be summed up and the total is to be displayed. Display the total in currency format with the $ sign right up against the first digit.

5 Answers

My apologies. Thanks for letting me know. It looked alright, but I should have tested it. There were two problems: an indentation error, and I'd forgotten that raw_input() has been renamed input(). So here's one that works:

total = 0
gradeCounter = 0

grade = input( "Enter grade, -1 to end: " )   # get one grade
grade = int( grade )                              # convert string to an integer

while grade != -1:
  total = total + grade
  gradeCounter = gradeCounter + 1
  grade = input( "Enter grade, -1 to end: " )
  grade = int( grade )

if gradeCounter != 0:
  average = float( total ) / gradeCounter
  print ( "Class average is", average )
else:
  print ( "No grades were entered" )

Or at least it did in a Treehouse Python workspace!

Here's an example from the Internet. Should be fairly straightforward to modify for your needs:

total = 0          
gradeCounter = 0   

grade = raw_input( "Enter grade, -1 to end: " )   # get one grade
grade = int( grade )                              # convert string to an integer

while grade != -1:
   total = total + grade
   gradeCounter = gradeCounter + 1
   grade = raw_input( "Enter grade, -1 to end: " )
   grade = int( grade )

if gradeCounter != 0:
   average = float( total ) / gradeCounter
   print "Class average is", average
else:
   print "No grades were entered"
Meghan Garrity
Meghan Garrity
2,751 Points

Thanks for your input, but the code doesn't work. Keeps saying raw_input is not defined.

Meghan Garrity
Meghan Garrity
2,751 Points

I will check it it python to see if it works.

Meghan Garrity
Meghan Garrity
2,751 Points

The code works now. Now I'm trying to fit the code for the problem. Thanks for your help.