Python“格式”语法错误

时间:2015-09-29 14:24:08

标签: python syntax syntax-error

#Get the user's name.
Name = input('Enter your name.')

#Get number of stocks purchased.
Stocks_P = int(input('Enter the number of stocks purchased.'))

#Get purchase price of stocks.
Price_P = float(input('Enter the price of stocks purchased.'))

#Calculate total price.
Total_price = Stocks_P * Price_P

#Calculate Commission.
Com1 = Total_price * 0.03

#Calculate Cost.
Cost = Com1 + Total_price

#Get number of stocks sold.
Stocks_S = int(input('Enter the number of stocks sold.'))

#Get sale price of stocks.
Price_S = float(input('Enter the sale price of stocks.'))

#Calculate sale.
Sale = Stocks_S * Price_S

#Calculate sale Commission.
Com2 = Sale * 0.03

#Calculate profit or loss.
Profit = Sale - (Cost + Com2)

print('Your end total is: $' format(Profit, ',.2f') Name, sep='')

这就是我在我的python类中第一次使用的内容,在最后一行中,“print('结束总数为:$')后面的任何内容都会返回语法错误,无论我如何更改它。< / p>

1 个答案:

答案 0 :(得分:1)

实际上,只是列出一个字符串,一行format()调用和一个变量名称都不是有效的Python语法。

使用逗号将这三个内容作为单独的参数传递,或者为要插入的值创建str.format()模板:

print('Your end total is: $', format(Profit, ',.2f'), Name, sep='')

print('Your end total is: ${:,.2f}{}'.format(Profit, Name))