Skip to main content
 首页 » 编程设计

Python: 'NoneType' 对象没有属性 'get_username'

2025年05月04日6telwanggs

我正在开发一个也有用户帐户对象的刽子手程序。玩家可以登录、创建新帐户或查看帐户详细信息,所有这些都可以在玩游戏之前正常工作。游戏结束后,更新用户的输赢。在退出程序之前,如果我尝试查看帐户(viewAcc 函数),我会收到错误消息:

'NoneType' object has no attribute 'get_username'. 

当我再次运行程序时,我可以登录账户,但是当我查看账户信息时,输赢没有更新。任何帮助将不胜感激,我必须在大约 8 小时内将其上课。

类代码如下:

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 

这是我的程序代码:

import random 
import os 
import Account 
import pickle 
import sys 
#List of images for different for different stages of being hanged 
STAGES = [ 
''' 
      ___________ 
     |/         | 
     |          | 
     |          
     |          
     |           
     |          
     | 
     | 
     | 
_____|______ 
''' 
, 
''' 
      ___________ 
     |/         | 
     |          | 
     |        (o_o) 
     |          
     |           
     |          
     | 
     | 
     | 
_____|______ 
''' 
, 
''' 
      ___________ 
     |/         | 
     |          | 
     |        (o_o) 
     |          |  
     |          | 
     |          
     | 
     | 
     | 
_____|______ 
''' 
, 
''' 
      ___________ 
     |/         | 
     |          | 
     |        (o_o) 
     |          |/  
     |          | 
     |           
     | 
     | 
     | 
_____|______ 
''' 
, 
''' 
      ___________ 
     |/         | 
     |          | 
     |        (o_o) 
     |         \|/  
     |          | 
     |            
     | 
     | 
     | 
_____|______ 
''' 
, 
''' 
      ___________ 
     |/         | 
     |          | 
     |        (o_o) 
     |         \|/  
     |          | 
     |         /  
     | 
     | 
     | 
_____|______ 
''' 
, 
''' 
    YOU DEAD!!! 
      ___________ 
     |/         | 
     |          | 
     |        (X_X) 
     |         \|/  
     |          | 
     |         / \  
     | 
     | 
     | 
_____|______ 
''' 
] 
 
#used to 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 from user 
    choice = menu() 
 
    #Initializes dictionary of user accounts from 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 the game 
def play(user): 
 
    os.system("cls") #Clears screen 
    hangman = 0      #Used as index for stage view 
    done = False    #Used to signal when game is finished 
    guessed = ['']   #Holds letters already guessed 
 
    #Gets the game word lenght from the user 
    difficulty = int(input("Chose Difficulty/Word Length:\n"\ 
                           "1. Easy: Four Letter Word\n"\ 
                           "2. Medium: Five Letter Word\n"\ 
                           "3. Hard: Six Letter Word\n"\ 
                           "Choice: ")) 
    #Validates input                  
    while difficulty < 1 or difficulty > 3: 
        difficulty = int(input("Invalid menu choice.\n"\ 
                               "Reenter Choice(1-3): ")) 
 
    #Gets a random word from a 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]) 
        for i in range(len(word)): 
            sys.stdout.write(viewWord[i]) 
            sys.stdout.write(" ") 
        print() 
        print("Guessed Letters: ") 
        for i 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 that letter.\n"\ 
                              "Enter another guess: ")) 
 
        while len(guess) != 1: 
            guess = str(input("Guess must be ONE letter.\n"\ 
                              "Enter another guess: ")) 
 
        while guess not in ALPHABET[0]: 
            guess = str(input("Guess must be a letter.\n"\ 
                     "Enter another guess: ")) 
 
        if guess not in letters: 
            hangman+=1 
 
        for i in range(len(word)): 
            if guess in letters[i]: 
                viewWord[i] = guess 
 
        guessed += guess 
 
        if '_' not in viewWord: 
            print ("Congratulations! You 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 the word",word.upper(),"before being hanged.") 
            print("Sorry, you 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 from file 
def loadAcc(): 
    try: 
        iFile = open('userAccounts.txt', 'rb') 
 
        accDct = pickle.load(iFile) 
 
        iFile.close 
 
    except IOError: 
        accDct = {} 
 
    return accDct 
 
#Displays the menu         
def menu(): 
    os.system('cls') 
    print("Welcome to 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 to 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 does 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 a new account and a new account file if one doesn't exist 
def createAcc(accDct): 
    os.system('cls') 
    print("Enter account info:") 
    username = str(input("UserName: ")) 
 
    if username in accDct: 
        print("Account already 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 to file 
def saveAcc(accDct):     
    oFile = open("userAccounts.txt", "wb") 
 
    pickle.dump(accDct, oFile) 
 
    oFile.close() 
 
 
main() 

非常感谢任何帮助。

请您参考如下方法:

您的 play() 函数没有 return 语句,这意味着它返回 None 作为其返回值。这就是如何将 None 设置到 main() 中的 user 变量中。将 return 语句添加到您的 play() 函数,您应该没问题。