0

私はコーディングを学んでいて、授業を補うために個人的なプロジェクトに取り組んでいます。これは、ドイツ語の語彙を覚えるのにも役立つクイズです。今、私はこのこと自体を評価する方法を理解するのに苦労しています. 変更しようとしているコードは次のとおりです。

def dialogue (question,response,score):
    if question == response:
        print ("Correct! ")
        score = 1
    else:
        print ("Correct answer: " + response)
        score = 0
    return score

score = dialogue
currentScore = 0
currentScore = currentScore + score
question = raw_input ("Good morning ")
response = ("guten morgen")
dialogue(question,response,score)
print currentScore

私の完全なエラーは次のとおりです。

Traceback (most recent call last):
   File "C:/Users/Burgess/Desktop/COLLEGE FOLDER/scoreMod.py", line 12, in <module>
   currentScore = currentScore + score
   **TypeError: unsupported operand type(s) for +: 'int' and 'function'**

スコアの定義に関するこのナンセンスはすべて、少し長くなります。モジュールとして動作するように設定することも検討するかもしれません。また、それを変換して % 値のフィードバックを提供したいと考えていますが、これらの問題は自分で処理できると思います。現時点では、コードを複雑にする前に、この問題を修正したいと考えています。

これを理解するのを手伝ってくれる人はいますか?私はフォーラムに潜んでいて、同様のタイトルの別の問題を見つけましたが、私たちの問題に同様の解決策があるとは思いません.

4

3 に答える 3

0

使用している Python のバージョンを教えて、コードにコメントしてください。そのようにコードを書いた理由を説明してください。

これは、Python 2.7 で機能するコメント付きの改訂版です。

def dialogue(question,response): # you are defining dialogue as a function
    # This function checks whether the answer matches the correct answer
    if question.lower() == response.lower(): # .lower() makes it not case sensitive
        print ("Correct! ")
        return 1 # If matches, return 1
    else:
        print ("Correct answer: " + response)
        return 0 # If does not match, return 0

currentScore = 0 # Initial Score

question = raw_input("Good morning: ") #Asking for input
response = "guten morgen" #Correct answer

currentScore += dialogue(question, response) #calling the function with 2 arguments
#adding the returned score to currentScore
"""
currentScore += dialogue(question, response)
is same as
currentScore = currentScore + dialogue(question, response)
"""
print currentScore #returning output

そして、コメントなしのコードは次のとおりです。

def dialogue(question,response): 
    if question.lower() == response.lower():
        print ("Correct! ")
        return 1
    else:
        print ("Correct answer: " + response)
        return 0 

currentScore = 0

question = raw_input("Good morning: ")
response = "guten morgen"

currentScore += dialogue(question, response)
print currentScore
于 2014-09-14T19:26:31.733 に答える
0

最初にこのコードを試してみてください。バージョン 3.X を使用している場合は、raw_input を使用しないでください。2つの文を比較したい場合は、 x.upper() を使用してみてください。

def dialogue (question,response):
    if question == response:
        print ("Correct! ")
        score = 1
    else:
        print ("Incorrect answer: " + response)
        score = 0
    return score
currentScore = 0
question = input("Good morning ")
question='Good morning'
response='guten morgen'
score = dialogue(question,response)
currentScore = currentScore + score
print ('Your score:',(currentScore))
于 2014-09-14T19:27:49.320 に答える