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

时间:2022-02-23 18:36:57

When I run the following code:

当我运行以下代码时:

 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

I'm getting this traceback:

我得到了这个追溯:

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'

What am I doing wrong? How can I fix the error?

我究竟做错了什么?我该如何修复错误?

1 个解决方案

#1


The error says their player1strength or player2strength (or possibly both) are None, which int() can't take as a parameter. It probably is both, since you are assigning the result of a call to print to each.

错误表明他们的player1strength或player2strength(或可能两者)都是None,int()不能作为参数。它可能都是,因为您要将打印调用的结果分配给每个。

Maybe it does say something about *strength difference`, but since you haven't provided details, I can't respond to that.

也许它确实说了一些关于*强度差异的信息,但由于你没有提供细节,我无法回应。

#1


The error says their player1strength or player2strength (or possibly both) are None, which int() can't take as a parameter. It probably is both, since you are assigning the result of a call to print to each.

错误表明他们的player1strength或player2strength(或可能两者)都是None,int()不能作为参数。它可能都是,因为您要将打印调用的结果分配给每个。

Maybe it does say something about *strength difference`, but since you haven't provided details, I can't respond to that.

也许它确实说了一些关于*强度差异的信息,但由于你没有提供细节,我无法回应。