Hello guys I’m currently working on this program. Sorry if pasting the whole code of my program is wrong as I did not find a way to import a file
import easygui
import sys
Setting the card items in nested dictionaries
cards_catalogue = {
“Stoneling”: {
“Strength”: 7,
“Speed”: 1,
“Stealth”: 25,
“Cunning”: 15
},
"Vexscream": {
"Strength": 1,
"Speed": 6,
"Stealth": 21,
"Cunning": 19
},
"Dawnmirage": {
"Strength": 5,
"Speed": 15,
"Stealth": 18,
"Cunning": 22
},
"Blazegolem": {
"Strength": 15,
"Speed": 20,
"Stealth": 23,
"Cunning": 6
},
"Websnake": {
"Strength": 7,
"Speed": 15,
"Stelth": 10,
"Cunning": 5
},
"Moldvine": {
"Strength": 21,
"Speed": 18,
"Stealth": 14,
"Cunning": 5
},
"Vortexwing": {
"Strength": 19,
"Speed": 13,
"Stealth": 19,
"Cunning": 2
},
"Rotthing": {
"Strength": 16,
"Speed": 7,
"Stealth": 4,
"Cunning": 12
},
"Froststep": {
"Strength": 14,
"Speed": 14,
"Stealth": 17,
"Cunning": 4
},
"Wispghoul": {
"Strength": 17,
"Speed": 19,
"Stealth": 3,
"Cunning": 2
},
}
Setting minimum and maximum
possible values
for each category
VALUE_MIN = 1
VALUE_MAX = 25
def display_options():
“”“Displays the menu options for the user”""
options = {
"Add Card": add_card,
"Find Card": find_card,
"Delete Card": delete_card,
"Output all": output_cards,
"Exit": exit_program,
}
get_input = "Yes"
# Loop below continues until the user selects "Exit" from options
# and get input is set as "No"
while get_input != "No":
msg = "What would you like to do?"
title = "Card Catalogue Options"
choices = []
# For loop to populate the choices in the easygui menu
for i in options:
choices.append(i)
selection = easygui.buttonbox(msg, title, choices)
if selection == None:
sys.exit()
# Calls a specific function, based on the users selection from
# the cards.
# For example, if user clicks on "Delete" button, it well be
# get_input = options[leave()]
get_input = options[selection]()
def add_card():
“”“Allows the user to add a card”""
add_more = "Yes"
while add_more == "Yes":
card_name = easygui.enterbox("Enter the card name: ",
title="Card Name")
# Calls function to validate name input
card_name = validate_name(card_name)
if card_name == None:
display_options()
# Calls function to check if the card name is already used
# if it is the user is told an error message and is
# returned to the main menu options
name_check = check_exists(card_name)
if name_check == card_name:
msg = "That card name has already been used"
name_check = easygui.msgbox(msg)
display_options()
else:
card_name = card_name.capitalize()
cards_catalogue[card_name] = {}
# Another functions
add_items(card_name)
def find_card():
“”“Searches for a specific card from the catalogue.If the card is in
the catalogue, the card details are displayed on the screen
Otherwise the user is returned to the main menu options”""
msg = "Enter card to search"
title = "Card to search"
find_card = easygui.enterbox(msg, title)
find_card = validate_name(find_card)
if find_card == None:
display_options()
# Calls function to check whether the card already exists
card = check_exists(find_card)
# Calls function to display card details on screen.
if card:
show_menu(card)
# If the comic does not exist, the user is told this and
# is returned to the main menu options
else:
msg = "There is no such card in the catalogue."
title = "No card"
no_card = easygui.msgbox(msg, title)
def delete_card():
“”“Allows the user to delete a card from the catalogue”""
msg = "Enter name of card you want to delete."
title = "comic to delete"
del_card = easygui.enterbox(msg, title)
del_card = validate_name(del_card)
if del_card == None:
display_options()
# Calls function to check if the card exists
del_card = check_exists(del_card)
# If the card exists, the user is asked to confirm whether they
# want to delete the card. Otherwise they are shown an
# error message and returned to the main menu options
if del_card:
msg = "Are you sure you want to delete the " + del_card \
+ "card?"
title = "Confirm Delete"
choices = ["Yes", "No"]
sure = easygui.buttonbox(msg, title, choices)
if sure == "Yes":
cards_catalogue.pop(del_card)
display_options()
else:
msg = "That card is not on the menu"
title = "ERROR: comic not on menu"
error = easygui.msgbox(msg, title)
display_options()
def output_cards():
“”“Prints the entire card catalogue to screen and to the shell”""
output = ""
for card in cards_catalogue:
output = output + f"{card}:\n"
for card_item, stats in cards_catalogue[card].items():
output = output + f" {card_item}: {stats}\n"
output = output + "\n"
# Prints the card catalogue to the shell
print(output)
easygui.msgbox(output, title = "Card Catalogue")
def validate_name(name):
“”“Input validation: Checks that the user has
input a valid name while adding, deleting or
searching for a card”""
# If the user presses enter without entering a name they see
# an error message, and prompted to enter the name again.
while name == "" or name == 0:
msg = "A name must be entered"
title = "Error: Name required"
name = easygui.enterbox(msg, title)
return(name)
def check_exists(find_card):
“”“Checks if the card exists in the catalogue when
the user is adding, deleting or searching for a
card”""
for card_name, card_info in cards_catalogue.items():
if find_card.title() == card_name:
card = card_name
return(card)
display_options()
def exit_program():
“”“Thanks the user for using the card catalogue and exits the program”""
easygui.msgbox("Thank you for using the program. Goodbye!",
title = "Exiting Program")
sys.exit()
def show_menu(name):
“”“Displays details of a crad that has been added by the user or
a card that has been searched for by the user”""
output = [f"\n\n***{name} Card Details ***\n"]
for value in cards_catalogue[name]:
stats = format(cards_catalogue[name][(value)])
info = value + ": " + stats
output.append(info)
details = easygui.msgbox("\n". join(output))
“”"
def add_items(card_name):
“”" Allows the user to add items to the card"""
msg = “Enter items seperated by a comma”
title = “Item to add”
items = easygui.enterbox(msg, title)
items = validate_name(items)
if items == None:
cards_catalogue.pop(card_name)
display_options()
items_list = items.split(",")
The code belows handles a situation
where the user puts a space after the comma and
when the user puts in an
invalid value for a category
for i in items_list:
item = i.strip()
msg = item + “: Enter Card Details”
title = item + “Details”
cards_catalogue[card_name][item] = easygui.integerbox(msg, title, 0, VALUE_MIN, VALUE_MAX)
if cards_catalogue[card_name][item] == None:
cards_catalogue.pop(card_name)
display_options()
Calls function to display card details
show_menu(card_name)
display_options()
“”"
def add_items(card_name):
msg = “Speed Details”
title = “Speed”
speed = easygui.integerbox(msg,title,0,VALUE_MIN,VALUE_MIN)
msg = “Stealth Details”
title = “Stealth”
staelth = easygui.integerbox(msg,title,0,VALUE_MIN,VALUE_MAX)
msg = “Strength Details”
title = “Strength”
strength = easygui.integerbox(msg,title,VALUE_MIN,VALUE_MAX)
msg = “Cunning Details”
title = “Cunning”
cunning = easygui.integerbox(msg,title,0,VALUE_MIN<VALUE_MAX)
Calls function to display card details
show_menu(card_name)
display_options()
The main program starts here
display_options()
As you can see I have two of the same functions. One of them is original and the one below is what I’m experimenting with and what I need help with.
def add_items(card_name):
msg = “Speed Details”
title = “Speed”
speed = easygui.integerbox(msg,title,0,VALUE_MIN,VALUE_MIN)
msg = “Stealth Details”
title = “Stealth”
staelth = easygui.integerbox(msg,title,0,VALUE_MIN,VALUE_MAX)
msg = “Strength Details”
title = “Strength”
strength = easygui.integerbox(msg,title,VALUE_MIN,VALUE_MAX)
msg = “Cunning Details”
title = “Cunning”
cunning = easygui.integerbox(msg,title,0,VALUE_MIN<VALUE_MAX)
Calls function to display card details
show_menu(card_name)
display_options()
As I realized that each item in my dictionary has 4 categories. I did not want the user to be able to add more and instead give them the categories and just ask them to enter their details. For the code above I would like to know how to use a for loop to make it more efficient.