#๐Ÿ”’ Questions & Answers project

61 messages ยท Page 1 of 1 (latest)

ruby ploverBOT
#

@leaden helm

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

leaden helm
#

""questions = [
{
"prompt": "Which langauge is primarily spoken in Germany?",
"options": ["A. French","B. German", "C. English", "D.Spanish"],
"answer": "B"
},
{
"prompt":"What is the capital of Alaska? ",
"options": ["A. Junaeu", "B. Fairbanks","C. Anchorage", "D. Dutch Harbor"],
"answer": "B"
},
{
"prompt": "What is an even number?",
"options": ["A. 23", "B. 93", "C. 19", "D. None of the above"],
"answer": "D"
}
]

def run_quiz(question):
score = 0
for question in questions: print(question["prompt"])

    for option in question["options"]:
        print(option)
    answer = input("Enter your answer(A. B. C. D.): ")

if answer == questions["answer"]:
        
        print("Correct, Great Job!""\n")
        answer += 1

else:
    print("Wrong answer", questions["answer"], "\n")

print(f"You Got{score} out of {len(questions)} questions Correct")

run_quiz(questions)""

stiff ice
#

!code

ruby ploverBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

stiff ice
#

And what's your question?

leaden helm
#

I get an error message when I try and run it

#

let me try and run it again

#

forgot what error message it was

#

TypeError List indices must be intergers or slices not str on ""if answer == questions["answer"]:""

stiff ice
#

I'm assuming you meant question["answer"]?

leaden helm
#

also how do I do the paste thing

stiff ice
#

!paste

ruby ploverBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

leaden helm
stiff ice
#

Did you try the suggestion I made above?

#

And post all code and errors as text.

leaden helm
#

TypeError: list indices must be integers or slices, not str
Questions & Answers.py, line 28
Python was asked to do an operation with an object which doesn't support it.
Did you expect another type?
Step through your program to see why this type appears here.
Maybe you forgot some details about this operation?
Look up the documentation or perform a web search with the error message.

Warnings
May help you find the cause of the error.
Questions & Answers.py
Line 21 : Redefining argument with the local name 'question'
Did you notice that an argument gets overwritten here?
Line 28 : No overload variant of "getitem" of "list" matches argument type "str" [call-overload]
Line 28 : Possible overload variants:
Line 28 : def getitem(self, SupportsIndex, /) -> dict[str, Sequence[str]]
Line 28 : def getitem(self, slice, /) -> list[dict[str, Sequence[str]]]
Line 30 : Bad indentation. Found 12 spaces, expected 8
It looks like the unexpected number of indentation's tabulations or spaces has been found.
Line 30 : Implicit string concatenation found in call
String literals are implicitly concatenated in a literal iterable definition : maybe a comma is missing ?
Line 31 : Unsupported operand types for + ("str" and "int") [operator]
Line 31 : Bad indentation. Found 12 spaces, expected 8
It looks like the unexpected number of indentation's tabulations or spaces has been found.
Line 34 : No overload variant of "getitem" of "list" matches argument type "str" [call-overload]
Line 34 : Possible overload variants:
Line 34 : def getitem(self, SupportsIndex, /) -> dict[str, Sequence[str]]
Line 34 : def getitem(self, slice, /) -> list[dict[str, Sequence[str]]]

#

I feel like a damn idiot

#

been like forever since I've done Python

leaden helm
#

And it didn't work

stiff ice
#

Well, it would work. That would fix at least one problem. What's the traceback of the new error?

#

The screenshot you sent still has the problem.

leaden helm
#

I'm sorry I'm not much of a programmer like I was back in HS

#

File "C:\Users\brown\PycharmProjects\PythonProject\Questions & Answers.py", line 31, in run_quiz
answer += 1
TypeError: can only concatenate str (not "int") to str

stiff ice
#

answer is a string (text).

#

It's the user's guess.

#

Why are you adding 1 to it?

leaden helm
#

Was trying to do a small project a simple one Q&A game

#

But I guess I'm still a relearner

stiff ice
#

Yes, but think about that line carefully. Why add 1 to the user's guess? What is your intent with that line?

leaden helm
#

It's a point system

#

Wait

stiff ice
#

No. Is answer holding the points?

leaden helm
#

No

stiff ice
#

So, if it's not holding the points, why are you adding points to it? What is holding the points?

leaden helm
#

Wait my smooth brain isn't corporating this morning

#

It is holding points

stiff ice
#

answer is holding the user's guess.

leaden helm
#

I'm definitely not a morning person nor a smart programmer

#

Gimme some time

stiff ice
#

You just don't appear to be thinking the code through. Look at what variables you have and think about what their purposes are.

leaden helm
stiff ice
#

There you go. The answer == 1 is useless, but the score += 1 looks correct.

leaden helm
#

I've got more error messages

still kite
#

bro you made so many simple mistakes

leaden helm
#

Yes

#

I'm built on mistakes and slowly learning

#

Smooth brain.exe is working overtime

still kite
#

if condition ,
it should be if answer == question["answer"]:

#
questions = [
    {
        "prompt": "Which langauge is primarily spoken in Germany?",
        "options": ["A. French","B. German", "C. English", "D.Spanish"],
        "answer": "B"
    },
    {
        "prompt":"What is the capital of Alaska? ",
        "options": ["A. Junaeu", "B. Fairbanks","C. Anchorage", "D. Dutch Harbor"],
        "answer": "B"
    },
    {
        "prompt": "What is an even number?",
        "options": ["A. 23", "B. 93", "C. 19", "D. None of the above"],
        "answer": "D"
    }
]

def run_quiz(question):
    score = 0
    for question in questions:        
        print(question["prompt"])

        for option in question["options"]:
            print(option)
        answer = input("Enter your answer(A. B. C. D.): ")
        print(answer,type(answer))

        if answer == question["answer"]:
            print("Correct, Great Job!")
            score += 1

        else:
            print("Wrong answer", question["answer"])

    print(f"You Got {score} out of {len(questions)} questions Correct")

run_quiz(questions)
#

it still going to fail if you give lower case option so change instance type or capitalize

#

if you ask AI , it will fix and give better solution (in this case)

questions = [
    {
        "prompt": "Which language is primarily spoken in Germany?",
        "options": ["A. French", "B. German", "C. English", "D. Spanish"],
        "answer": "B"
    },
    {
        "prompt": "What is the capital of Alaska?",
        "options": ["A. Juneau", "B. Fairbanks", "C. Anchorage", "D. Dutch Harbor"],
        "answer": "A"
    },
    {
        "prompt": "What is an even number?",
        "options": ["A. 23", "B. 93", "C. 19", "D. None of the above"],
        "answer": "D"
    }
]

def run_quiz(questions):
    score = 0
    for question in questions:
        print(question["prompt"])
        for option in question["options"]:
            print(option)
        
        answer = input("Enter your answer (A, B, C, D): ").strip().upper()  # Normalize input

        if answer == question["answer"]:
            print("Correct! Great Job!\n")
            score += 1  # Increment score for correct answer
        else:
            print(f"Wrong answer. The correct answer is {question['answer']}.\n")

    print(f"You got {score} out of {len(questions)} questions correct.")

run_quiz(questions)  # Fixed the function call
#

btw do not give \n a normal print has that default

leaden helm
#

I'm having a headache grinding this all morning

#

Gonna close this

#

!close

ruby ploverBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.