Python “我做错了什么,而我一直在得到”;typererror:只能将str(而不是int';)连接到str";?

Python “我做错了什么,而我一直在得到”;typererror:只能将str(而不是int';)连接到str";?,python,math,import,typeerror,decode,Python,Math,Import,Typeerror,Decode,我不熟悉编码/python,因此非常感谢您的帮助。我正试图找出如何将userQuantityPurchased和userPrice添加在一起,但我不知道该怎么做。这是Python btw,因此提前感谢您 #Write a program that will ask the User to enter the Customer Name, Quantity purchased, and the Price per unit. The program should compute the Sa

我不熟悉编码/python,因此非常感谢您的帮助。我正试图找出如何将userQuantityPurchased和userPrice添加在一起,但我不知道该怎么做。这是Python btw,因此提前感谢您

  #Write a program that will ask the User to enter the Customer Name, Quantity purchased, and the Price per unit. The program should compute  the Sales Amount, State and County 
    #sales taxes. The program should also computer the net sales and discount givenn tot he customer after deduction of the sales taxes from the gross sales. Assume the state sales tax is 9 percent, County sales tax is 5 percent, and discount is %10. A lop should prompt the user to enter yes(y) to process another record or not.sum
import math

userName = input("Enter Customer Name: ")
userQuantityPurchased = int(input("Enter Quantity Purchased: "))
userPrice = int(input("Enter Price per Unit: "))




print("------------------------------")
print("Here is your Net Sale!")
print("------------------------------")

print("Customer Name:  " + userName)
print("Sales Amount: " + userQuantityPurchased + userPrice)
问题是:

"Sales Amount: " + userQuantityPurchased + userPrice
您有一个字符串,您正试图向其中添加一个数字。
+
运算符可以将字符串连接在一起,但不会自动将数字连接到字符串。您必须将数字量转换为字符串才能将其串联:

"Sales Amount: " + str(userQuantityPurchased + userPrice)
虽然基于变量名,但您似乎希望实际将
userPrice
乘以
userQuantityPurchased
,这样总金额将等于购买的所有项目的总成本:

"Sales Amount: " + str(userQuantityPurchased * userPrice)

使用此选项连接值:

print("Sales Amount: " + str(userQuantityPurchased) + str(userPrice))
或:

要连接总和,请尝试以下操作:

import math
userName = input("Enter Customer Name: ")
userQuantityPurchased = int(input("Enter Quantity Purchased: "))
userPrice = int(input("Enter Price per Unit: "))

print("------------------------------")
print("Here is your Net Sale!")
print("------------------------------")

print("Customer Name:  " + userName)
print("Sales Amount: " , int(userQuantityPurchased) + int(userPrice))
而且,听起来你可能想要乘法,而不是加法。将购买的数量与购买价格相加不会得到订单的总数,如果将它们相乘的话

售出数量4+价格6=总销售额10

vs


售出数量4*price 6=总销售额24

我建议根据中设置的约定使用f字符串和名称变量

函数名应该是小写的,单词之间用 必要时加下划线以提高可读性

变量名遵循与函数名相同的约定

所以代码可以是这样的:

customer = input('Enter Customer Name: ')
quantity = int(input("Enter Quantity Purchased: "))
unit_price = int(input("Enter unit price: ")

print(f"Customer name: {customer}")
print(f"Sales amount: {quantity * unit_price}")

非常感谢你!非常感谢,非常感谢!非常感谢。谢谢你的邀请explanation@StevenMusk:如果此答案解决了您的问题,请确保在您有机会时将其标记为已接受答案。此外,你可以投票选出任何你认为有用的答案。这有助于将最佳响应推到顶部,并奖励贡献者的解释。如果您只是将这些作为参数传递给
print
,则无需使用
int()
来包装它们。不管怎样,它们已经是整数了,这没有什么区别,因为不管怎样,它们都会被打印出来。
customer = input('Enter Customer Name: ')
quantity = int(input("Enter Quantity Purchased: "))
unit_price = int(input("Enter unit price: ")

print(f"Customer name: {customer}")
print(f"Sales amount: {quantity * unit_price}")