This question already has answers here:
Defining lists as global variables in Python
(3 answers)
Slicing list inside a method (Python 3)
(1 answer)
Closed 5 years ago.
I have the following code:
import sys
import os.path
import ConfigParser
import copy
import time
import colorama
from colorama import init
from colorama import Fore, Back, Style
init()
#init variables
discoveredelements = []
discoveredgroups = []
combo = []
savefile = ConfigParser.ConfigParser()
os.chdir(os.getcwd())
#init other stuff
class style:
BOLD = '\033[1m'
END = '\033[0m'
def combos():
#all combos
combo.append(('Air', 'Air', 'Wind'))
combo.append(('Earth', 'Earth', 'Pressure'))
combo.append(('Fire', 'Fire', 'Explosion'))
combo.append(('Water', 'Water', 'Sea'))
combo.append(('Air', 'Earth', 'Dust'))
combo.append(('Air', 'Fire', 'Energy'))
combo.append(('Air', 'Water', 'Steam'))
combo.append(('Earth', 'Fire', 'Lava'))
combo.append(('Earth', 'Water', 'Swamp'))
combo.append(('Fire', 'Water', 'Alcohol'))
def mainmenu():
print(style.BOLD + "ALCHEMY" + style.END)
print(style.BOLD + "Load Game" + style.END)
print(style.BOLD + "New Game" + style.END)
print(style.BOLD + "Exit" + style.END)
print("Type \"load\" or \"new\" or \"exit\" ")
mainmenuinput = raw_input()
if mainmenuinput == "exit":
sys.exit()
elif mainmenuinput == "load":
if os.path.exists('save.ini'):
savefile.read('save.ini')
discoveredelements = savefile.get('Elements','discoveredelements')
print("Game Loaded")
rungame()
else:
print("Save file not found, check file directory or start a new game.")
mainmenu()
elif mainmenuinput == "new":
if os.path.exists("save.ini"):
print("Current save file will be overwritten. Proceed?")
print("Y or N")
overwriteinput = raw_input()
if overwriteinput == "Y":
newgame()
rungame()
elif overwriteinput == "N":
mainmenu()
else:
newgame()
rungame()
def newgame():
save = open('save.ini','w')
#reset data
savefile.add_section('Elements')
savefile.add_section('Groups')
savefile.set('Elements','discoveredelements',"")
savefile.set('Groups','discoveredgroups',"")
#adds the default elements
discoveredelements.append("Air")
discoveredelements.append("Earth")
discoveredelements.append("Fire")
discoveredelements.append("Water")
savefile.set('Elements','discoveredelements',discoveredelements)
discoveredgroups.append("Air")
discoveredgroups.append("Earth")
discoveredgroups.append("Fire")
discoveredgroups.append("Water")
savefile.set('Groups','discoveredgroups',discoveredgroups)
savefile.write(save)
save.close()
print("Game Loaded")
def gameloop():
#actual gameplay
print("Type two elements (seperately) or \"list\" or \"hint\" or \"save\" or \"exit\"")
gameinput = raw_input()
if gameinput == "list":
displayelements = copy.copy(discoveredelements)
print(','.join(map(str, displayelements)))
gameloop()
elif gameinput == "hint":
if (time.time() - timerstart) >= 10:
print('hint')
timerstart = time.time()
gameloop()
else:
print("Hint is still on cooldown")
gameloop()
elif gameinput == "save":
savefile.set('Elements','discoveredelements',discoveredelements)
savefile.set('Groups','discoveredgroups',discoveredgroups)
print("Game saved")
elif gameinput == "exit":
savefile.read('save.ini')
savelist = savefile.get('Elements','discoveredelements')
if len(savelist) < len(discoveredelements):
print("Game not saved! Do you wish to exit without saving?")
print("Y or N")
overwriteinput = raw_input()
if overwriteinput == "Y":
mainmenu()
else:
gameloop()
else:
elementA = gameinput
elementB = raw_input()
if (elementA in discoveredelements) and (elementB in discoveredelements):
i = 0
created = 0
while True:
if (combo[i][0] == elementA and combo[i][1] == elementB) or (combo[i][1] == elementA and combo[i][0] == elementB):
print("You created " + combo[i][2])
discoveredelements.append(combo[i][2])
created = 1
break
i += 1
if i == len(combo):
break
if created == 0:
print("No elements created")
gameloop()
else:
print("Error, using non-existent or not yet discovered elements")
gameloop()
def rungame():
#initializing game
timerstart = time.time()
displayelements = copy.copy(discoveredelements)
print(','.join(map(str, displayelements)))
gameloop()
#game starts here
print(Style.RESET_ALL)
combos()
mainmenu()
When I type "load" into the console, nothing is outputted for displayelements. So I tried to see if the list contained anything (and if the copy.copy() worked or not) by doing print(displayelements) and it printed []
Then I checked if discoveredelements contained anything and it did:
['Air', 'Earth', 'Fire', 'Water']
Why isn't the copy.copy() working?
EDIT:
I declared discoveredelements as global:
global discoveredelements
discoveredelements = []
The copying still doesn't work, displayelements is still an empty list.
To assign to a global var in function you must declare is as global:
discoveredelements = []
def func():
# a new var is created in the scope of this function
discoveredelements = [1,2,3,4]
func()
print (discoveredelements)
discoveredelements = []
def func():
# declare as global
global discoveredelements
# assign to the global var
discoveredelements = [1,2,3,4]
func()
print (discoveredelements)
Related
main.py:
import keyboard
import ui
import os
os.system("cls")
ui.play[ui.counter] = "> " + ui.play[ui.counter] + " <"
ui.navmenuprint(ui.play)
while True:
while ui.state == "play":
keypressed = keyboard.read_key()
while keyboard.is_pressed("down"): pass
while keyboard.is_pressed("up"): pass
while keyboard.is_pressed("enter"): pass
if keypressed == "up":
os.system("cls")
ui.navup(ui.play, ui.play2)
ui.navmenuprint(ui.play)
if keypressed == "down":
os.system("cls")
ui.navdown(ui.play, ui.play2)
ui.navmenuprint(ui.play)
if keypressed == "enter":
if ui.counter == 0:
ui.switchstate("shop")
if ui.counter == 1:
ui.switchstate("shop")
if ui.counter == 2:
ui.switchstate("shop")
if ui.counter == 3:
ui.switchstate("shop")
while ui.state == "shop":
keypressed = keyboard.read_key()
while keyboard.is_pressed("down"): pass
while keyboard.is_pressed("up"): pass
while keyboard.is_pressed("enter"): pass
if keypressed == "up":
os.system("cls")
ui.navup(ui.shop, ui.shop2)
ui.navmenuprint(ui.shop)
if keypressed == "down":
os.system("cls")
ui.navdown(ui.shop, ui.shop2)
ui.navmenuprint(ui.shop)
if keypressed == "enter":
if ui.counter == 0:
ui.switchstate("play")
if ui.counter == 1:
ui.switchstate("play")
if ui.counter == 2:
ui.switchstate("play")
if ui.counter == 3:
ui.switchstate("play")
if ui.counter == 4:
ui.switchstate("play")
ui.py:
import os
from termcolor import cprint
state = "play"
counter = 0
play = ["TOSHOP", "TOSHOP", "TOSHOP","TOSHOP"]
play2 = ["TOSHOP", "TOSHOP", "TOSHOP","TOSHOP"]
shop = ["TOPLAY", "TOPLAY","TOPLAY","TOPLAY","TOPLAY"]
shop2 = ["TOPLAY", "TOPLAY","TOPLAY","TOPLAY","TOPLAY"]
def switchstate(fromwhere):
global state, counter
if fromwhere == "play":
counter = 0
state = fromwhere
play = play2.copy()
os.system("cls")
play[counter] = "> " + play[counter] + " <"
navmenuprint(play)
if fromwhere == "shop":
counter = 0
state = fromwhere
shop = shop2.copy()
os.system("cls")
shop[counter] = "> " + shop[counter] + " <"
navmenuprint(shop)
def navup(list1, list2):
global counter
if counter != 0:
list1[counter] = list2[counter]
counter -= 1
list1[counter] = "> " + list1[counter] + " <"
else:
list1[counter] = list2[counter]
counter -= 1
list1[counter] = "> " + list1[counter] + " <"
counter = len(list1) - 1
print (counter)
def navdown(list1,list2):
global counter
if counter != len(list1) - 1:
list1[counter] = list2[counter]
counter += 1
list1[counter] = "> " + list1[counter] + " <"
else:
list1[counter] = list2[counter]
counter = 0
list1[counter] = "> " + list1[counter] + " <"
print (counter)
def navmenuprint(list):
global counter
for i in list:
print(i)
This code is an extract from my little homemade console game project, I tried to delete all unnecessary code, I successfully made a working interactive menu which means I want to achieve navigation with up and down arrow in menu and currently selected item show as > item <, handle error if list out of index, state handling (for switching screens).
Unfortunately I had to make a few ugly workaround to make this happen or I just too beginner to figure it out.
Python 3.11, I don't want to use additional modules.
The problem:
Go down to 4th item (counter variable value will be 3)
Press Enter
Go down to 5th item (counter variable value will be 4)
Press Enter
Press down
Actual:
TOSHOP
> TOSHOP <
TOSHOP
> TOSHOP <
Expected:
TOSHOP
> TOSHOP <
TOSHOP
TOSHOP
I understand my code and spent many hours to solve this issue but I have no idea why it's faulty.
I think counter variable value is good everywhere.
I make sure to reset "play" and "shop" list to original form and counter variable to 0.
I had to expand global variables with lists inside switchstate function:
def switchstate(fromwhere):
global state, counter, play, play2, shop, shop2
When I run clear() as below it does not print the 'else' statement. It only works for the 'if' part. When I run it one indent outside, it clears without doing the print for both if and else. Please guide on where I should place it.
import random
from art import logo,vs
from game_data import data
from replit import clear
def game_question():
return random.choice(data)
def format_data(account):
account_name = account["name"]
account_description = account["description"]
account_country = account["country"]
return f"{account_name}, {account_description}, {account_country}"
def count(num_a, num_b):
if num_a > num_b:
return "a"
else:
return "b"
win = 0
play_on = False
while not play_on:
print (logo)
account_a = game_question()
account_b = game_question()
if account_a == account_b:
account_b = game_question()
num_a = account_a["follower_count"]
num_b = account_b["follower_count"]
print(f"Account A : {format_data(account_a)}")
print (vs)
print(f"Compare to Account B: {format_data(account_b)}")
ans = input("Which account has more followers? A or B: ").lower()
if ans == count(num_a,num_b):
win += 1
print ("A win")
else:
print (f"Wrong. You lose. Win = {win}")
play_on = True
clear()
The clear() function is not indented properly in else:. Try this for the if else statement
if ans == count(num_a,num_b):
win += 1
print ("A win")
else:
print (f"Wrong. You lose. Win = {win}")
play_on = True
clear()
I was searching on the internet for projects do with python and came up with a Tic Tac Toe game. But now i'm kind of stuck in some part of the code because i can't store the player moves.
Well, to be honest, i maybe know how to do it, but i'll be using A LOT of if, elif and else. Is there another way i can do it without writing 100+ lines of code just to store some data using my current code?
Ps: It needs to be OOP, as i am learning it right now :l
import msvcrt as m # Using it for 'Press any key to continue' thing.
import os
import keyboard
class TicTacToe:
def __init__(self):
self.difficultAI = 1
self.playWith = 1 # 3 = Strings and Numbers
self.playerOne = None
self.PlayerTwo = None
def menuSelection(self):
os.system('cls')
print(f'[1] - Play\n'
f'[2] - Options\n'
f'[3] - Exit')
try:
option = int(input('I\'ll go with '))
except ValueError as valueError:
print(f'Invalid Option\n'
f'Following error: {valueError}\n'
f'Press any key to continue...')
m.getch() # Waiting for any key to be pressed.min
os.system('cls')
objectRun.menuSelection() # Call menuSelection again.
if option == 1:
objectRun.play()
elif option == 2:
pass
elif option == 3:
exit()
else:
print(f'You should choose something between 1 and 3, and not {option}'
f'Press any key to continue...')
m.getch()
objectRun.menuSelection()
def drawBoard(self, topL='?', topM='!', topR='?',
midL='!', midM='?', midR='!',
lowL='?', lowM='!', lowR='?'):
board = {
'top-L': topL,
'top-M': topM,
'top-R': topR,
'mid-L': midL,
'mid-M': midM,
'mid-R': midR,
'low-L': lowL,
'low-M': lowM,
'low-R': lowR,
}
print(' {0} | {1} | {2}\n'
'----------\n'
' {3} | {4} | {5}\n'
'----------\n'
' {6} | {7} | {8}\n'.format(board['top-L'], board['top-M'], board['top-R'],
board['mid-L'], board['mid-M'], board['mid-R'],
board['low-L'], board['low-M'], board['low-R']))
# change that stupid name when done
def play(self):
os.system('cls')
print(f'[1] - Player VS Player\n'
f'[2] - Player VS Machine\n'
f'[3] - Return')
try:
option = int(input('I\'ll go with '))
except ValueError as valueError:
print(f'Invalid Option\n'
f'Following error: {valueError}\n'
f'Press any key to continue...')
m.getch()
objectRun.play()
try:
os.system('cls')
self.drawBoard()
if option == 1:
if self.playWith == 1: # Numbers
isBoardFull = False # Not using it right now, but ok... I guess.
while isBoardFull != True:
print('Player 1, it\'s your turn\n'
'You can choose between 1 (TOP LEFT) up to 9 (BOTTOM RIGHT)')
self.playerOne = int(input('I\'ll go with '))
self.playerOptions()
elif self.playWith == 2: # Strings
pass
else: # Strings and Numbers
pass
elif option == 2:
pass
elif option == 3:
objectRun.menuSelection()
else:
print(f'You should choose something between 1 and 3, and not {option}'
f'Press any key to continue...')
m.getch()
objectRun.play()
except:
print('ERROR AT LINE 126, GO FIND WHAT IS WRONG')
def playerOptions(self):
if self.playerOne == 1:
os.system('cls')
self.drawBoard(topL='X')
elif self.playerOne == 2:
os.system('cls')
self.drawBoard(topM='X')
elif self.playerOne == 3:
os.system('cls')
self.drawBoard(topR='X')
elif self.playerOne == 4:
os.system('cls')
self.drawBoard(midL='X')
elif self.playerOne == 5:
os.system('cls')
self.drawBoard(midM='X')
elif self.playerOne == 6:
os.system('cls')
self.drawBoard(midR='X')
elif self.playerOne == 7:
os.system('cls')
self.drawBoard(lowL='X')
elif self.playerOne == 8:
os.system('cls')
drawBoard(lowM='X')
elif self.playerOne == 9:
os.system('cls')
self.drawBoard(lowR='X')
else:
pass
def options(self):
os.system('cls')
print(f'[1] - Difficult\n'
f'[2] - P\n'
f'[3] - Exit')
try:
option = int(input('I\'ll go with '))
except ValueError as valueError:
print(f'You should choose something between 1 and 3, and not {option}'
f'Press any key to continue...')
m.getch()
objectRun.menuSelection()
self.difficultAI = int(input('[1] - EASY\n'
'[2] - MEDIUM\n'
'[3] - HARD\n'
'I\'ll go with '))
self.playWith = int(input('[1] - NUMBERS\n'
'[2] - STRING\n'
'[3] - BOTH\n'
'I\'ll go with '))
if __name__ == '__main__':
objectRun = TicTacToe()
objectRun.menuSelection()
Store the current board as dictionary within self.
Initialize your board with the default starting characters.
def __init__(self):
self.difficultAI = 1
self.playWith = 1 # 3 = Strings and Numbers
self.playerOne = None
self.PlayerTwo = None
self.board = {'topL':'?', 'topM':'!', 'topR':'?',} #fill in the rest
Your drawBoard would need adjusted like this...
def drawBoard(self):
board = {
'top-L': self.board['topL'],
'top-M': self.board['topM'],
'top-R': self.board['topR'],
And then I would have a separate method for a "move"
def makeMove(self, player, position):
if self.board[position] not in ['X', 'O']:
self.board[position] = player
self.drawboard()
else:
# error statement and send back to make a new move...
In general, I would have separate methods for the main loop of the game vs everything else. Right now you have a lot of things going on inside the main loop when it should be just sending you to different methods of your game like playerOptions, makeMove, drawBoard, etc. until someone wins or ties.
I am making a text based game with my friend for a programming class, we are avoiding using object oriented stuff so please avoid those suggestions. to use the program I would normally do type "help" then I can go in any direction to move like "right", "left", "down" and "up" when I try to use those commands I get an error. this happend after I added the spawnMonster command part
#Connor and Griffin's text based adventure
import os
import random
############################
####---Variable setup---####
#########ADD COMMAND VARIATION############
#########ADD ITEM LISTS#################
commands = 1 #for debuging only please
maxHealth = 100 #Default begin health
health = 100 #Current health
mana = 0 #THERES NO MAGIC
mapS = [5, 5]
objects = {}
color = "0f"
output = "" #this is where whats happening is told to you
level = 1
canMove = 1
playerSym = "P"
# for activeQuests remember that if the value is 0 its not completed and if its 1 its completed
activeQuests = {"Journey To Riverwood": 0}
# Add new quest names above^
commandList = {"help", "legend", "color", "show inv", "quests", "console", "up", "left", "right", "down", "clear", "map"}
#Default inventory
inv = {"apple(s)":2, "shortsword":1, "gold":50,"cloth shirt":1,"pair of cloth pants":1,"pair of shoes":1}
clearedSpaces = []
##### "Name":baseDMG #####
monsters = {"Goblin":1, "Troll":3, "Bear":2, "Giant Spider": 1, "Bandit":1, "Goblin Chief":3}
###########################
###########################
##### Name:lv:monsterSpawnRate #####
zones = {"Forest":[1,90]}
#######################
#---Quest Log Stuff---#
def checkQuest():
for questn in activeQuests:
print("\n------", questn, "------")
if activeQuests[questn] == 0:
print("\nNot Complete")
else:
print("\nComplete")
######Description for quests######
if questn == "Journey To Riverwood":
print("""
Welcome to Connor and Griffins excellent adventure!
try out some of the commands like; help, quests,
color, inv or show inv. now using your new found
commands move to the city of riverwood.\n\n""")
#########################
#########################
############################
###---Scenes/Functions---###
def mapSize(x, y):
global mapS
mapS = [x, y]
####Ads point to map
def addObject(name, x, y, symbol):
objects[name] = [x, y, symbol]
legend[symbol] = name
#### Clears some variables
def roomStart():
global objects
objects = {}
global legend
legend = {"░":"Unknown area"}
global roomName
roomName = "BLANK"
def newArea():
for area in zones:
global spawnChance
spawnChance = zones[area][1]
def spawnMonster():
enemy, DMG = random.choice(monsters)
return enemy
def moveToNewSpace():
rand = random.randint(1,100)
if rand <= spawnChance:
global spawnedMonster
spawnMonster()
###Move player
def changePos(name, newx, newy):
objects[name][0] += newx
objects[name][1] += newy
global clearedSpaces
clearedSpaces.append([objects[name][0],objects[name][1]])
moveToNewSpace()
###First room
def roomBegin():
roomStart()
mapSize(15,10)
global roomName
roomName = "Forest"
newArea()
addObject("Riverwood",10,5,"R")
addObject("Griffin's House",2,2,"G")
addObject("Player",2,3,playerSym) #######Remember to make a "ChangePos" command to change the pos of the player when they move#######
clearedSpaces.append([2,3])
################### MAPPING HERE ##################
def makeMap():
print("\n------"+roomName+"------")
for y in range(mapS[1]):
line = ""
numy = y+1
for x in range(mapS[0]):
numx = x + 1
for place in objects:
if objects[place][:2] == [numx, numy]:
line += objects[place][2]
break
else:
if [numx, numy] in clearedSpaces:
line += " "
else:
line += "░"
print(line)
print("\n----Legend----\n")
for thing in legend:
print(thing + " - " + legend[thing])
############################
############################
#######################
###--- MAIN LOOP ---###
roomBegin()
while 1 == 1:
makeMap()
print("\n\n" + output + "\n\n")
print("\n\nHealth is at ",health,"/",maxHealth)
command = input("Enter action: ")
if command.lower() == "quests":
os.system("cls")
checkQuest()
elif command.lower() == "legend":
os.system("cls")
print("\n----Legend----\n")
for thing in legend:
print(thing + " - " + legend[thing])
elif command.lower() == "help":
os.system("cls")
print("\n\n------HelpMenu------\n")
for comd in commandList:
print(comd)
elif command.lower() == "color":
newc = input("new color: ")
os.system("color 0" + newc)
os.system("cls")
elif command.lower() == "show inv" or command.lower() == "inv" or command.lower() == "inventory":
os.system("cls")
print("\n------Inventory------\n")
for item in inv:
print(" ", inv[item]," ", item)
elif command.lower() == "console":
if commands == 1:
consolecmd = input("Enter a command: ")
os.system(consolecmd)
else:
print("Sorry, you dont have permition to use that command.")
elif command.lower() == "up":
if canMove == 1:
os.system("cls")
changePos("Player", 0,-1)
else:
os.system("cls")
output += "\nCant move that way right now!"
elif command.lower() == "down":
if canMove == 1:
os.system("cls")
changePos("Player", 0,1)
else:
os.system("cls")
output += "\nCant move that way right now!"
elif command.lower() == "left":
if canMove == 1:
os.system("cls")
changePos("Player", -1,0)
else:
os.system("cls")
output += "\nCant move that way right now!"
elif command.lower() == "right":
if canMove == 1:
os.system("cls")
output = "There are some trees here, and a small pond"
changePos("Player", 1,0)
else:
os.system("cls")
output += "\nCant move that way right now!"
elif command.lower() == "clear":
os.system("cls")
else:
os.system("cls")
print("Previous attempt was an invalid command, try again.")
#######END MAIN#######
######################
The random.choice function requires a sequence.
In a previous version of your code (or maybe it was code from a classmate of yours just having similar but not identical problems with similar code?), you had the monsters stored in a list, like [("Goblin", 1), ("Troll", 3), …]. For that code, random.choice works, because a list is a sequence.
But now you have a dict. And a dict is not a sequence.
You can get a list of all of the keys in a dict just by writing list(d). So, you could do this:
return random.choice(list(monsters))
But you're not actually getting any benefit from monsters being a dict from what I can tell, so why not just use a list in the first place?
monsters is a dictionary. It is being passed to random.choice(), however, random.choice() accepts a sequence as its argument (from which it randomly selects one element). This will not work for a dictionary and you will see a KeyError exception if you try.
Since you just want to return an enemy from spawnMonster() you can instead use the keys of monsters which is a list:
monsters = {"Goblin":1, "Troll":3, "Bear":2, "Giant Spider": 1, "Bandit":1, "Goblin Chief":3}
def spawnMonster():
return random.choice(monsters.keys())
Update
Since you are using Python 3 you can do this instead (also works in Python 2):
def spawnMonster():
return random.choice(tuple(monsters))
Alright so what I am trying to do is to get objects to save in list form when a user creates a NoteSet. It appends the objects to the list db properly when I input NoteSet('ex','example',True). I made a function called makeNewNoteSet() and it seems to be working correctly but it doesnt append to the db list. I can not figure out why.
import sys
import datetime
import pickle
notesets = []
db = []
def save():
global db
filename = "notesets.dat"
file = open(filename, "wb")
if file == None:
print("There was an error creating your file")
return
pickle.dump(db, file)
file.close()
print("Saved words to ",filename)
def load():
global db
filename = "notesets.dat"
file = open(filename, "rb")
db = pickle.load(file)
print("There are ",len(db)," Note Sets")
file.close()
class NoteSet:
nextseqNum = len(db)+2
def __init__(self,name,description,hidden):
global db
self.seqNum = NoteSet.nextseqNum
self.name = name
self.description = description
self.dateCreated = datetime.date.today()
self.hidden = hidden
self.notes = list()
NoteSet.nextseqNum += 1
print(self)
notesets.append(self)
notelist = [self.seqNum,self.name,self.description,self.dateCreated,self.hidden,self.notes]
print(notelist)
db.append(notelist)
NoteSet.nextseqNum += 1
def __str__(self):
printstr = str(self.seqNum),self.name,self.description,str(self.dateCreated)
printstr = str(printstr)
return printstr
class Note:
nextseqNum = 0
def __init__(self,text,dateCreated,description,category,priority,hidden):
self.text = text
self.dateCreated = str
self.dateRead = str
self.description = str
self.category = str
self.priority = int
self.hidden = bool
self.seqNum = Note.nextseqNum
Note.nextseqNum += 1
def main():
while True:
load()
printMainMenu()
selection = int(input("? "))
if selection == 1:
listNoteSets()
elif selection == 2:
listAllNoteSets()
elif selection == 3:
makeNewNoteSet()
elif selection == 4:
selectNoteSet() # this makes the working note set
elif selection == 5:
deleteNoteSet()
elif selection == 6:
sys.exit()
else:
print("Invalid choice")
def printMainMenu():
print("1. List note sets")
print("2. List all note sets (including hidden sets)")
print("3. Make a new note set")
print("4. Select a working note set")
print("5. Delete a note set")
print("6. Quit")
def listNoteSets():
num = 0
for row in db:
if db[num][4] == False:
print('#',db[num][0],' ',db[num][1],'----',db[num][2])
num += 1
input("[Press return to see main menu]")
main()
def listAllNoteSets():
num = 0
for row in db:
print('#',db[num][0],' ',db[num][1],'----',db[num][2])
num += 1
input("[Press return to see main menu]")
main()
def makeNewNoteSet():
num = 0
name = input("What would you like to name your note set? --- ")
for row in db:
if name == db[num][1]:
print("That note set has already been created")
makeNewNoteSet()
description = input("Describe your note set briefly --- ")
hidden = input("Would you like this note set to be hidden? --- ")
if hidden == 'y' or 'yes':
hidden = True
else:
hidden = False
NoteSet(name, description, hidden)
print("noteset created you can now access it through the menu")
input("[Press enter to return to menu]")
main()
def selectNoteSet():
num = 0
for row in db:
print('#',db[num][0],' ',db[num][1],'----',db[num][2])
num += 1
response = input("Enter the number assosciated with the noteset you would like to access")
print("Note set #",response," was selected")
main()
After you add a new note in makeNewNoteSet(), you call main() which calls load() which overwrites the in-memory copy of the database you just changed. You probably want to call save() somewhere in there.