这是我的固定缩进,几乎我正在尝试制作一个程序,其中包含多个“答案”,例如嗡嗡声的人格测验。该程序试图猜测一个人最想去哪里度假。我使用了一种在youtube上发现的模板,该模板可以在只有一个“正确”答案的情况下起作用,因此我尝试使用该模板,除了对其进行格式设置外,这样每个答案都可以增加价值,而具有最大价值的字母将决定用户应该在哪里外出度假。
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
from Question import Question
def main():
print("Hello! This is a quiz that tries to guess where you want to go on vacation most based\n"\
"on the answers you provide. Please respond with either 'a, b, c, or d.' Lets do this!\n")
question_prompts = [
"What is your go-to footwear?\n(a) Flip-flops\n(b) Sneaker\n(c) High-heels\n(d) Boots\n\n",
"What is your favorite season?\n(a) Summer\n(b) Fall\n(c) Winter\n(d) Spring\n\n",
"What is your favorite color?\n(a) Magenta\n(b) Teal\n(c) Yellow\n(d) Green\n\n",
"What is your favorite fruit?\n(a) Watermelon\n(b) Pineapple\n(c) Mango\n(d) Peach\n\n",
"What is your hair color?\n(a) Red\n(b) Blonde\n(c) Black\n(d) Brown\n\n",]
questions = [
Question(question_prompts[0]),
Question(question_prompts[1]),
Question(question_prompts[2]),
Question(question_prompts[3]),
Question(question_prompts[4])]
test = run_test(questions)
def run_test(questions):
score_one = 0
score_two = 1
score_three = 2
score_four = 3
score_five = 4
for question in questions:
answer = input(question.prompt)
if answer == 'a':
score_one += 1
elif answer == 'b':
score_two += 1
elif answer == 'c':
score_three += 1
elif answer == 'd':
score_four += 1
else:
print('Invalid response')
print(question_prompt)
print("Result:", counter(answer))
run_test(questions)
#count total for each option
def counter(answer):
if score_one > score_two and score_one > score_three and score_one > score_four:
print('You should vacation at the beach!')
elif score_two > score_one and score_two > score_three and score_two > score_four:
print('You should vacation at the Swiss Alps!')
elif score_three > score_one and score_three > score_two and score_three > score_four:
print('You should vacation in Paris, France!')
else:
print('You should go on vacation in Alaska!')
main()
答案 0 :(得分:0)
即使您对类的了解不多,也应该了解基础知识,以了解错误的出处。
类是创建特定类型的对象的蓝图。对象是可以用您希望它们表示真实对象的任何方式定义的数据结构。它们保存数据字段,无论它们是int
,str
等,并且在您编写时它们可以具有某些行为类中的>方法(方法只是类中定义的函数)。创建q1 = Question('Enter a/b/c/d', 'b')
之类的对象时,特殊的__init__
被隐式(自动)调用。 self
参数必须是类中每个方法中的第一个参数。特殊之处在于,在创建对象时,您不会在括号中显式传递值。解释器通过将对象本身的引用名称作为第一个参数发送来为您完成此操作。因此,在此示例中,self
是q1
。您代码中的__init__
方法将使用它来设置q1.prompt = 'Enter a/b/c/d'
和q1.answer = b
。
我看到的第一个问题是您正在尝试创建Question
对象,而没有为其提供需要创建的参数。您只需向其传递一个参数,即提示(我假设),但它需要两个参数,即提示和答案(请记住,self
是隐式传递的。)
正如其他人所提到的,如果您在我们进一步帮助您之前解决了缩进错误,将会很有帮助。