如何修复此程序中的while循环?

时间:2017-03-30 03:04:39

标签: python

编写一个包含3个列表的python程序:

姓名工资 小时 胡安 7.50 35 Rae 11.00 41 Ivanna 18.25 26 莉莉 9.25 35 罗伯特 11.10 45

并使用此逻辑:

员工工作超过40小时后加班

加班费使用以下公式计算:

总薪酬=(35 *工资)+((小时-35)*工资* 1.5)

使用以下公式计算定期工资:

总薪酬=(小时*工资)

使用循环处理这些列表。

打印每位员工,他们的工资,工时和总工资。

我正在运行这个程序,我有for循环。输入工作正常,但它应该具有相同输出的while循环根本不给我任何输出。这是我的代码。

`Name = ["Juan","Rae","Ivanna", "Lilly", "Robert"]
Hours = [35,41,26,35,45]
Wage = [7.5,11,18.25,9.25,11.1]
print ("Name\tWage\tHours\tGP")
for X in range(5):
   GP = 0
   if(Hours[X] > 40):
       GP = (35*Wage[X]) + ((Hours[X]-35)*Wage[X]*1.5)
   else:
       GP = Hours[X] * Wage[X]
   print (Name[X],"\t", Wage[X],"\t", Hours[X],"\t", GP)

Name = ["Juan","Rae","Ivanna", "Lilly", "Robert"]
Hours = [35,41,26,35,45]
Wage = [7.5,11,18.25,9.25,11.1]
print ("Name\tWage\tHours\tGP") 
counter = 5
Y = 0
while (Y):
    if (Hours[Y] > 40):
        GP = (35*Wage[Y]) + ((Hours[Y]-35)*Wage[Y]*1.5)
    else:
        GP = Hours[Y] * Wage[Y]
print (Name[Y],"\t", Wage[Y],"\t", Hours[Y],"\t", GP)`

我的输出是

Name    Wage    Hours   GP
Juan     7.5     35      262.5
Rae      11      41      484.0
Ivanna   18.25   26      474.5
Lilly    9.25    35      323.75
Robert   11.1    45      555.0
Name    Wage    Hours   GP
Juan     7.5     35      555.0

我不知道while循环中的错误在哪里。

2 个答案:

答案 0 :(得分:0)

你的最后一行需要缩进。而不是

while (Y):
    if (Hours[Y] > 40):
        GP = (35*Wage[Y]) + ((Hours[Y]-35)*Wage[Y]*1.5)
    else:
        GP = Hours[Y] * Wage[Y]
print (Name[Y],"\t", Wage[Y],"\t", Hours[Y],"\t", GP)

它需要

while (Y):
    if (Hours[Y] > 40):
        GP = (35*Wage[Y]) + ((Hours[Y]-35)*Wage[Y]*1.5)
    else:
        GP = Hours[Y] * Wage[Y]
    print (Name[Y],"\t", Wage[Y],"\t", Hours[Y],"\t", GP)

因为现在print位于while循环之外,因此只使用值Y=0进行打印。

答案 1 :(得分:0)

必须遵循python code indented并且需要使用(递增/递减/布尔值)给出正确的while loop condition。因为您的代码需要增加Y的值。

Name = ["Juan","Rae","Ivanna", "Lilly", "Robert"]
Hours = [35,41,26,35,45]
Wage = [7.5,11,18.25,9.25,11.1]
print ("Name\tWage\tHours\tGP")
for X in range(5):
    GP = 0
    if(Hours[X] > 40):
        GP = (35*Wage[X]) + ((Hours[X]-35)*Wage[X]*1.5)
    else:
        GP = Hours[X] * Wage[X]
    print (Name[X],"\t", Wage[X],"\t", Hours[X],"\t", GP)

Name = ["Juan","Rae","Ivanna", "Lilly", "Robert"]
Hours = [35,41,26,35,45]
Wage = [7.5,11,18.25,9.25,11.1]
print ("Name\tWage\tHours\tGP") 
counter = 5
Y = 0
while (Y<counter):
    if (Hours[Y] > 40):
        GP = (35*Wage[Y]) + ((Hours[Y]-35)*Wage[Y]*1.5)
    else:
        GP = Hours[Y] * Wage[Y]
    print (Name[Y],"\t", Wage[Y],"\t", Hours[Y],"\t", GP)
    Y=Y+1