debugging 为什么我的程序在使用这个循环结构时崩溃?

vcirk6k6  于 2023-10-24  发布在  其他
关注(0)|答案(4)|浏览(116)

一个程序不断崩溃,我不能找出错误或什么原因导致它崩溃
这就是我所拥有的,请告诉我我错过了什么

STATE_TAX_RATE = 0.6
CITY_TAX_RATE = 0.2

totalTax = itemPrice * STATE_TAX_RATE + itemPrice * CITY_TAX_RATE

totalPrice = itemPrice + totalTax

itemPrice = float(input("Enter price: "))

print("Item price is " + str(itemPrice) + " and total tax is " + str(totalTax))
print("Total price is " + str(totalTax))
evrscar2

evrscar21#

看看这个作品
注意项目顺序价格

STATE_TAX_RATE = 0.6
CITY_TAX_RATE = 0.2
itemPrice = float(input("Enter price: "))
totalTax = itemPrice * STATE_TAX_RATE + itemPrice * CITY_TAX_RATE

totalPrice = itemPrice + totalTax

print("Item price is " + str(itemPrice) + " and total tax is " + str(totalTax))
print("Total price is " + str(totalTax))
cyej8jka

cyej8jka2#

这里没有循环。如下所示重新排序代码将修复您的问题:

STATE_TAX_RATE = 0.6
CITY_TAX_RATE = 0.2

itemPrice = float(input("Enter price: "))

totalTax = itemPrice * STATE_TAX_RATE + itemPrice * CITY_TAX_RATE

totalPrice = itemPrice + totalTax

print("Item price is " + str(itemPrice) + " and total tax is " + str(totalTax))
print("Total price is " + str(totalTax))
vdzxcuhz

vdzxcuhz3#

Name错误:名称'itemPrice'未定义
这是您得到的错误,因为您在计算之后(在需要的地方)分配了itemPrice,所以您得到了一个NameError
通过重新排序代码,

itemPrice = float(input("Enter price: "))

在计算totalTax之前,您的错误应该被修复
完全正确的代码-

STATE_TAX_RATE = 0.6
CITY_TAX_RATE = 0.2

itemPrice = float(input("Enter price: "))
''' now it is assigned first so it will indeed carry out the calculation 
afterwards without any errors '''

totalTax = itemPrice * STATE_TAX_RATE + itemPrice * CITY_TAX_RATE

totalPrice = itemPrice + totalTax

print("Item price is " + str(itemPrice) + " and total tax is " + 
str(totalTax))
print("Total price is " + str(totalTax))
eivgtgni

eivgtgni4#

你试图使用一个未定义的变量。将itemPrice = ...行移高,这样它就在任何itemPrice变量的使用之前。
顺便说一下,这里没有循环.
完整代码:

STATE_TAX_RATE = 0.6
CITY_TAX_RATE = 0.2

itemPrice = float(input("Enter price: "))

totalTax = itemPrice * STATE_TAX_RATE + itemPrice * CITY_TAX_RATE

totalPrice = itemPrice + totalTax

print("Item price is " + str(itemPrice) + " and total tax is " + str(totalTax))
print("Total price is " + str(totalTax))

相关问题