#๐ Questions & Answers project
61 messages ยท Page 1 of 1 (latest)
@leaden helm
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.
""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)""
!code
And what's your question?
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"]:""
I'm assuming you meant question["answer"]?
also how do I do the paste thing
!paste
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.
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
I've tried that
And it didn't work
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.
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
Was trying to do a small project a simple one Q&A game
But I guess I'm still a relearner
Yes, but think about that line carefully. Why add 1 to the user's guess? What is your intent with that line?
No. Is answer holding the points?
No
So, if it's not holding the points, why are you adding points to it? What is holding the points?
answer is holding the user's guess.
You just don't appear to be thinking the code through. Look at what variables you have and think about what their purposes are.
There you go. The answer == 1 is useless, but the score += 1 looks correct.
I've got more error messages
bro you made so many simple mistakes
Yes
I'm built on mistakes and slowly learning
Smooth brain.exe is working overtime
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
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.