Home > OS >  How can i sum totals from user input in a while loop?
How can i sum totals from user input in a while loop?

Time:09-28

I'm extremely new to learning coding in general (weeks!) and am still learning Python terminology and whatnot, so I've been having trouble searching for answers because I don't know how to word the question. I started very small with basic calculations. I am trying to write a program that will figure out how much your gross pay will be based on how many jobs you have, how many hours worked, and your hourly wage. I used a while loop to have the user input how many jobs, hours worked, rate, and it gives a total for each job. I want it to also give a sum of all their totals for a grand total, but am not sure how to do it.
Code below:

#how much money did user make this week

print('Hello user, do you want to know how much money you made this week?')
run = input('y or n: ')

while run =='y' or run == 'Y':
    print("First let's find out how many jobs you worked this week: ")
    jobs = int(input('No. of jobs worked: '))
    for Num_Jobs in range(jobs):
        print('How many hours did you work at job ', Num_Jobs 1,'?')
        hours = int(input(': '))
        rate = float(input("What is your hourly rate? $ "))
        total = hours*rate
        print("For job ", Num_Jobs 1," you will get paid: $", format(total, 
              ',.2f'), sep='')
        print()

    print('Do you have more jobs you want to calculate?')
    run = input('y or n: ')
print('ok, have a good day!')

CodePudding user response:

Just add a grand total out the scope of for loop. And add after each iteration.

#how much money did user make this week

print('Hello user, do you want to know how much money you made this week?')
run = input('y or n: ')

grand_total = 0

while run =='y' or run == 'Y':
    print("First let's find out how many jobs you worked this week: ")
    jobs = int(input('No. of jobs worked: '))
    for Num_Jobs in range(jobs):
        print('How many hours did you work at job ', Num_Jobs 1,'?')
        hours = int(input(': '))
        rate = float(input("What is your hourly rate? $ "))
        total = hours*rate
        grand_total  = total
        print("For job ", Num_Jobs 1," you will get paid: $", format(total, 
              ',.2f'), sep='')
    print("Grand Total is ", grand_total)

    print('Do you have more jobs you want to calculate?')
    run = input('y or n: ')
print('ok, have a good day!')

CodePudding user response:

#how much money did user make this week

print('Hello user, do you want to know how much money you made this week?')
run = input('y or n: ')

grand_total = 0

while run =='y' or run == 'Y':
    print("First let's find out how many jobs you worked this week: ")
    jobs = int(input('No. of jobs worked: '))
    for Num_Jobs in range(jobs):
        print('How many hours did you work at job ', Num_Jobs 1,'?')
        hours = int(input(': '))
        rate = float(input("What is your hourly rate? $ "))
        total = hours*rate
        print("For job ", Num_Jobs 1," you will get paid: $", format(total, 
              ',.2f'), sep='')
        print()
        grand_total = total

    print('Do you have more jobs you want to calculate?')
    run = input('y or n: ')
print('Your grand total is %d',grand_total)
print('ok, have a good day!')
  • Related