尝试在python中运行此程序时,我一直收到无效的语法错误

时间:2017-09-20 22:49:45

标签: python

它一直停留在第二个def()语句中。除此之外,我需要将每小时工资率,佣金支付率和预扣工资率标记为全局常数,但我不断收到多行声明错误。帮助

#Define the main function
def main():
    display_message()
    HOURLY_PAY_RATE = 7.50
    COMMISSION_RATE = 0.05
    WITHHOLDING_RATE = 0.25     
    name=input('What is the person\'s name? ')
    sales_amount=input(int('What is the sales amount? '))
    hours_worked=input(int('How many hours did they work? '))
    hourly_pay=hours_worked * HOURLY_PAY_RATE
    commission=sales_amount * COMMISSION_RATE
    gross_pay=hourly_pay + commission
    withholding=gross_pay * WITHHOLDING_RATE
    net_pay=gross_pay - withholding
    display_results()

#Define the display_message function
def display_message():
    print('This program calculates a salesperson\'s pay')
    print('Five values are required to calculate this')
    print('Hourly pay', 'Commission', 'Gross pay', 'Withholding')
    print('and Net pay.')

#Define the display_results function
def display_results():
    print('The hourly pay amount for', name, 'is$', format(hourly_pay, ',.2f'))
    print('The commission amount for', name, 'is$', format(commission, ',.2f'))
    print('The gross pay for', name, 'is$', format(gross_pay, ',.2f'))
    print('The withholding amount for$', name, 'is', format(withholding, ',.2f'))
    print('The net pay for', name, 'is$', format(net_pay, ',.2f'))

#Call the main function
main()

2 个答案:

答案 0 :(得分:1)

您的代码中有两种类型的错误:

首先:

sales_amount=input(int('What is the sales amount? '))
hours_worked=input(int('How many hours did they work? '))

函数调用input和int的顺序错误。您的代码所说的是"转换'销售额是多少? '"到一个整数并使输入提示。 Cleary,该字符串无法转换为int类型。你想要的是:

sales_amount=int(input('What is the sales amount? '))
hours_worked=int(input('How many hours did they work? '))

其次,您有一个范围错误。在display_results()中,您正在使用变量name,hourly_pay,commission,gross_pay,withholding和net_pay。但是,这些变量仅存在于main范围内。要解决此问题,您可以执行以下两项操作之一:1)将它们设为全局,或者2)将它们传递给display_results():

display_results(name, hourly_pay, commission, gross_pay, withholding, net_pay)

然后优化你的def:

def display_name(name, hourly_pay, commission, gross_pay, withholding, net_pay):

答案 1 :(得分:0)

这是解决错误的方法:

#Define the main function
def question_one():
    # for python 2 use raw_input
    #numbers = raw_input('What is the person\'s name?\n')
    #for python3 use input
    numbers = input('Persons name, Sales Amount and Hours Worked\n')
    name, sales_amount, hours_worked = numbers.split(',')
    return name, int(sales_amount), int(hours_worked)

#Define the display_message function
def display_message():
    print('This program calculates a salesperson\'s pay')
    print('Five values are required to calculate this')
    print('Hourly pay', 'Commission', 'Gross pay', 'Withholding')
    print('and Net pay.')

#Call the main function
def main():
    display_message()
    HOURLY_PAY_RATE = 7.50
    COMMISSION_RATE = 0.05
    WITHHOLDING_RATE = 0.25
    name, sales_amount, hours_worked = question_one()
    hourly_pay=hours_worked * HOURLY_PAY_RATE
    commission=sales_amount * COMMISSION_RATE
    gross_pay=hourly_pay + commission
    withholding=gross_pay * WITHHOLDING_RATE
    net_pay=gross_pay - withholding
    print('The hourly pay amount for', name, 'is$', format(hourly_pay, ',.2f'))
    print('The commission amount for', name, 'is$', format(commission, ',.2f'))
    print('The gross pay for', name, 'is$', format(gross_pay, ',.2f'))
    print('The withholding amount for$', name, 'is', format(withholding, ',.2f'))
    print('The net pay for', name, 'is$', format(net_pay, ',.2f'))

main()

注意question_one()中你必须使用不同的input()函数,具体取决于你使用的python版本。该函数的输入是:

`kyle,30,40`

它将从一行中获取所有值并从中返回您的计算数据。每个值必须用逗号分隔。希望这会对你有所帮助。