2016-09-06 8 views
-2

私は農業に関する基本的な3つの質問クイズを作成する必要があります。それはあなたがそれを正しいか間違っているかどうかを出力する3つの質問を尋ねる必要があり、あなたが間違っている場合は、再度試すことができます。スコア関数も必要です。私は仕様書の質問と誤った/正しい部分を完成させましたが、何を試してもスコア機能を働かせることはできません。私が試してみました:クイズスコアリング機能が必要

score = 0 

def counter(score) 
    score = score + 1 

def counter(score) 
    score = 0 
    score = score + 1 

def counter(score) 
    global score 
    score = 0 
    score = score + 1 

、その後、答えが正解だったら、ラインが読むことを次のよう

counter(score) 

を私もその後、

score = score + 1 

score = 0 

を試してみました何も働いていないし、私は何が間違っているのかを明らかにしてください。また、ユーザーが最後に何人取得したかを印刷する必要があります。

CODE:

score = 0 

def quiz(): 
    print("Here is a quiz to test your knowledge of farming...") 
    print() 
    print() 
    print("Question 1") 
    print("What percentage of the land is used for farming?") 
    print() 
    print("a. 25%") 
    print("b. 50%") 
    print("c. 75%") 
    answer = input("Make your choice: ") 
    if answer == "c": 
     print("Correct!") 
     score = score + 1 
    else: 
     print("Incorrect.") 
     answer = input("Try again! ") 
     if answer == "c": 
      print("Correct") 
      score = score + 1 
     else: 
      print("Incorrect! Sorry the answer was C.") 
    print() 
    print() 
    print("Question 2") 
    print("Roughly how much did farming contribute to the UK economy in 2014.") 
    print() 
    print("a. £8 Billion.") 
    print("b. £10 Billion.") 
    print("c. £12 Billion.") 
    answer = input("Make your choice: ") 
    if answer == "b": 
     print("Correct!") 
     score = score + 1 
    else: 
     print("Incorrect.") 
     answer = input("Try again! ") 
     if answer == "b": 
      print("Ccrrect!") 
      score = score + 1 
     else: 
      print("Incorrect! Sorry the answer was B.") 
    print() 
    print() 
    print("Question 3.") 
    print("This device, which was invented in 1882 has revolutionised farming. What is it called?") 
    print() 
    print("a. Tractor") 
    print("b. Wagon.") 
    print("c. Combine.") 
    answer == input("Make your choice. ") 
    if answer == "a": 
     print("Correct!") 
     score = score + 1 
    else: 
     print("Incorrect.") 
     answer == input("Try again! ") 
      if answer == "a": 
       print("Correct!") 
       score = score + 1 
      else: 
       print("Incorrect! Sorry the answer was A.") 

print("You got {0}/3 right!".format(score)) 
+4

「def」の下のすべてをインデントする必要があります。 – elethan

+2

ヒント:[関数は何かを返すことができます](https://learnpythonthehardway.org/book/ex21.html)。 – LaundroMat

+0

Pythonコードを投稿する場合は、インデントを正確に再現してください。ひどくインデントされたPythonコードはナンセンスです。 – khelwood

答えて

1

N00B(と作業)方法はglobalキーワードは、あなたがあなたの外を宣言したグローバル変数(を使用しています

score = 0 

def quiz(): 
    global score 

ような何かをすることです機能quiz)。あなたのコードを正しく字下げしていないので、quiz関数の内部または外部の最後の文を印刷しているかどうかは不明ですが、それは関係ありません。 scoreとクイズ機能の両方または両方の外側に印刷すると、外に出ても問題ありません。宿題で幸運を祈る!

0

したがって、pythonスコープはインデントで定義されています(上記のコメントのとおり)。したがって、スコープのレイヤーを作成するステートメントはインデントで区切られます。例には、クラス、関数、ループ、および論理ステートメントが含まれます。これはその例です。

Class A: 
    def __init__(self, msg): 
     self.message = msg 
    def print_msg(self): # prints your message 
     print self.message 
    def is_hi(self): 
     if self.message == "hi": 
      return true 
     else: 
      return false 

これは、存在する可能性のあるさまざまなスコープのレイヤーを示しています。 関数を定義していて、そのスコープに何も入れていないため、あなたのコードは現在動作していません。そのエラーのために関数の範囲内に何かを入れなければならないでしょうが、それはおそらくあなたが探しているものではありません。

0
import sys 
def quiz(): 
    score = 0 
    print("Here is a quiz to test your knowledge of farming...") 
    print() 
    print() 
    print("Question 1") 
    print("What percentage of the land is used for farming?") 
    print() 
    print("a. 25%") 
    print("b. 50%") 
    print("c. 75%") 
    answer = input("Make your choice: ") 
    if answer == "c": 
     print("Correct!") 
     score = score + 1 
    else: 
     print("Incorrect! Sorry the answer was C.") 
    print() 
    print() 
    print("Question 2") 
    print("Roughly how much did farming contribute to the UK economy in 2014.") 
    print() 
    print("a. £8 Billion.") 
    print("b. £10 Billion.") 
    print("c. £12 Billion.") 
    answer = input("Make your choice: ") 
    if answer == "b": 
     print("Correct!") 
     score = score + 1 
    else: 
     print("Incorrect! Sorry the answer was B.") 
    print() 
    print() 
    print("Question 3.") 
    print("This device, which was invented in 1882 has revolutionised farming. What is it called?") 
    print() 
    print("a. Tractor") 
    print("b. Wagon.") 
    print("c. Combine.") 
    answer = input("Make your choice: ") 
    if answer == "a": 
     print("Correct!") 
     score = score + 1 
    else: 
     print("Incorrect! Sorry the answer was A.") 
    print("You got {}/3 right!".format(score)) 
def main(): 
    quiz(); 
if __name__ == "__main__": 
    main() 

だから私はこれがあなたの役に立てば幸い、あなたのコードと、このコードを比較して、私が行った変更を参照してください。私が行く前にいくつか:

  1. '='と '=='の違いを知っています。 '='は代入演算子です。変数をある値に設定しています。 '=='はテスト目的で使用されます。 if 5 == 3 + 2:print 'True' if文にこの問題があることにお気づきになりました。コード:あなたは 'main()の場合は == "メイン"' を持っている場合

  2. 最後の主な機能は、常に実行されます。

関連する問題