我的变量在Python中是未定义的
我试图挑战自己,从头开始创建一个程序来帮助解决错误,等等 所以我做了一个简单的邮票商店,它对不同的邮票书籍有不同的价值。我现在想计算一下每件在征税后的价格。我试图设置一个if/elif/else语句来计算每个邮票的价格。这是我目前掌握的代码 ''' #每枚邮票包括税的价格是多少我的变量在Python中是未定义的,python,Python,我试图挑战自己,从头开始创建一个程序来帮助解决错误,等等 所以我做了一个简单的邮票商店,它对不同的邮票书籍有不同的价值。我现在想计算一下每件在征税后的价格。我试图设置一个if/elif/else语句来计算每个邮票的价格。这是我目前掌握的代码 ''' #每枚邮票包括税的价格是多少 number_of_stamps_purchased = 10 # calculate government sales taxes on stamps 13% sales_tax = 0.1
number_of_stamps_purchased = 10
# calculate government sales taxes on stamps 13%
sales_tax = 0.13
# number of stamps in each booklet
single_stamp = 1
book_of_five = 5
book_of_ten = 10
book_of_fifteen = 15
book_of_twenty = 20
# pricing chart for each book of stamps
single = 1.50
five = 7.00
ten = 10.00
fifteen = 14.00
twenty = 16.00
# price list for customers
print('''Welcome to my stamp shop!
Please review our price list:
Single Stamp $1.50
Book of 5 Stamps $7.00
Book of 10 Stamps $10.00
Book of 15 Stamps $14.00
Book of 20 Stamps $16.00
(Does not include sales tax)''')
# conditions for total price calculation of stamps purchased
def stamp_cost(price_per_piece):
if number_of_stamps_purchased <= 4:
print('''
Your stamp cost:''',((single * sales_tax / single_stamp) + single) * number_of_stamps_purchased)
elif number_of_stamps_purchased == 5:
print('''
Your stamp cost:''',((five * sales_tax) + five))
elif number_of_stamps_purchased == 10:
print('''
Your stamp cost:''',((ten * sales_tax) + ten))
elif number_of_stamps_purchased == 15:
print('''
Your stamp cost:''',((fifteen * sales_tax) + fifteen))
elif number_of_stamps_purchased == 20:
print('''
Your stamp cost:''',((twenty * sales_tax) + twenty))
else:
print('Error')'''
# calculate the per piece price based on their savings when purchasing a book
print(total_price / number_of_stamps_purchased)
购买的邮票数量=10
#计算邮票的政府销售税13%
销售税=0.13
#每本小册子内的邮票数目
单张邮票=1
五本书=5本
十本书中的十本=10本
第十五册=15册
二十本书=20本
#每本邮票的定价表
单个=1.50
五=7.00
十=10.00
15=14.00
二十=16.00
#顾客价目表
欢迎来到我的邮票店!
请查看我们的价格表:
单枚邮票$1.50
一本五枚邮票$7.00
一本十枚邮票$10.00
一本十五枚邮票$14.00
一本二十枚邮票$16.00
(不包括销售税)“”)
#购买邮票的总价计算条件
def印花成本(每件价格):
如果购买的邮票数量您从未声明名为总价的变量,那么当您尝试使用print打印(购买的邮票总数/价格)
时,Python会警告您不要定义它。您可以通过在最后一行之前定义一个总价
来解决此问题。也许是这样:
def stamp_cost(): # I have removed the argument as it was unused.
if number_of_stamps_purchased <= 4:
total_price = ((single * sales_tax / single_stamp) + single) * number_of_stamps_purchased
print('''
Your stamp cost:''', total_price)
# The rest of the function with the same modification
return total_price
total_price = stamp_cost()
print(total_price / number_of_stamps_purchased)
def stamp_cost():#我删除了未使用的参数。
如果你买了多少张邮票,像这样的东西应该会好一点。正如前面所指出的,主要问题是,您从未调用您的基金stamp\u cost
。因此,您需要做的是将您使用stamp\u cost
所做的操作的结果影响到您的变量total\u price
# calculate government sales taxes on stamps 13%
sales_tax = 0.13
# pricing chart for each book of stamps
single = 1.50
five = 7.00
ten = 10.00
fifteen = 14.00
twenty = 16.00
# number of stamps in each booklet
single_stamp = 1
book_of_five = 5
book_of_ten = 10
book_of_fifteen = 15
book_of_twenty = 20
# price list for customers
print('''Welcome to my stamp shop!
Please review our price list:
Single Stamp $1.50
Book of 5 Stamps $7.00
Book of 10 Stamps $10.00
Book of 15 Stamps $14.00
Book of 20 Stamps $16.00
(Does not include sales tax)
''')
# conditions for total price calculation of stamps purchased
def stamp_cost():
if number_of_stamps_purchased <= 4:
total_price = (single * sales_tax / single_stamp + single) * number_of_stamps_purchased
print('Your total cost:', total_price)
elif number_of_stamps_purchased == 5:
total_price = five * sales_tax + five
print('Your total cost:',total_price)
elif number_of_stamps_purchased == 10:
total_price = ten * sales_tax + ten
print('Your total cost:', total_price)
elif number_of_stamps_purchased == 15:
total_price = fifteen * sales_tax + fifteen
print('Your total cost:',total_price)
elif number_of_stamps_purchased == 20:
total_price = twenty * sales_tax + twenty
print('Your total cost:',total_price)
else:
print('Error')
return total_price
number_of_stamps_purchased = 10
print('Number of stamp purchase:',number_of_stamps_purchased)
total_price = stamp_cost()
# calculate the per piece price based on their savings when purchasing a book
print('Price per piece:',round(total_price / number_of_stamps_purchased,2))
#计算邮票的政府销售税13%
销售税=0.13
#每本邮票的定价表
单个=1.50
五=7.00
十=10.00
15=14.00
二十=16.00
#每本小册子内的邮票数目
单张邮票=1
五本书=5本
十本书中的十本=10本
第十五册=15册
二十本书=20本
#顾客价目表
欢迎来到我的邮票店!
请查看我们的价格表:
单枚邮票$1.50
一本五枚邮票$7.00
一本十枚邮票$10.00
一本十五枚邮票$14.00
一本二十枚邮票$16.00
(不包括销售税)
''')
#购买邮票的总价计算条件
def stamp_cost():
如果购买了多少张邮票,你能分享完整的错误信息/回溯吗?请确保你提供的代码按原样运行,并允许重现问题。运行代码后,我发现有几个问题:a)函数中的参数price\u per\u piece
从未使用过,所以你可以摆脱它:def stamp_cost():…
。b) total\u price
已被使用,但从未定义(我想这是您的问题所在),因此请将值设置为total\u price
,或者删除该特定行。可能在局部范围内使用全局变量会导致此类错误。->这对你有帮助