Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:';非类型';对象没有属性';标题';_Python_Python 3.x_Error Handling_Attributeerror - Fatal编程技术网

Python 属性错误:';非类型';对象没有属性';标题';

Python 属性错误:';非类型';对象没有属性';标题';,python,python-3.x,error-handling,attributeerror,Python,Python 3.x,Error Handling,Attributeerror,试图运行此代码,但错误表明我在.title中出错。我怎样才能解决这个问题?(请注意,我仍然是Python的业余爱好者,我愿意接受任何可以改进我的代码的建议。但是,如果它是新的东西,例如,你可以使用uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu 这是我的密码: import time import sys print("Wel

试图运行此代码,但错误表明我在.title中出错。我怎样才能解决这个问题?(请注意,我仍然是Python的业余爱好者,我愿意接受任何可以改进我的代码的建议。但是,如果它是新的东西,例如,你可以使用uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

这是我的密码:

import time
import sys

print("Welcome to my quiz")
time.sleep(2)
input('Press enter to continue')

score = int(0)
failed = int(0)

def points():
    print("Correct! Well done!")
    global score
    score = score + 1

def fail():
    print("Oh no, that is incorrect")
    global failed
    failed = failed + 1

def printtotal():
    global score
    global failed
    print("You have ",score,"points, and ",failed,"/3 lives used.")

if failed == 5:
    sys.exit('Program terminated.')

print('You will have 5 seconds to answer each question \nIf you fail 5 times or more, the program will exit')
time.sleep(3)
print('Good luck!')
time.sleep(2)

q1 = print(input("What is the capital of England?")).title
if q1 == 'London':
    points()
else:
    fail()
printtotal()

q2 = print(input("Who is the prime minister's wife?")).title
if q2 == 'Samantha' or 'Samantha Cameron':
    points()
else: 
   fail()
printtotal()

print('How would you say \'I came, I saw, I conquered\' in latin?')
print('a) veni, vidi, vici')
print('b) veni, vedi, vici')
print('c) vini, vedi, vici')
q3 = print(input('Type the letter here:'))
if q3 == 'a)' or 'a':
    points()
else:
    fail()
printtotal()
如果我尝试执行此代码,则会出现以下错误:

Traceback (most recent call last):
  File "/root/Desktop/Python_Stuff/quiz.py", line 34, in <module>
    q1 = print(input("What is the capital of England?")).title
AttributeError: 'NoneType' object has no attribute 'title'
回溯(最近一次呼叫最后一次):
文件“/root/Desktop/Python\u Stuff/quick.py”,第34行,在
q1=打印(输入(“英格兰的首都是什么?”)。标题
AttributeError:“非类型”对象没有属性“标题”

如果有人发现错误,请告诉我。如何修复此错误?

函数不返回任何内容,或者更具体地说,它返回
NoneType
。因此,您无法获取其成员
title
的值

print(input("What is the capital of England?")).title
相当于:

answer = input("What is the capital of England?")
print(answer).title
但是,
print(answer)
只需打印出用户输入的任何内容,然后返回一个None对象。你真正想要的是:

print(input("What is the capital of England?").title())

这将揭示代码中的另一个问题:您试图同时输入某些内容并打印需要输入的问题。在这种情况下:


上述操作将用户输入保存到
q1

错误消息表明此行存在问题:

q1 = print(input("What is the capital of England?")).title
您在这里所做的是请求用户输入(使用python3.x的
input
功能),然后将结果打印到标准输出(通常是屏幕或“终端窗口”)。但是,
print
不返回任何特殊的内容,因此在python术语中,它返回
None
None
是一个没有属性
title
的单例变量(将其视为一种特殊类型的变量),因此不能键入例如
None.title

print(input("What is the capital of England?")).title
考虑只将输入存储到
q1
,如下所示:

q1 = input("What is the capital of England?")
q1 = input("What is the capital of England?")
if q1 == 'London':

print()
返回
None
。因此,
print(…).title
会引发您看到的
AttributeError

print
没有名为“title”的属性。也就是说,你不能这样做:

print().title
实际上,我认为您要做的是完全摆脱
print()
,如下所示:

q1 = input("What is the capital of England?")
q1 = input("What is the capital of England?")
if q1 == 'London':

您需要将第二个右括号移到行的末尾。你也应该做
.title()
,而不是
.title
。在过去的一个小时里,我看到了4个python问题,都有打字错误,我觉得明天在某所大学有一个python作业。他实际上想要的是将输入存储在变量
q1
q2
等中,所以直接打印也是错误的。