首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“NoneType”错误

“NoneType”错误
EN

Stack Overflow用户
提问于 2016-12-02 21:46:17
回答 1查看 761关注 0票数 1

我是Python新手,我看过其他代码,但不知道如何从我的代码中删除非类型错误,

代码语言:javascript
复制
tutorial = input("Do you want to do the tutorial? 1 is yes and 0 is no, this will be the same for everything")
if tutorial <1:
    print("That isn't valid")
if tutorial>0:
    print("That isn't valid")
if tutorial == 1:
    print("In battle you can use either sword, magic or items. Sword damage will be between your minimum and maximum damage, magic will be constant and can be used to heal or attack, items can be used to boost attack, restore health or armour")

我知道这个错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "<string>", line 250, in run_nodebug
  File "C:\Users\Bas\Desktop\Game.py", line 50, in <module>
    tutorial = input("Do you want to do the tutorial? 1 is yes and 0 is no, this will be the same for everything")
TypeError: 'NoneType' object is not callable

这是完整的代码:

代码语言:javascript
复制
playerhealth = 100
playerlow = 10
playerhigh = 30
playerarmour = 50
playermagic = 20
level = 0
xptonextlevel = 200
itemuse = 0

rookhealth = 100
rooklow = 10
rookhigh = 20
rookarmour = 100

medichealth = 150
mediclow = 10
medichigh = 30
medicarmour = 50

rushhealth = 20
rushlow = 10
rushhigh = 10
rusharmour = 30

drillerhealth = 30
drillerlow = 50
drillerhigh = 80
drillerarmour = 20

dragonhealth = 200
dragonlow = 150
dragonhigh = 250
dragonarmour = 150
dragonmagic = 100
dragonl = 45

godhealth = 300
godlow = 250
godhigh = 350
godarmour = 250
godmagic = 200
godl = 50

magehealth = 100
magearmour = 0
magemagic = 50
magel = 10

tutorial = input("Do you want to do the tutorial? 1 is yes and 0 is no, this will be the same for everything")
if tutorial <1:
    print("That isn't valid")
if tutorial>0:
    print("That isn't valid")
if tutorial == 1:
    print("In battle you can use either sword, magic or items. Sword damage will be between your minimum and maximum damage, magic will be constant and can be used to heal or attack, items can be used to boost attack, restore health or armour")

se = input("Input 1 for story or 0 for endless")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-02 22:25:42

您将得到此错误,因为标识符input已被分配为值None。在您的代码中的某个位置,一定已经执行了以下行。

代码语言:javascript
复制
input = None

NoneType是单例常量None的类型。如果尝试这样做,您将得到相同的错误:

代码语言:javascript
复制
>>> None("Do you want to do the tutorial?")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

Python并不阻止您用新值覆盖内置函数。有时候,重新定义函数print是有用的。但是在正常情况下,使用inputlistint等变量名是个坏主意,因为这可能会导致像这个这样的错误。

您可以通过使用字典而不是声明一个非常长的变量列表来减少意外覆盖标识符的可能性。

代码语言:javascript
复制
gamestate = {
    'playerhealth': 100,
    'playerlow': 10,
    'playerhigh': 30,
    'input': None,
    'else': 42,
    ...

您可以使用任何字符串作为字典键,而不必担心隐藏内置。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40941322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档