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

2024-09-24 06:35:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉编码/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)

Tags: andthetonameinputiscustomerprogram
3条回答

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

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Variable names follow the same convention as function names.

所以代码可以是这样的:

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}")

使用此选项连接值:

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

或:

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

连接总和

问题是:

"Sales Amount: " + userQuantityPurchased + userPrice

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

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

虽然基于变量名,但您似乎希望实际将乘以userPrice乘以userQuantityPurchased,这样总金额将等于所有购买物品的总成本:

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

相关问题 更多 >