Python程序不会在第一次用户输入之外执行 [英] Python program does not execute beyond first user input
问题描述
我编写了一个Python程序,我希望接受来自用户的y输入,如果用户输入y,则进行一些简单的计算并打印结果。
I have written a Python program that I expect to accept a "y" input from a user, and if the user inputs "y", do some simple calculations and print the results.
然而,即使输入是y,它也不会执行超过
的用户输入。请帮我找到程序中的错误。
However it is not executing beyond the user input even if that input is "y". Please help me locate the errors in the program.
代码是:
The code is:
#This function is to get the user to input the data needed to use in the rest of the program
#It should return the 3 variables name, hrs_wrkd, and payrate
def get_info(name,hrs_wrkd,payrate):
print('is this working?');
name = input('What is the last name of the employee?');
hrs_wrkd = float(input('How many hours did',name,' work last week?'));
payrate = float(input('How much does',name,' get paid?'));
return name,hrs_wrkd,payrate
#This function should be to calculate the employee's regular pay hours
#It accepts arguments from get_info
def calculate_reg_pay(hrs_wrkd,payrate):
reg_hrs = hrs_wrkd
reg_pay = reg_hrs * payrate
OT_hrs = 0
OT_pay = 0
return reg_hrs,reg_pay,OT_hrs,OT_pay
#This function should calculate the Overtime pay for the employee
#It accepts arguments from the get_info function as well
def calculate_OT_pay(hrs_wrkd,payrate):
reg_hrs = hrs_wkrd - 40
reg_pay = reg_hrs * payrate
OT_hrs = hrs_wrkd - reg_hrs
OT_pay = OT_hrs * (payrate * 1.5)
return reg_hrs,reg_pay,OT_hrs,OT_pay
#This function decides which calculation to use, either OT or regular pay
#It also accepts srguments from get_info
def calc_employee(hrs_wrkd,payrate):
if hrs_wrkd <= 40:
calculate_reg_pay(hrs_wrkd,payrate)
else:
calculate_OT_pay(hrswrkd,payrate)
#This function should print the single employee information after it was calculated
#It gets its arguments from the calc_employee function
def print_employee(reg_pay,OT_pay,name):
print(name,'earned $',format(reg_pay,'.2f'),' worth of regular pay and ',format(OT_pay,'.2f'),' in overtime this week.')
#This function is supposed to calculate the running total of the hours and pay for overtime and regular pay for the company
# It accepts its arguments from the calc_employee function also
def running_total(reg_hrs,reg_pay,OT_hrs,OT_pay,total_reg_hrs,total_reg_pay,total_OT_hrs,total_OT_pay):
total_reg_hrs = total_reg_hrs + reg_hrs
total_reg_pay = total_reg_pay + reg_pay
total_OT_hrs = total_OT_hrs + OT_hrs
total_OT_pay = total_OT_pay + OT_pay
#This function is supposed to print out the running total for the company, but I realized that it isnt in the proper position when called
def print_totals(total_reg_hrs,total_reg_pay,total_OT_hrs,total_OT_pay):
print('The total regular hours worked was',total_reg_hours)
print('The total regular pay was $',format(total_reg_pay,'.2f'))
print('The total overtime hours worked was',total_OT_hours)
print('The total overtime pay was $',format(total_OT_pay,'.2f'))
# So here I am defining the main loop that will activate everytime the user selects Yes
#It calls most of the other functions
def main_loop():
get_info
calc_employee
print_employee
running_total
#Here I am defining the main program where I put the loop control
def main():
loop_control = input("Would you like to enter an employee's name, payrate and hours? y to do so")
if loop_control == "y":
main_loop
else:
print_totals(total_reg_hrs,total_reg_pay,total_OT_hrs,total_OT_pay)
#Here we call the main function
main()
推荐答案
您需要调用您的函数,这只会引用函数对象:
You need to call your functions. This does nothing but reference the function object:
main_loop
添加()
功能:
Add ()
to call a function:
main_loop()
在您的 main_loop()
函数中相同:
Same in your main_loop()
function:
def main_loop():
get_info()
calc_employee()
print_employee()
running_total()
不是那样,因为你的函数需要参数;你需要在函数调用中传入这些参数。 c $ c> get_info()接受它忽略的参数;删除这些参数()函数签名:
Not that that'll work, because your functions take parameters; you need to pass those in in a function call to work. get_info()
takes parameters it then ignores; remove those parameters in the function signature:
def get_info():
print('is this working?');
name = input('What is the last name of the employee?');
hrs_wrkd = float(input('How many hours did',name,' work last week?'));
payrate = float(input('How much does',name,' get paid?'));
return name,hrs_wrkd,payrate
接下来,您要指定返回值的; get_info()
会返回一些内容,但您从不指定任何内容:
Next you want to assign return values; get_info()
returns a few things, but you never assign any of them:
name, hours_worked, payrate = get_info()
现在您可以将这些传递给其他函数调用:
Now you can pass these to other function calls:
calc_employee(hours_worked, payrate)
等。
您已经退出了一些修复程序,这超出了范围这个答案虽然。不要忘记在函数调用中也使用 return
!例如, calc_employee
不会返回任何内容,例如,它只是忽略它委托给它返回的功能。
You'll have quit a few more to fix, that is outside the scope of this answer though. Don't forget to use return
on function calls too! calc_employee
doesn't return anything, for example, it simply ignores what the functions it delegates to return, for example.
这篇关于Python程序不会在第一次用户输入之外执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!