我试图在函数中使用字典值,并且一直得到一个NameError
。不知道我做错了什么。在
import re
def main():
sales_amount = get_amount()
customer_ID = get_ID()
discounts = read_discounts()
calculate_discount(sales_amount, discounts)
print(discount_percentage)
#print (discounts)
def get_amount():
return float(input("Enter total sales amount: "))
def get_ID():
return float(input("Enter customer ID: "))
#function to read discount values, strip non-alphanumeric characters and add values to dictionary
def read_discounts():
myFile = open('discount.txt', 'r')
discountValues = {}
#read and split first line
firstLine = myFile.readline()
firstLine = re.sub(r'\$','',firstLine)
firstLine = re.sub(r'\%','',firstLine)
firstLine = firstLine.split()
#add values to dictionary
discountValues['UpperLimit1'] = {firstLine[2]}
discountValues['UpperLimit1'] = [ float(x) for x in discountValues['UpperLimit1'] ]
discountValues['PercentDiscount1'] = {firstLine[4]}
discountValues['PercentDiscount1'] = [ float(x) for x in discountValues['PercentDiscount1'] ]
return (discountValues)
def calculate_discount(sales_amount,discounts):
if sales_amount < discountValues[UpperLimit1]:
discount_percentage = discountValues[PercentDiscount1]
main()
这是回溯:
^{pr2}$我尝试过将字典名称作为函数中的参数传递,但结果相同。另外,我应该注意到,我知道将文本文件读入字典的方法可能不是最佳方法,但我现在想把重点放在calculate_discount
函数上。在
完全公开:这是一个任务。在
正如我看到的类AddressBookGUI,有两个构造函数,但只有一个构造函数没有初始化
addressbook = new ExtendedAddressBook();
的参数我想你可以调用第二个带标题的构造函数,这就是为什么
addressbook
为空。这是NullPointException的原因==>;解决方案是在第二个构造函数中添加
addressbook
的初始化错误的邮政编码:请参见此示例:
}
相关问题 更多 >
编程相关推荐