简体   繁体   中英

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='')

that's what i'm using for my first assignment in my python class, and in the last line, anything after the "print('You end total is: $' returns a syntax error no matter how i change it.

Indeed, just listing a string, a format() call and a variable name in a row is not valid Python syntax.

Either pass those three things in as separate arguments , using commas, or create a str.format() template for the values to be interpolated into:

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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