(基本)质疑的错误

时间:2017-07-26 05:37:04

标签: python

我对python很新,我试图让这段代码工作。我只能制作基本的节目,我觉得我的表现远远不够!如果您认为我可以采取其他方式,我愿意接受任何建议。这是我的代码:

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'

非常感谢你的帮助!如果您有更好的方法来创建此调查问卷,我愿意接受任何建议!

亲切的问候, 用户

2 个答案:

答案 0 :(得分:0)

更改功能问题中的循环问这样:

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

答案 1 :(得分:0)

首先要了解您遇到的错误。它是由+函数中的print引起的。原因是+运算符不知道如何“添加”dict的实例和str的实例。 我不太确定你在那里做什么,所以如果你解释一下我会更有帮助。

关于你的代码。既然你是Python的初学者,这里有一些基础知识:

  1. 此处不需要全局变量。你可以把所有东西都放在一个(或两个)函数中,然后去掉它们全局。
  2. 字符串格式。如果您只关注this
  3. 会更容易
  4. 也可以使用print()打印换行符。
  5. 下一步 - 课程。如果你想添加问题怎么办?你必须对它们进行硬编码。尽量使你的代码更加通用。
  6. 快速简单的例子:

    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')
    

    要想做得更好,还有很多东西需要学习。祝你好运!