Question
For Python: Create a program that keeps track of the items that a wizard is carrying. *Your instructor should provide a text file named wizard_all_items.txt
For Python:
Create a program that keeps track of the items that a wizard is carrying.
*Your instructor should provide a text file named wizard_all_items.txt that contains a list of all the items that a wizard can carry.
* You should create another text file named wizard_inventory.txt to store the current items that the wizard is carrying.
* A wizard can only carry four items at time, and those four items must be different.
* When the user selects the walk command, the program should read the items from the file, create a list of the items that aren't already in the wizard's inventory, randomly pick one of those items, and give the user the option to grab it. To create a list of the items that aren't already in the wizard's inventory, you can use a list comprehension as described in chapter 6.
* Make sure to update the inventory text file you created every time the usher grabs or drops an item.
*For the drop command, display an error message if the user enters an invalid number for the item.
*(Code must be placed in functions)
Here is what I have so far.
import random
def read_items(): f = open("wizard_all_items. txt", "r") items = [] for item in f: items.append(item)
f.close() return items
def read_inventory(): f = open("wizard_inventory.txt", "r") invenrtory = [] for item in f: inventory.append(item)
f.close() return inventory
def write_inventory(inventory): f = open("wizard_inventory.txt", "w") for item in inventory: f.write(item) f.close()
def display_title(): print("The Wizard Inventory Program ")
def display_menu(): print("COMMAND MENU") print("walk - walk down a path") print("show - show all items") print("drop - drop an item") print("exit - Exit program")
def walk(): items = read_items() inventory = read_inventory()
random_item = random.choice(items) print("While walking ddown a path, you see", random_item) option = input("Do you want to grab it? (y): ")
if option == "n": print("You didn't picked up ", random_item)
else: total_item = len(inventory) if total_item == 4: print("You cannot carry any more items. Drop something first. ") else: print("you picked up", ranom_item) inventory.append(random_item) write_inventory(inventory) def show(): f = open("wizard_inventory.txt") index = 1 for item in f: item = item.strip() print("%d. %s" %(index, item)) index += 1
def drop(): num = int(input("Number: ")) inventory = read_inventory() total_item = len(inventory) if num total_item: print("Error. Invalid item number.") else: print("You dropped", inventory[num -1]) inventory.pop(num-1) write_inventory(inventory) def main(): display_title()
display_menu()
while True: command = input(" Command: ") if command == "walk": walk() elif command == "show": show() elif command == "drop": drop() elif command == "exit": break else: print("Error. Invalid command.") print("Bye!")
if __name__ == "__main__": main()
Project 7-2: Wizard Inventory Create a program that keeps track of the items that a wizard is carrying. Console The Wizard Inventory program COMMAND MENU walk - Walk down the path show - Show all items drop - Drop an item exit - Exit program Command: walk While walking down a path, you see a scroll of uncursing. Do you want to grab it? (y):y You picked up a scroll of uncursing. Command: walk While walking down a path, you see an unknown potion. Do you want to grab it? (y) : y You can't carry any more items. Drop something first. Command: show 1. a wooden staff 2. a scroll of invisibility 3. a crossbow 4. a scroll of uncursing Command: drop Number: 3 You dropped a crossbow. Command: exit Bye! Specifications - Your instructor should provide a text file named wizard_all_items.txt that contains a list of all the items that a wizard can carry. - You should create another text file named wizard_inventory.txt to store the current items that the wizard is carrying. - A wizard can only carry four items at a time, and those four items must be different. - When the user selects the walk command, the program should read the items from the file, create a list of the items that aren't already in the wizard's inventory, randomly pick one of those items, and give the user the option to grab it. To create a list of the items that aren't already in the wizard's inventory, you can use a list comprehension as described in chapter 6. - Make sure to update the inventory text file you created every time the user grabs or drops an item. - For the drop command, display an error message if the user enters an invalid number for the item
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started