2017-07-26 7 views
1

私は非常にPythonに新しく、このコードを動作させようとしています。私は基本的なプログラムしか作ることができず、私は自分の深みからうまくいっていると感じています!もし私がこれを行うことができる別の方法があると思うなら、私はどんな提案にも開放的です。(基本)questionareのエラー

import time 
right = 0 
again = 0 
name = 0 
wrong = 0 
questionNumber = 0 
questions = [ 
    {'text': 'What is the population of New Zealand?', 
    'choices': ('6.7m', '3.2m', '5.1m', '4.5m'), 
    'answer': {'D', '4.5M'}, 
    }, 
    {'text': 'What year did the first european set foot on ' 
    'New Zealand? (Abel Tasman)', 
    'choices': ('1830', '1543', '1642', '1765'), 
    'answer': {'C', '1642'}, 
    }, 
    {'text': 'How High is Mt Cook New Zealand?', 
    'choices': ('3,724m', '4,272m', '3,893m', '2,280m'), 
    'answer': {'A', '3724'}, 
    }, 
    {'text': 'What is the biggest lake in New Zealand?', 
    'choices': ('Taupo', 'Te Anau', 'Wanaka', 'Wakatipu'), 
    'answer': {'A', 'Taupo'}, 
    }, 
    {'text': 'What Percentage of New Zealands population are Maori?', 
    'choices': ('25%', '45%', '10%', '15%'), 
    'answer': {'D', '15'}, 
    }, 

] 

print("Please Enter Your Name") 
name = input() 

def questionAsk(question): 
    global right, wrong, questions, questionNumber, name 
    print(question['text']) 
    for i in questions: 
     print(chr(i+ord('A')) + ': ', choice) 

    return input('> ').upper() in question['answer'] 

for question in questions: 
     if questionAsk(question): 
      print ("Right") 
      right = right + 1 
      questionNumber = questionNumber + 1 
      print() 
      print("So far",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions") 
      print() 
     else: 
      print ("Wrong") 
      wrong = wrong + 1 
      questionNumber = questionNumber + 1 
      print() 
      print("So far",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions") 
      print() 
print("Well done",name,"! you have completed my Questionare! You have got",right,"Out of a possible 10 questions correct and",wrong,"You have completed",questionNumber,"Questions!") 
print() 
print() 
print("Do you want to play again?") 
print("Y: Yes") 
print("N: No") 
again = input() 

そして相続エラー:ここに私のコードがあるあなたの助けのための

Traceback (most recent call last): 
    File "C:/Users/Jason/Downloads/Python.py", line 44, in <module> 
    if questionAsk(question): 
    File "C:/Users/Jason/Downloads/Python.py", line 39, in questionAsk 
    print(chr(i+ord('A')) + ': ', choice) 
TypeError: unsupported operand type(s) for +: 'dict' and 'int' 

本当にありがとうございました!あなたがこのアンケートを作成するより良い方法を持っているなら、私はどんな提案にもオープンしています!

種類よろしく、 ユーザー関数questionAskでループの

答えて

0

変更するには、この方法:あなたが持っているエラーに関する

for i,choice in enumerate(question['choices']): 
    print(chr(i+ord('A')) + ': ', choice) 
+0

回答の正確さに完全に同意しますが、OPが何が起こっているのかを理解するために、少し詳しく説明してください。 – JohanL

0

まず。これは、print機能の+によって発生します。その理由は、+オペレータは、dictのインスタンスとstrのインスタンスを「追加」する方法を知らないためです。 私はあなたが何をしようとしているのかよくわからないので、説明すればもっと助けになるかもしれません。

第2のコードについてあなたはPythonの初心者ですから、いくつかの基本があります:

  1. グローバル変数の必要はありません。ここにすべてのものを1つ(または2つ)の関数に入れて、それらのグローバルを取り除くことができます。
  2. 文字列の書式設定。 thisに従うだけで簡単になります。
  3. これは、改行を印刷するためだけにprint()を使用するために使用されます。
  4. 次のステップ - クラス。質問を追加する場合はどうなりますか?それらをハードコーディングする必要があります。あなたのコードをより汎用性のあるものにしてください。

迅速かつ簡単な例:

class Question(object): 

    def __init__(self, text, options, ans): 
     self.text = text 
     self.options = options 
     self.answer = ans 

    def ask(self): 
     print(self.text) 
     for i, opt in enumerate(self.options): 
      print('%d: %s' % (i, opt)) 

    def check_ans(self, ans): 
     if answer not in (1, 2, 3, 4): 
      print('invalid answer') 
     else: 
      if ans == self.answer: 
       print('correct!') 
      else: 
       print('wrong answer') 

これを改善するために学ぶことは多くのがあります。がんばろう!

+1

私はある程度あなたに同意しなければなりません。ここではクラスは明らかに必要ありません。少なくともプログラムがより多くの質問を処理するためではない。それはクラスなしで簡単にやります。クラスを追加することは、このような小さなプログラムではほとんど利益を得ることができないほど複雑になります。 Pythonでは、クラスを使用するのはまったく問題ありません。別の無関係なメモは、あなたの答えのチェックがOPのようにインテリジェントではないということです。正しい答えは、文字「A」〜「D」、または実際の文字列が正しい値になります。 – JohanL

+0

レビューをいただき、ありがとうございます。チェックの答えはOPの意図通りではありませんが、これは単純化されたバージョンなので、私は正確であることを目指しませんでした。そして確かに、関数を使ってさらに多くの質問を作成することができます。質問を印刷(質問)すると 'q.ask()'よりも少し複雑になります。たぶん、初心者のためには、関数を使う方がいいでしょう。 – nutmeg64

関連する問題