Python:';非类型';对象没有属性';获取用户名';

Python:';非类型';对象没有属性';获取用户名';,python,class,python-3.x,attributeerror,nonetype,Python,Class,Python 3.x,Attributeerror,Nonetype,我正在开发一个hangman程序,它也有用户帐户对象。玩家可以登录、创建新帐户或查看帐户详细信息,所有这些都可以在玩游戏之前正常工作。游戏完成后,将更新用户的赢和输。在退出程序之前,如果我尝试查看帐户(viewAcc功能),我会收到以下错误: 'NoneType' object has no attribute 'get_username'. 当我再次运行该程序时,我可以登录该帐户,但当我查看帐户信息时,赢家和输家尚未更新。如果有任何帮助,我将不胜感激,我必须在大约8小时内把这个交给课堂 这是

我正在开发一个hangman程序,它也有用户帐户对象。玩家可以登录、创建新帐户或查看帐户详细信息,所有这些都可以在玩游戏之前正常工作。游戏完成后,将更新用户的赢和输。在退出程序之前,如果我尝试查看帐户(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()
随机导入
导入操作系统
进口帐户
进口泡菜
导入系统
#不同被悬挂阶段的不同图像列表
阶段=[
'''
___________
|/         |
|          |
|         
|         
|          
|         
|
|
|
_____|______
'''
,
'''
___________
|/         |
|          |
|(欧欧)
|         
|          
|         
|
|
|
_____|______
'''
,
'''
___________
|/         |
|          |
|(欧欧)
|          | 
|          |
|         
|
|
|
_____|______
'''
,
'''
___________
|/         |
|          |
|(欧欧)
|          |/ 
|          |
|          
|
|
|
_____|______
'''
,
'''
___________
|/         |
|          |
|(欧欧)
|         \|/ 
|          |
|           
|
|
|
_____|______
'''
,
'''
___________
|/         |
|          |
|(欧欧)
|         \|/ 
|          |
|         / 
|
|
|
_____|______
'''
,
'''
你死了!!!
___________
|/         |
|          |
|(X_X)
|         \|/ 
|          |
|         / \ 
|
|
|
_____|______
'''
]
#用于验证用户输入
字母表=['abcdefghijklmnopqrstuvwxyz']
#声明不同大小单词的列表
fourWords=['ties'、'shoe'、'wall'、'dime'、'pens'、'lips'、'toys'、'from'、'your'、'will'、'have'、'long'、'clam'、'crow'、'duck'、'dove'、'fish'、'鸥'、'fowl'、'frog'、'hare'、'hair'、'hawk'、'deer'、'
五个字=['jazzy','faker','aline','allot','aline','alias','alert','intro','inlet','erase','error','onion','least','liner','Lines','lions','loose','loner','lists','Nasia','lunar','lunar','louse','oasis','nurse','notes','noose','otter reset','rerun ratio','Res','Res Rear','']
sixwords=['狒狒','蟒蛇',]
def main():
#从用户获取菜单选项
选项=菜单()
#从文件初始化用户帐户的字典
accDct=loadAcc()
#初始化用户的帐户
用户=帐户。帐户(“,”,“,”,0,0)
而选项!=0:
如果选项==1:
用户=播放(用户)
如果选项==2:
createAcc(accDct)
如果选项==3:
用户=登录(accDct)
如果选项==4:
viewAcc(用户)
选项=菜单()
saveAcc(accDct)
#玩游戏
def播放(用户):
操作系统(“cls”)#清除屏幕
hangman=0#用作舞台视图的索引
完成=错误#用于在游戏结束时发出信号
猜测=['']#保存已猜测的字母
#从用户处获取游戏单词长度
难度=int(输入(“选择的难度/字长:\n”\
“1.简单:四个字母的单词\n”\
“2.中等:五个字母的单词\n”\
“3.硬:六个字母的单词\n”\
“选择:”)
#验证输入
难度<1或难度>3时:
难度=int(输入(“无效的菜单选择”。\n”\
“重新输入选项(1-3):”)
#根据难度从不同的列表中获取随机单词
如果难度==1:
单词=随机。选择(四个单词)
如果难度=2:
单词=随机。选择(五个单词)
如果难度=3:
单词=随机。选择(六个单词)
viewWord=列表(单词)
字母=列表(单词)
完成时==False:
操作系统(“cls”)
印刷品(舞台[刽子手])
对于范围内的i(len(word)):
sys.stdout.write(viewWord[i])
sys.stdout.write(“”)
打印()
打印(“猜测的字母:”)
对于范围内的i(len(猜测)):
sys.stdout.write(猜测[i])
打印()
guess=str(输入(“输入guess:”)
guess=guess.lower()
在猜测中猜测:
guess=str(输入(“已经猜到了那个字母。\n”\
“输入另一个猜测:”)
而len(猜测)!=1:
guess=str(输入(“guess必须是一个字母。\n”\
“输入另一个猜测:”)
虽然猜测不是字母表[0]:
guess=str(输入(“guess必须是字母。\n”\
“输入另一个猜测:”)
如果猜测不是用字母表示的:
刽子手+=1
对于范围内的i(len(word)):
如果用字母猜[i]:
猜一猜
猜
如果viewWord中没有“\u1”:
打印(“恭喜!你猜对了”,word)
完成=正确
win=user.get_win()
赢+=1
username=user.get_username()
password=user.get_password()
name=user.get_name()
email=user.get_email()
损耗=用户。获取_损耗()
user=Account.Account(用户名、密码、姓名、电子邮件、赢、输)
如果刽子手==6:
操作系统(“cls”)
def play(user):
    # ....

    return user