Whats wrong in this code?

uestion ={
“Question”:“What is the captial of India?”,
“Options”:[
“A:Delhi”,
“B:Mumbai”,
“C:Chennai”,
“D:Bengal”,],
“Correct_answer”:“A”,}
{
“Question”:“What is RBI?”,
“Options”:
[
“A:Bank”,
“B:Firm”,
“C:Shop”,
“D:Showroom”,
],
“Correct_answer”:“A”,
}
{
“Question”:“What is Bitcoin?”,
“Options”:
[“A:Pen”,
“B:Fruit”,
“C:Bike”,
“D:Currency”],

        "Correct_answer":"D",}

{
“Question”:“Who is Virat Kohli?”,
“Options”:
[“A:Cricketer”,
“B:Footballer”,
“C:Hokey Player”,
“D:Aam Aadami”],
“Correct_Anser”:“A”,}
{
“Question”:“Who is the father on nation?”,
“Options”:
[“A:Mahatma Gandi”,
“B:Mahatma Phule”,
“C:Sardar Patel”,
“D:Narendra Modi”],
“Correct_Answer”:“A”,}

{ “Question”:“who is Google CEO”,
“Options”:[“A:Narendra Modi”,
“B:Sundar Pichai”,
“C:Virat Kohali”,
“D:Mukesh Ambani”],
“Correct_Answer”:“B”,}

{ “Question”:“How many tyres in Bike?”,
“Options”:[“A:One”,“B:Three”,“C:Two”,“D:Four”],
“Correct_Answer”:“C”,}

score = 0
negetive_score = 0
for i in question:
if negetive_score == 3:
print(f"You Lost with the score{score}“)
break
question =i[“question”]
options = i[“Options”]
correct_answer = i[“correct answer”]
ans = input(f”{question}\nA) {options[0]}\tB) {options[1]}\nC) {options[2]}\tD) {options[3]}\n").upper()
if ans == correct_answer:
score +=1
else:
negetive_score +=1

else:
print(f"Congrats!You Won with the Score{score}")

Hello,

Question:
Whats wrong in this code?

Answer:
It’s not formatted.

When entering code for others to help, please follow these guidelines. It makes it easier to analyze, and at your end, verify that the code appears as it does on your terminal.

4 Likes

Arvind,

I suspect many things are wrong and you have not told us anything useful of what happens when you run it.

Consider sending it again with three grave accent marks on one line before and another after so it is froamtted as-is.

But one guess is your use of double quotes of three kinds. Python does not support the open and close quotes in the program and just the purely vertical double quote you also use. Code edited in something like Microsoft Word puts in the smart quotes and the code is then dumb in python.

1 Like

I see a number of problems.

The code starts with code in this form:

uestion ={
    # dict for question 1
}
{
    # dict for question 2
}
{
    # dict for question 3
}

Is that meant to be question instead of uestion?

It assigns a dict to uestion, and that is then followed by more dicts that aren’t in a list, so they’ll be ignored.

If you want it to be a list of questions, then enclose them in [...] and separate or follow them with commas:

uestion = [
    {
        # dict for question 1
    },
    {
        # dict for question 2
    },
    {
        # dict for question 3
    },
]

You also have this:

for i in question:
    if negetive_score == 3:
        print(f"You Lost with the score{score}")
        break
    question =i["question"]
    # more lines

It iterates over question, which is a list of questions (questions would be a better name), but it assigns i["question"] to question, which is confusing. It’ll raise a KeyError anyway because there’s no key "question"; the key is actually "Question". Similarly, there’s no key "correct answer"; the key is "Correct_answer".

1 Like

Matthew,

I notice similar issues that suggested lots of potential errors in how the thinking and implementation of the code were intended and started by suggesting we get to see the code formatted properly and also to wonder if the wild versions of double quotes were in the original or somehow introduced when it was formatted by some version of MarkDown.

The many missing parts included not specifying what the code was meant to do. Your guesses that the idea was to have a data structure like a list of dictionaries may turn out to be true but there are many other possibilities as the code is fairly disorganized, and of course has lost indentation info too.

I suggest that besides showing us what, if anything, went wrong and also error messages, that they learn to code incrementally as python allows in the interpreter and validate smaller sections and fix any errors long before it reaches some point where it may mysteriously fail. The coder may be from India but the actual text they include along with creative spelling is not really relevant as long as they follow the rules of the Python language including use of the proper version of quotes, matching open braces of a particular kind with the corresponding close braces and so on.

What I would suggest to anyone, especially beginners is to keep it simple. An example might be:


dict1Choices = {

"A" : "Delhi",

"B" : "Mumbai",

"C" : "Chennai",

"D" : "Bengal"

} # end dictionary

dict1 = {

"Question" : "What is the capital of India?",

"Options" : dict1Options,

"Correct_answer" : "A"

} # end dictionary

Note no deep nesting is needed. If you now continue using whatever formatting makes sense and can be handled by python and hopefully is easier to read by human readers, you can continue defining additional items like dict2 and dict3 as stand-alone entities and finally do something like:


Questions = [

dict1,

dict2,

... # fill in missing values here

dict_n # again a specific number rather than n

] # end list

Code like that can have advantages and as it is only done once, and this app does not use much memory, might help find out what is wrong before trying to use it.

Of course, some of the choices I made above such as properly making A,B,C,D into dictionary entries may not be the way they want it but whatever design they choose would then guide how to have the code later down handle it if they are made to fit each other.

I recommend using the PyCharm Community IDE. That will highlight a lot of misspelled variables here.

As for improving the code, I would declare a class called Question like this:

from typing import Dict:
class Question:
    question: str
    options: Dict[str, str]
    correct_answer: str

    def __init__(question: str, options: Dict[str, str], correct_answer: str):
        self.question = question
        self.options = options
        self.correct_answer = answer

In doing so you can use the questions by referencing the members a_question.question, a_question.options etc. The IDE will help you to ensure consistent spelling. This is not the case with dicts, and the code you posted is riddled with inconsistent spelling. This would also happen to me if I did not use an IDE.

1 Like

Ruben,

Your method is one of many excellent advanced methods many experienced programmers would use.

I note the OP has not posted again and may not even be reading our replies so we may simply be talking to ourselves.

My impression, which can easily be wrong, based on the style of coding and the style of words spelled oddly in English, is that the OP is not fully comfortable in English and new at programming in Python. They may also be asking about what some might call homework from the earlier parts of a Python course. If they replied and told us more, we could be more helpful.

My replies were focused on helping at a lower level including by showing that breaking something up into smaller parts and putting them together might be an easier way to avoid mistakes and in debugging.

In the real world, I might use a method like yours or a named tuple if each “item” had a fixed set of contents and would have gone a step further and made the A/B/C/D choices into another object as well.

Help us help you. Read this link first and learn how to format code so we can help you better. About the Python Help category You will get more help this way.