如何从for循环中计算总薪水

时间:2018-09-13 19:50:42

标签: python

我想计算出用户输入一定天数后for循环运行的便士总数,给出每次迭代后的“便士”数量。我想对所有右侧的列进行汇总,以使其总计为$ xx.xx格式。

 numberOfDaysWorked = int(input ("Enter the amount of days worked: \n")) #Asking the user to enter the amount of days they have worked
 salary = 0.01 #setting the inital amount of pennies 
 total = 0.00
 print( "Day\tSalary\n---\t----" ) #printing out the titles for days and salary
 for currentDay in range(numberOfDaysWorked): 
      salary += 2 * salary # this is adding the amount of pennies earned that day to the total amount of pennies that will be used toward the total
      print (currentDay + 1, "\t", salary) #printing out the day (+1 to ensure progression from day1 to day2 to day3 etc. and then printing out the amount of pennies earned on that one day)


     print ("Total Pay: $", totalPay) 

4 个答案:

答案 0 :(得分:1)

for循环的总和只是for循环完成后的变量#!/bin/bash /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" # wait until that is done, eventually check if it succeeded brew install git brew install composer 。不过请检查一下数学。那2似乎关闭了。

编辑:

根据您的评论,只需在for循环中使用变量salary

totalPay

请确保下次更好地解释您的问题,因此在这里会有很多不同的结果!

答案 1 :(得分:0)

您的问题非常令人困惑。您是说每天的日薪增加三倍吗?这段代码会提供您所需的信息吗?

 numberOfDaysWorked = int(input ("Enter the amount of days worked: \n")) #Asking the user to enter the amount of days they have worked

salary = 0.01  # setting the inital amount of pennies
total = 0.00
print("Day\tSalary\n---\t----")  # printing out the titles for days and salary
for currentDay in range(numberOfDaysWorked):
    salary += 2 * salary
    total += salary  # this is adding the amount of pennies earned that day to the total amount of pennies that will be used toward the total
    print(currentDay + 1, "\t",
          salary)  # printing out the day (+1 to ensure progression from day1 to day2 to day3 etc. and then printing out the amount of pennies earned on that one day)

print("Total Pay: $", total)

输入为10的输出:

Day Salary
--- ----
1    0.03
2    0.09
3    0.27
4    0.81
5    2.43
6    7.290000000000001
7    21.870000000000005
8    65.61000000000001
9    196.83000000000004
10   590.4900000000001
Total Pay: $ 885.7200000000003

答案 2 :(得分:0)

您有totalsalary可用。在循环中将salary添加到total以获得“总”工资:

    ...
    print (currentDay + 1, "\t", salary)
    ## add these lines ...
    print ("Total Pay: $", salary)
    total += salary

print ("Total salary: $%.2f" % total)

演示:

Enter the amount of days worked: 
4
Day Salary
--- ----
1    0.03
Total Pay: $ 0.03
2    0.09
Total Pay: $ 0.09
3    0.27
Total Pay: $ 0.27
4    0.81
Total Pay: $ 0.81
Total salary: $1.20   # equals 0.03 + 0.09 + 0.27 + 0.81

答案 3 :(得分:0)

我认为您的问题是您在循环期间从未向“总计”变量中添加任何内容。

numberOfDaysWorked = int(input ("Enter the amount of days worked: \n"))
salary = 0.01 #setting the inital amount of pennies 
total = 0.00
print( "Day\tSalary\n---\t----" ) 
for currentDay in range(numberOfDaysWorked):
    salary += 2 * salary
    total += salary
    print (currentDay + 1, "\t", salary)
print ("Total Pay: $"+str(total)) 

输出:

Enter the amount of days worked: 
5
Day Salary
--- ----
1    0.03
2    0.09
3    0.27
4    0.81
5    2.43
Total Pay: $3.6300000000000003