繁体   English   中英

Python“格式”语法错误

[英]Python “format” 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('最终总和为:$'之后的任何内容,无论我如何更改,都将返回语法错误。

实际上,仅在一行中列出一个字符串,一个format()调用和一个变量名是无效的Python语法。

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

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

要么

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM