0

I can not figure out why this sentinel will not work. I have searched all over for an answer to no avail. Thank you for your help!

gas = 0
miles = 0
miles = int(input("Enter number of miles driven(enter -11 to exit): "))
while miles != -11:
    gas = int(input("Enter the number of gallons of gas used: "))
    print ("The number of miles entered was:", miles)
    print ("The number of gallons of gas used was:", gas)
    mpg = miles/gas
    print ("The MPG was:", mpg)

This is the result:

Enter number of miles driven(enter -11 to exit): 100
Enter the number of gallons of gas used: 10
The number of miles entered was: 100
The number of gallons of gas used was: 10
The MPG was: 10.0
Enter the number of gallons of gas used: -11
The number of miles entered was: 100
The number of gallons of gas used was: -11
The MPG was: -9.090909090909092
Enter the number of gallons of gas used:
4
  • Which error are you getting? Commented Mar 14, 2014 at 15:35
  • 4
    You never update the value of miles in the loop body. Commented Mar 14, 2014 at 15:35
  • define 'not working' please. Commented Mar 14, 2014 at 15:38
  • Here is what it is doing : Enter number of miles driven(enter -11 to exit): 100 Enter the number of gallons of gas used: 10 The number of miles entered was: 100 The number of gallons of gas used was: 10 The MPG was: 10.0 Enter the number of gallons of gas used: -11 The number of miles entered was: 100 The number of gallons of gas used was: -11 The MPG was: -9.090909090909092 Enter the number of gallons of gas used: Commented Mar 14, 2014 at 15:39

2 Answers 2

1

Use a blank line for the "sentinel" instead... I'd also re-write it using the two form version of iter as such:

for miles in iter(lambda: input('Enter miles (or enter to exit): '), ''):
    try:
        miles = int(miles)
        gas = int(input('Enter gas used: '))
        print('MPG was: ', miles/gas)
    except (ValueError, ZeroDivisionError): # int type conversion failed or div by 0
        print('You must enter valid numbers and gas used must be > 0 or press enter to exit')
Sign up to request clarification or add additional context in comments.

Comments

1

You should also include the number of miles in the cycle.

gas = 0
miles = 0
while miles != -11:
    miles = int(input("Enter number of miles driven(enter -11 to exit): "))
    gas = int(input("Enter the number of gallons of gas used: "))
    print ("The number of miles entered was:", miles)
    print ("The number of gallons of gas used was:", gas)
    mpg = miles/gas
    print ("The MPG was:", mpg)

Even in this case, the program will ask for the amount of gas before it checks for the sentinel condition, so a better version would be:

gas = 0
miles = 0
while True:
    miles = int(input("Enter number of miles driven(enter -11 to exit): "))
    if miles==-11:
        break
    gas = int(input("Enter the number of gallons of gas used: "))
    print ("The number of miles entered was:", miles)
    print ("The number of gallons of gas used was:", gas)
    mpg = miles/gas
    print ("The MPG was:", mpg)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.