为什么我的代码会引发TypeError异常?

2024-06-02 14:21:11 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行以下代码时:

 while start == "yes":

    player1name = input("Player 1 what shall your character be called?")
    player2name = input("Player 2 what shall your character be called?")

    player1strength=print("Player 1,your strength score is :)", random.randint(1,7))
    player2strength=print("Player 2,your strength score is :)", random.randint(1,7))
    strengthdifference =(int(player1strength) - int(player2strength))
    if strengthdifference<0:
        strengthdifference=player2strength-player1strength
        strengthdifference=strengthdifference/5
        player1skill=int(input("Player 1,enter your skill score :)"))
        player2skill=int(input("Player 2,enter your skill score :)"))
        skilldifference=player1skill-player2skill

我得到了这个回溯:

File "C:\Computing\A453 Assessment\Task 3\main.py", line 18, in <module>
    strengthdifference =(int(player1strength) - int(player2strength))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

我做错什么了?我怎样才能纠正这个错误?你知道吗


Tags: inputyourbewhatstrengthintscoreplayer
1条回答
网友
1楼 · 发布于 2024-06-02 14:21:11

该错误表示它们的player1strengthplayer2strength(或可能两者)是None,而int()不能将其作为参数。它可能两者,因为您要将调用的结果print分配给每一个。你知道吗

也许它确实提到了“力量差异”,但由于你没有提供细节,我无法对此作出回应。你知道吗

相关问题 更多 >