0

can someone help me figure out why my code isn't taking my input for this code?

import os def read_file(dictionary, file_path): try: with open(file_path, 'r') as file: for line in file: # Ensure that the month abbreviation is stored in uppercase to make it case-insensitive month, amount = line.strip().split(',') dictionary[month.capitalize()] = float(amount) print("File read successfully.") except FileNotFoundError: print(f"File not found at {file_path}. Initializing with default values.") initialize_default_data(dictionary) def write_file(dictionary, file_path): with open(file_path, 'w') as file: for month, amount in dictionary.items(): file.write(f"{month},{amount}\n") print("File written successfully.") def initialize_default_data(dictionary): default_data = { 'Jan': 14317.00, 'Feb': 10500.00, 'Mar': 1000.00, 'Apr': 2000.00, 'May': 3000.00, 'Jun': 4000.00, 'Jul': 5000.00, 'Aug': 15578.00, 'Sep': 6000.00, 'Oct': 7000.00, 'Nov': 88.00, 'Dec': 8000.00 } dictionary.update(default_data) def view_sales_amount(dictionary): month = input("Three-letter month: ").capitalize() if month in dictionary: print("Sales amount for {:s} is ${:,.2f}.".format(month, dictionary[month])) else: print("Invalid three-letter month. Try again.") def get_highest_amount(dictionary): if dictionary: max_month = max(dictionary, key=dictionary.get) print("The highest sales amount is ${:,.2f} in {:s}".format(dictionary[max_month], max_month)) else: print("No sales data available.") def get_lowest_amount(dictionary): if dictionary: min_month = min(dictionary, key=dictionary.get) print("The lowest sales amount is ${:,.2f} in {:s}".format(dictionary[min_month], min_month)) else: print("No sales data available.") def edit_sales_amount(dictionary): month = input("Three-letter month: ").capitalize() if month in dictionary: try: new_amount = fl

17th Nov 2023, 10:36 PM
Giovani Medina
Giovani Medina - avatar
2 Answers
+ 4
your code is incomplete. Looking at your profile codebits, I found your file. https://code.sololearn.com/cLd1syvcAk99/?ref=app running it in Pydroid, it seems that dictionary is empty. Was your dictionary initialized properly? How are you accessing it for each function?
17th Nov 2023, 11:58 PM
Bob_Li
Bob_Li - avatar
+ 4
Giovani Medina add a read_file function call. if __name__ == "__main__": sales_data = {} file_path = "monthly_sales.txt" read_file(sales_data, file_path) here is a working copy of your code https://code.sololearn.com/cI3d6uwwVDuZ/?ref=app
18th Nov 2023, 1:10 AM
Bob_Li
Bob_Li - avatar