0

i just started learning python and this is my issue, i am very new to python - i have two python files all in the same folder..

1. question.py

class Question:

    def __init__(self, prompt, answer):
        self.prompt = prompt
        self.answer = answer

    def chkQuestion(self):
        if self.answer != "":
            return True

2. quiz.py

questions_prompts = [
    "what color are apples? \n (a) red \n(b) green \n(c) orange",
    "what color are bananas? \n (a) Teal \n(b) Magenta \n(c) Yellow",
    "what color are strawberries? \n (a) yellow \n(b) red \n(c) blue"
]

questions = []

answers = "bcd"

for i in range(3):
    pos = int(i)-1
    print(i)
    questions.append(Question(questions_prompts[pos], answers[pos]))

print(questions[0].prompt)

when i print the questions list - it shows me all the questions added to the list as expected, but when i try to print out the prompt property of the first question (questions[0]) it gives this error

AttributeError: 'str' object has no attribute 'prompt'

pls what am i doing wrong, how do i push an object to a list and read the properties as expected. the tutorial created new question objects and passed the values manually, but i got curious what if i had 300+ questions, i cant do that manually, so i created the loop and i'm stuck.

5
  • What is answers[pos] ? Commented Mar 28, 2020 at 11:33
  • 2
    1. Why is the list questions not initialized? 2. Why are you indexing questions_prompts and answers with pos which takes the values -1, 0, 1 instead of i which takes the values 0, 1, 2? 3. What is answers? Commented Mar 28, 2020 at 11:34
  • @maurice meyer, sorry i just adjusted the code. Commented Mar 28, 2020 at 11:43
  • 1
    I suggest asking beginner questions on a Discord server. If you ask questions here as a non-professional, all you'll get is mean answers and downvotes. I strongly suggest going to a friendlier community. I can reccommend The Coding Den Discord server, they have seperate channels for all languages and are much friendlier than the people on here. Commented Mar 28, 2020 at 12:26
  • 1
    @Aci thanks soo much, i'd take your advice, doing my best to migrate to python after doing php for years.. but stackoverflow aint friendly.. Commented Mar 28, 2020 at 12:38

2 Answers 2

1

Although it is not clear what you are trying to do, so i assumed from code written:

class Question:
    def __init__(self, prompt):
        self.prompt = prompt

    def execute(self):
        self.answer = input(self.prompt)

    def chkQuestion(self):
        if self.answer != "":
            return True


questions_prompts = [
    "what color are apples? \n (a) red \n(b) green \n(c) orange",
    "what color are bananas? \n (a) Teal \n(b) Magenta \n(c) Yellow",
    "what color are strawberries? \n (a) yellow \n(b) red \n(c) blue"
]

questions = []

for question in questions_prompts:
    q = Question(question)
    q.execute()
    questions.append(q)

print(questions)

for i, q in enumerate(questions):
    print("Answer for question %s was: %s" % (i, q.answer))

Output:

[<__main__.Question object at 0x10e2d67d0>, <__main__.Question object at 0x10e2d6810>, <__main__.Question object at 0x10e2d6850>]
Answer for question 0 was: a
Answer for question 1 was: b
Answer for question 2 was: c
Sign up to request clarification or add additional context in comments.

Comments

0

A working example that you'll need to adapt:

import numpy as np

class Foo:
    def __init__(self, a, b):
        self.a = a
        self.b = b

list_of_foos = list()

a_rand = np.random.rand(100) * 1000 # Random a
b_rand = np.random.rand(100) * 1000 # Random b

for i in range(100): # Generate 100 Objects
    Obj = Foo(a_rand[i], b_rand[i])
    list_of_foos.append(Obj)

print (list_of_foos[20].a)
print (a_rand[20])

In this case, I created a list of objects, and at the end, I print the attribute a from the 20th object which is the same as the 20th element of the list a_rand.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.