Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

NEED HELP TO WRITE LOGGING AND REFRESHING FUNCTIONS AND CALLING THEM. Please precisely follow instructions: this file is called bottle.py bottle_types = ( (metal, 20),

NEED HELP TO WRITE LOGGING AND REFRESHING FUNCTIONS AND CALLING THEM. Please precisely follow instructions: this file is called bottle.py

bottle_types = ( ("metal", 20), ("plastic", 50), ("wooden", 100) ) def purchase_bottles(money: int, weather_protection_consumed: bool) -> tuple: bottles_bought = [0, 0, 0] # metal, plastic, wooden bottle_type_indices = {"metal": 0, "plastic": 1, "wooden": 2} money_spent = 0 while True: print(f"You have {money}") bottles_input = input("Enter [bottles type] [quantity] (e.g. metal 3) or 'back': ") if bottles_input == "back": break bottles_type, bottles_quantity_str = bottles_input.split(' ') bottles_quantity = int(bottles_quantity_str) bottle_type_index = bottle_type_indices.get(bottles_type, -1) if bottle_type_index == -1: print("Sorry, we don't sell that type of bottle.") else: bottles_cost = bottles_quantity * bottle_types[bottle_type_index][1] if money < bottles_cost: print("Not enough money to purchase that many bottles.") else: bottles_bought[bottle_type_index] += bottles_quantity money_spent += bottles_cost money -= bottles_cost print(f"Successfully purchased {bottles_quantity} {bottles_type} bottles.") if not weather_protection_consumed: if weather_protection == 'hat': money += 25 weather_protection_consumed = True print("You received a benefit of $25 for using a hat for weather protection.") elif weather_protection == 'cap': bottles_bought[1] += 1 weather_protection_consumed = True print("You received a benefit of 1 additional plastic bottle for using a cap for weather protection.") return money, tuple(bottles_bought), weather_protection_consumed def display_purchase(money: int, bottles_bought: list, weather_protection: str) -> None: money_left = money print() print(f'Money left: {money}') print("Bottles:") print(f'metal: {bottles_bought[0]}') print(f'plastic: {bottles_bought[1]}') print(f'wooden: {bottles_bought[2]}') print(f'Weather protection: {weather_protection}') print() def is_valid(name) -> bool: valid_name = name_length and len(name) >= 1 and len(name) < 10 and name.isalpha() return valid_name def main(): name_input = str(input("what's you name? ")) if is_valid(name_input): print("hello", name_input) else: print("invalid name but let's continue") money = 100 bottles_bought = [0, 0, 0] # metal, plastic, wooden weather_protection = 'none' weather_protection_consumed = False while True: print("1. Buy bottles") print("2. View purchase") print("3. Leave") print("4: weather_protection") num = int(input()) if num == 4: weather_protection = input("pick protection: ") elif num == 1: purchase_result = purchase_bottles(money, weather_protection_consumed) money, bottles_bought, weather_protection_consumed = purchase_result display_purchase(money, bottles_bought, weather_protection) elif num == 2: display_purchase(money, bottles_bought, weather_protection) elif num == 3: break if __name__ == '__main__': main()

When num == 3, a txt file is made with the user's name if it is valid and the contents in /home/files/log.txt will be copied over to the file called name_input.txt where name_input is the valid name. The name_input.txt must be saved at /home/files/ as well. So, at the end of the bottle program, a file of contents can be accessed at either the default or name_input path.

To log the users input, log function must be called in bottle.py but no other inputs or outputs must be changed. All these inputs are logged into /home/files/log.txt and then copies to /home/files/name_input.txt if valid name. The file contents in log.txt and then subsequently name_input.txt will be refreshed when a new bottle program begins (when bottle.py is input again into terminal).

In a separate python file called filelog.py are these two functions:

def restart_file(filepath):

#filepath is the absolute path to a file

#creates empty file if filepath exists

In this function, if the file exists, file gets rid of all the contents in them and creates empty file at the filepath. Else the function displays error "file path not found"

def log(material_to_log, filepath = "/home/files/log.txt"):

#material_to_log is the users inputs into the program

#filepath default is default log.txt

In this function, if no arguments are provided, function by default writes material into default file. This function returns True if material is successfully logged into file, new material is always appended to end of the file in new line. Otherwise, function returns False and does nothing.

If filepath argument is valid path and file is not existing, a new file of this path is created and material is written at start.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions