我必须写一个程序,为一家公司写一个文件,并分析销售数据
我在使用analyze_data函数时遇到问题。我不断收到一个错误,说我的request_data函数中的一些变量未定义。我相信这是因为这些变量(如软件)是局部定义的,而不是全局定义的。我尝试在analyze_data函数的开头调用request_data函数来调用我在文件中写入的信息,但仍然得到一个错误。我也不确定我是否正确使用累加器来计算每种产品类型的总数
我该如何解决这个问题
#Request country name from user
#Country name must be at least 2 characters long
def request_countryname():
character_length = 2
while True:
country = input("Please enter the country's name: ")
if len(country) < character_length or not country.isalpha():
print("Name must be at least 2 characters.")
else:
return country
#Request total sales for each product type for the user's country
#Input must be numeric and non negative
def request_sales(product, country_name):
flag = -1
while flag < 0:
sales = input("Please enter the total sales for " + product + " in " + country_name + ": $ ")
try:
sales = float(sales)
except ValueError:
print("Amount must be numeric")
else:
if sales < 0 :
print("Amount must be numeric and and non-negative. ")
else:
flag = 1
return sales
#Iteratively requests country names from the user and asks for totals
#Once the user finishes inputting countries, program will store data to a file
#Program will display total number of records added
def request_data(sales_data):
sales_data = open(sales_data, "w")
count = 0
software_accumulator = 0
hardware_accumulator = 0
accessories_accumulator = 0
again = "y"
while again == "y" or again == "Y":
country_name = request_countryname()
software = request_sales("software", country_name)
hardware = request_sales("hardware", country_name)
accessories = request_sales("accessories", country_name)
#Write data to file
sales_data.write(country_name + '/n')
sales_data.write(software + '/n')
sales_data.write(hardware + '/n')
sales_data.write(accessories + '/n')
count += 1
software_accumulator += software
hardware_accumulator += hardware
accessories_accumulator += accessories
#Request country names from user
again = input("Do you want to add another country? (Enter y/Y for Yes: ")
#Displays total number of records added
print(count, " record(s) successfully added to file")
sales_data.close()
#Calculates and displays information
def analyze_data(sales_data):
sales_data = open(sales_data, "r")
sales_data = request_data(sales_data)
#Calculates total software of all country inputs
total_software = software_accumulator
#Calculates total hardware of all country inputs
total_hardware = hardware_accumulator
#Calculates total accessories of all country inputs
total_accessories = accessories_accumulator
#Calcuates average software
average_software = total_software / count
#Calcuates average hardware
average_hardware = total_hardware / count
#Calcuates average accessories
average_accessories = total_accessories / count
#Calculates total sales
total_sales = total_software + total_hardware + total_accessories
#Prints and displays calculations
print("----------------------------")
print()
print("Average software sales per country: $ ", format(average_software, ',.2f'))
print("Average hardware sales per country: $ ", format(average_hardware, ',.2f'))
print("Average accessories sales per country: $ ", format(average_accessories, ',.2f'))
print()
print("Total software sales: $ ", format(total_software, ',.2f'))
print("Total hardware sales: $ ", format(total_hardware, ',.2f'))
print("Total accessories sales: $ ", format(total_accessories, ',.2f'))
print()
print("Total sales: $ ", format(total_sales, ',.2f'))
#Defines main function
def main():
request_data("sales_data.txt")
analyze_data("sales_data.txt")
#Calls main function
main()
你的直觉是正确的,这与变量所在的范围有关,注意定义变量的位置,一旦退出函数,该函数范围内的所有变量都将消失。不要使用全局变量,而是从函数返回所需的值
我建议也使用IDE进行开发,当您访问一个不在范围内(即未定义)的变量时,它们会告诉您—PyCharm或VSCode都是高度可访问的
IDE说函数没有返回值
IDE表示未定义变量
相关问题 更多 >
编程相关推荐