1

I was wondering if anyone could help point me in the right direction! I'm a beginner and I'm totally lost. I'm trying to make a Sentinel controlled loop that asks the user to "enter the amount of check" and then ask "how many patrons for this check". After it asks the user then enters it until they type -1.

once user is done inputting it is suppose to calculate the total,tip,tax of each check with an 18% tip for anything under 8 patrons and 20%tip for anything over 9 and a tax rate of 8%.

and then it should add up the Grand totals. ex: check 1 = 100$ check 2 = 300 check 3 = 20 Total checks = $420 I'm not asking for someone to do it for me but just if you could point me in the right direction, this is all i have so far and im stuck.

As of right now the code is horrible and doesn't really work. I completed it in Raptor and it worked perfectly I just don't know how to convert it to python

sum1 = 0
sum2 = 0
sum3 = 0
sum4 = 0
sum5 = 0
check = 0
print ("Enter -1 when you are done")



check = int(input('Enter the amount of the check:'))
while check !=(-1):
    patron = int(input('Enter the amount of patrons for this check.'))
    check = int(input('Enter the amount of the check:'))

tip = 0
tax = 0


if patron <= 8:
    tip = (check * .18)
elif patron >= 9:
    tip = (check * .20)

total = check + tax + tip
sum1 = sum1 + check
sum2 = sum2 + tip
sum3 = sum3 + patron
sum4 = sum4 + tax
sum5 = sum5 + total

print ("Grand totals:")
print ("Total input check = $" + str(sum1))
print ("Total number of patrons = " + str(sum3))
print ("Total Tip = $" +str(sum2))
print ("Total Tax = $" +str(sum4))
print ("Total Bill = $" +str(sum5))
Vinn
  • 11
  • 1
  • 2
  • possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – g.d.d.c Jul 07 '14 at 21:39

2 Answers2

2

Your code runs fine, but you have some logic problems.

It appears you're planning to deal with multiple checks at the same time. You'll probably want to use a list for that, and append checks and patrons to it until check is -1 (and don't append the last set of values!).

I think the real issue you're having is that to leave the loop, check must be equal to -1.

If you follow that a bit further down, you continue to work with check, which we now know is -1, regardless of what happened previously in the loop (check is overwritten every time).

When you get to these lines, then you have a real problem:

if patron <= 8:
    tip = (check * .18)
elif patron >= 9:
    tip = (check * .20)

# This is the same, we know check == -1

if patron <= 8:
    tip = (-1 * .18)
elif patron >= 9:
    tip = (-1 * .20)

At this point you probably won't be able to do anything interesting with your program.

EDIT: A bit more help

Here's an example of what I'm talking about with appending to a list:

checks = []
while True:
    patron = int(input('Enter the amount of patrons for this check.'))
    check = int(input('Enter the amount of the check:'))
    # here's our sentinal
    if check == -1:
        break
    checks.append((patron, check))
print(checks)
# do something interesting with checks...

EDIT: Dealing with cents

Right now you're parsing input as int's. That's OK, except that an input of "3.10" will be truncated to 3. Probably not what you want.

Float's could be a solution, but can bring in other problems. I'd suggest dealing with cents internally. You might assume the input string is in $ (or € or whatever). To get cents, just multiply by 100 ($3.00 == 300¢). Then internally you can continue to work with ints.

Jon Surrell
  • 9,444
  • 8
  • 48
  • 54
  • have you tried converting input from 3.55 to an int? – Padraic Cunningham Jul 07 '14 at 22:06
  • Thank you! sorry for the late reponse ever since i got this i seen your post ive been trying to figure it out :c. – Vinn Jul 07 '14 at 22:06
  • is having the if patron <=8: a bad idea? Im trying to make it check to see if the patrons is less then 8 the tip would be 18% But i cant even get that far honestly. It keeps outputting my sentinel in the Total fields at the end – Vinn Jul 07 '14 at 22:08
  • No, I don't think anything it inherently bad in checking the number of patrons. It's pretty tricky to work on your code in these comments. You really need to figure out how to work with multiple pieces of data, you could follow my example and build a list and then loop over that list with for to find your totals. Just keep working at it… – Jon Surrell Jul 07 '14 at 22:17
0

This program should get you started. If you need help, definitely use the comments below the answer.

def main():
    amounts, patrons = [], []
    print('Enter a negative number when you are done.')
    while True:
        amount = get_int('Enter the amount of the check: ')
        if amount < 0:
            break
        amounts.append(amount)
        patrons.append(get_int('Enter the number of patrons: '))
    tips, taxs = [], []
    for count, (amount, patron) in enumerate(zip(amounts, patrons), 1):
        tips.append(amount * (.20 if patron > 8 else .18))
        taxs.append(amount * .08)
        print('Event', count)
        print('=' * 40)
        print('  Amount:', amount)
        print('  Patron:', patron)
        print('  Tip:   ', tips[-1])
        print('  Tax:   ', taxs[-1])
        print()
    print('Grand Totals:')
    print('  Total amount:', sum(amounts))
    print('  Total patron:', sum(patrons))
    print('  Total tip:   ', sum(tips))
    print('  Total tax:   ', sum(taxs))
    print('  Total bill:  ', sum(amounts + tips + taxs))

def get_int(prompt):
    while True:
        try:
            return int(input(prompt))
        except (ValueError, EOFError):
            print('Please enter a number.')

if __name__ == '__main__':
    main()
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117