class - Python: 'NoneType' object has no attribute 'get_username' -
i'm working on hangman program has user accounts objects. player can log in, create new account, or view account details, of work fine before playing game. after game has completed, user's wins , losses updated. before exiting program, if try view account (the viewacc function) error:
'nonetype' object has no attribute 'get_username'.
when run the program again, can log in account, when view account info wins , losses haven't been updated. appreciated, have turn in class in 8 hours.
heres class code:
class account: def __init__(self, username, password, name, email, win, loss): self.__username = username self.__password = password self.__name = name self.__email = email self.__win = int(win) self.__loss = int(loss) def set_username (self, username): self.__username = username def set_password (self, password): self.__password = password def set_name (self, name): self.__name = name def set_email (self, email): self.__email = email def set_win (self, win): self.__win = win def set_loss (self, loss): self.__loss = loss def get_username (self): return self.__username def get_password (self): return self.__password def get_name (self): return self.__name def get_email (self): return self.__email def get_win (self): return self.__win def get_loss (self): return self.__loss
and here's program's code:
import random import os import account import pickle import sys #list of images different different stages of being hanged stages = [ ''' ___________ |/ | | | | | | | | | | _____|______ ''' , ''' ___________ |/ | | | | (o_o) | | | | | | _____|______ ''' , ''' ___________ |/ | | | | (o_o) | | | | | | | | _____|______ ''' , ''' ___________ |/ | | | | (o_o) | |/ | | | | | | _____|______ ''' , ''' ___________ |/ | | | | (o_o) | \|/ | | | | | | _____|______ ''' , ''' ___________ |/ | | | | (o_o) | \|/ | | | / | | | _____|______ ''' , ''' dead!!! ___________ |/ | | | | (x_x) | \|/ | | | / \ | | | _____|______ ''' ] #used validate user input alphabet = ['abcdefghijklmnopqrstuvwxyz'] #declares lists of different sized words fourwords = ['ties', 'shoe', 'wall', 'dime', 'pens', 'lips', 'toys', 'from', 'your', 'will', 'have', 'long', 'clam', 'crow', 'duck', 'dove', 'fish', 'gull', 'fowl', 'frog', 'hare', 'hair', 'hawk', 'deer', 'bull', 'bird', 'bear', 'bass', 'foal', 'moth', 'back', 'baby'] fivewords = ['jazzy', 'faker', 'alien', 'aline', 'allot', 'alias', 'alert', 'intro', 'inlet', 'erase', 'error', 'onion', 'least', 'liner', 'linen', 'lions', 'loose', 'loner', 'lists', 'nasal', 'lunar', 'louse', 'oasis', 'nurse', 'notes', 'noose', 'otter', 'reset', 'rerun', 'ratio', 'resin', 'reuse', 'retro', 'rinse', 'roast', 'roots', 'saint', 'salad', 'ruins'] sixwords = ['baboon', 'python',] def main(): #gets menu choice user choice = menu() #initializes dictionary of user accounts file accdct = loadacc() #initializes user's account user = account.account("", "", "", "", 0, 0) while choice != 0: if choice == 1: user = play(user) if choice == 2: createacc(accdct) if choice == 3: user = login(accdct) if choice == 4: viewacc(user) choice = menu() saveacc(accdct) #plays game def play(user): os.system("cls") #clears screen hangman = 0 #used index stage view done = false #used signal when game finished guessed = [''] #holds letters guessed #gets game word lenght user difficulty = int(input("chose difficulty/word length:\n"\ "1. easy: 4 letter word\n"\ "2. medium: 5 letter word\n"\ "3. hard: 6 letter word\n"\ "choice: ")) #validates input while difficulty < 1 or difficulty > 3: difficulty = int(input("invalid menu choice.\n"\ "reenter choice(1-3): ")) #gets random word different list depending on difficulty if difficulty == 1: word = random.choice(fourwords) if difficulty == 2: word = random.choice(fivewords) if difficulty == 3: word = random.choice(sixwords) viewword = list('_'*len(word)) letters = list(word) while done == false: os.system("cls") print(stages[hangman]) in range(len(word)): sys.stdout.write(viewword[i]) sys.stdout.write(" ") print() print("guessed letters: ") in range(len(guessed)): sys.stdout.write(guessed[i]) print() guess = str(input("enter guess: ")) guess = guess.lower() while guess in guessed: guess = str(input("already guessed letter.\n"\ "enter guess: ")) while len(guess) != 1: guess = str(input("guess must 1 letter.\n"\ "enter guess: ")) while guess not in alphabet[0]: guess = str(input("guess must letter.\n"\ "enter guess: ")) if guess not in letters: hangman+=1 in range(len(word)): if guess in letters[i]: viewword[i] = guess guessed += guess if '_' not in viewword: print ("congratulations! correctly guessed",word) done = true win = user.get_win() win += 1 username = user.get_username() password = user.get_password() name = user.get_name() email = user.get_email() loss = user.get_loss() user = account.account(username, password, name, email, win, loss) if hangman == 6: os.system("cls") print() print(stages[hangman]) print("you couldn't guess word",word.upper(),"before being hanged.") print("sorry, lose.") done = true loss = user.get_loss() loss += 1 username = user.get_username() password = user.get_password() name = user.get_name() email = user.get_email() win = user.get_win() user = account.account(username, password, name, email, win, loss) #loads user accounts file def loadacc(): try: ifile = open('useraccounts.txt', 'rb') accdct = pickle.load(ifile) ifile.close except ioerror: accdct = {} return accdct #displays menu def menu(): os.system('cls') print("welcome karl-heinz's hangman") choice = int(input("1. play hangman\n"\ "2. create account\n"\ "3. log in\n"\ "4. view account details\n"\ "0. quit program\n"\ "choice: ")) while choice < 0 or choice > 4: choice = int(input("invalid menu choice.\n"\ "reenter choice: ")) return choice #logs user in existing account def login(accdct): os.system('cls') user = account.account("","","","",0,0) username = str(input("enter username(case sensitive): ")) if username not in accdct: print("account not exist") os.system("pause") return user temp = account.account("","","","",0,0) temp = accdct[username] password = str(input("enter password(case sensitive): ")) if password != temp.get_password(): print("incorrect password.") os.system("pause") return user user = accdct[username] return user #creates new account , new account file if 1 doesn't exist def createacc(accdct): os.system('cls') print("enter account info:") username = str(input("username: ")) if username in accdct: print("account exists.") os.system("pause") return password = str(input("password: ")) name = str(input("name: ")) email = str(input("email: ")) wins = 0 loss = 0 tempuser = account.account(username, password, name, email, wins, loss) accdct[username] = tempuser print("account created.") os.system("pause") def viewacc(user): os.system('cls') print("account details: ") print("username: ",user.get_username()) print("name: ",user.get_name()) print("email: ",user.get_email()) print("wins: ",user.get_win()) print("losses: ",user.get_loss()) os.system("pause") #saves accounts dictionary file def saveacc(accdct): ofile = open("useraccounts.txt", "wb") pickle.dump(accdct, ofile) ofile.close() main()
any appreciated.
your play() function doesn't have return
statement, means it's returning none
return value. that's how you're getting none
user
variable in main(). add return
statement play() function , should fine.