2016-03-23 25 views
-2

このコードを実行しようとしましたが、エラーは私が.titleで間違っていると言います。これをどうすれば解決できますか? (私はまだPythonの世界では素人ですが、コードを改善できる提案はありませんが、説明されていない限り理解できない可能性があります。 __________使用の代わりに、_________)AttributeError: 'NoneType'オブジェクトに属性 'title'がありません

ここに私のコードです:。

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' 

誰もがもう見つけてしまった場合エラー、教えてください。このエラーを修正するにはどうすればよいですか?

+0

は、あなたがに二閉じ括弧を移動する必要があります。

print().title 

は本当に私はあなたがこのようなので、完全にprint()を取り除くされて何をしたいと思います:それはあなたが行うことはできませんラインの終わり。 '.title'ではなく' .title() 'もやってください。 – zondo

+2

私は過去1時間に4つのpython質問のようなタイプミスがあるのを見ましたが、明日は一部の大学でpythonの割り当てがあるようです。 –

答えて

1

print関数は何も返しません。具体的には、NoneTypeを返します。したがって、あなたはそのメンバーの価値を得ることができませんtitle

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

は同等です:

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

しかしprint(answer)単にユーザーがを入力し、その後なしオブジェクトを返すものは何でもプリントアウトします。あなたが実際に欲しいのです:

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

See this for more about print and title.

これはあなたのコードで別の問題が発見されます:あなたが同時に入力何かしようとしていることを、あなたが入力したい質問を印刷します。その場合this is what you wantに:

q1 = input("What is the capital of England?") # This will automatically print 'What is...' when it reaches this point in the program 

上記q1へのユーザ入力を保存します。

+0

彼が実際に望んでいるのは、入力を変数 'q1'、' q2'などに格納することです。したがって、完全な出力も間違っています。 –

0

エラーメッセージは、問題がこのラインであることを示しています

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

あなたは(python3.xのinput機能付き)、ユーザの入力を求め、その後、その結果を印刷している、ここで何をやっています標準出力(通常、それは画面または "端末ウィンドウ"です)に移動します。しかし、printは特別なものを返さないので、Pythonの用語ではNoneを返します。 Noneは、属性titleを持たないシングルトン(特殊な変数とみなされます)です。 None.title

q1 = input("What is the capital of England?") 
0

print()戻りNone

はちょうどこのように、q1に入力を保存することを検討してください。したがって、表示されているAttributeErrorprint(...).titleになります。

0

printには、「タイトル」という属性がありません。

q1 = input("What is the capital of England?") 
if q1 == 'London': 
関連する問題