繁体   English   中英

如何修复 TypeError:无法解压不可迭代的 NoneType 对象

[英]How can I fix TypeError: cannot unpack non-iterable NoneType object

即使进行了小的修复,我也一遍又一遍地收到此错误,它总是会中断。 我被告知将包含数字的变量更改为字母 ex。 玩家 1 -> 玩家一。 但这仍然没有修复 TypeError。

我已经包含了完整的回溯,以进一步帮助查找其他用户请求的代码问题。

What is your username? User1
What is your password? password
Welcome, User1 you have been successfully logged in.
What is your username? User2
What is your password? password
Welcome, User2 you have been successfully logged in.
After this round User1 you now have: 10 Points
After this round User2 you now have: 9 Points
After this round User1 you now have: 13 Points
After this round User2 you now have: 19 Points
After this round User1 you now have: 16 Points
After this round User2 you now have: 26 Points
After this round User1 you now have: 22 Points
After this round User2 you now have: 37 Points
Traceback (most recent call last):
  File "C:\Users\corn\Desktop\nea\cs nea.py", line 81, in <module>
    main()
  File "C:\Users\corn\Desktop\nea\cs nea.py", line 69, in main
    (playerOne, playerOne_win), (playerTwo, playerTwo_win) = game(userOne, userTwo);
TypeError: cannot unpack non-iterable NoneType object
import random
import time

def login():
    while True:
        username = input('What is your username? ')
        password = input('What is your password? ')
        if username not in ('User1', 'User2', 'User3', 'User4', 'User5'):
            print('Incorrect username or password, try again')
            continue

        if password != 'password':
            print('Incorrect username or password, try again')
            continue

        print(f'Welcome, {username} you have been successfully logged in.')
        return username

def roll():
    dieOne = random.randint(1, 6)
    dieTwo = random.randint(1, 6)
    points = dieOne + dieTwo
    if dieOne == dieTwo:
        points += random.randint(1, 6)
    return points

def game(userOne, userTwo):
    playerOne_points = 0
    playerTwo_points = 0
    for i in range(1,5):
        playerOne_points += roll()
        print(f'After this round {userOne} you now have: {playerOne_points} Points')
        time.sleep(1)
        playerTwo_points += roll()
        print(f'After this round {userTwo} you now have: {playerTwo_points} Points')
        time.sleep(1)

    playerOne_tiebreaker = 0
    playerTwo_tiebreaker = 0
    if playerOne_points == playerTwo_tiebreaker:
        while playerOne_tiebreaker == playerTwo_tiebreaker:
            playerOne_tiebreaker = random.randint(1,6)
            playerTwo_tiebreaker = random.randint(1,6)

def add_winner(winner):
    file = open('Winner.txt', 'a')
    file.write('{winner[0]},{winner[1]}\n')

def get_leaderboard():
    file = open('Leaderboard.txt', 'r')
    return [line.replace('\n','') for line in file.readlines()]

def save_leaderboard(leaderboard):
    with open('Leaderboard.txt', 'w'):
        for item in leaderboard:
            file.write(f'{item}\n')
            file.close(winner)

def main():
    userOne = login()
    userTwo = login()
    (playerOne, playerOne_win), (playerTwo, playerTwo_win) = game(userOne, userTwo);
    if playerOne_win:
        winner = (playerOne, userOne)
    else:
        winner = (playerTwo, userTwo)
    print('Well done, {userOne} you won with {playerOne_points} Points')
    add_winner(winner)
    leaderboard = get_leaderboard()
    save_leaderboard(leaderboard)

if __name__ == '__main__':
    main()

您没有从调用 game(userOne, userTwo) 的 game() 函数返回任何内容,将 NoneType 错误返回:

(playerOne, playerOne_win), (playerTwo, playerTwo_win) = game(userOne, userTwo);

只需在函数末尾添加一行: return (playerOne, playerOne_win), (playerTwo, playerTwo_win)将这些名称替换为该函数内的正确变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM