Question Details

No question body available.

Tags

python

Answers (8)

March 1, 2026 Score: 1 Rep: 39,634 Quality: Medium Completeness: 60%

Your code is well structured because it separates out the different parts of the game into their own functions. The main loop is a loop (as it should be) and does not use recursion as I see too often.

The loop in newgame() does not need to initialise and adjust questionnum because you can use enumerate() for that.

The only thing actually wrong is the second loop in displayscore() since you break out after the first iteration. Neither that loop nor the one before it actually need to be loops.

See my version of your code below:

def newgame(): #the function where all the game logic is
    guesses = [] #the guesses the user had
    correctguesses = 0
    for num,quest in enumerate(questions): #i specifically wanna shorten this part downward
        print('---------------------------------------------------------')
        print(quest)
        for option in options[num]:
            print(option)
        guess = input("enter (A, B, C or D):  ").upper()
        guesses.append(guess)
        correctguesses += checkanswers(questions[quest], guess)  #everytime it's correct it adds 1 and 0 if wrong

display
score(correctguesses, guesses)

def check
answers(answer, userguess): if answer == userguess: print("CORRECT") return 1 else: print("WRONG") return 0

def displayscore(correctguess, guess): print("-----------------------------------------") print("RESULT:") print("-----------------------------------------") print("Answers: ", ' '.join(questions.values())) print("Your guesses: ", " ".join(guess))

score=int((correctguess/len(guess))*100)#percentage user got print("score: ", score, "%")

def play
again(): play = input("do you wanna play again(y/n)? ").lower() if play == "y": return True else: return False

questions={"who created python? ": "B", "From where is the name 'python' originating? ":"C", "What year did the second ww started?": "A", "What was UsainBolt time in 2009?": "C"}

options=[['A.Bjarne stroute', 'B.Guido Von Rossum', 'C.Alexander Cobol', 'D.Blaise Pascal'], ['A.from the python snake', 'B.from his mother heart disease', 'C.From music band Monty python', 'D.None'], ["A.1939", "B.1969","C.1200'A.D'", 'D.2001'], ['A.9.2', 'B.7.67', 'C.9.58', 'D.10.05']]

while True: #loop that restarts the code if playagain is true newgame() if not play_again(): break

print("\nBye")
March 1, 2026 Score: 1 Rep: 23,654 Quality: Low Completeness: 0%

There's some specific questions in his code as comments

March 1, 2026 Score: 0 Rep: 23,654 Quality: Medium Completeness: 80%

So the things I would change:

  • Move your static data into a proper structure, like a list of questions.
  • Do not encode game logic into your static data, like A. or whitespace. This can and should be added dynamically, the static data should only contain the differences between questions.
  • Use f-strings.
  • Move the checkanswers function into the question class. Remove its print statements. You should not mix logic and side effects like printing.
  • Annotate your function parameter and return types. That makes debugging in an editor like VSCode much easier.
  • Get comfortable with built in functions like enumerate and zip.
  • Avoid redundant data. questionnum is redundant, use enumerate instead. correctguesses is semi-redundant and can be calculated later on the fly.
  • Learn how to better utilize comments. If you wanna comment functions or variables, use proper docstrings. Don't comment things that are obvious, like guesses=[]#the guesses the user had. Prefer good variable names and clean functions over comments. I've seen too many times in projects that people update code but forget comments, and comments that do not fit the code any more are worse than no comments. Good code does not need comments.
  • Use an auto-formatter like autopep8. Enforce consistent coding style.
  • Use a main function. It's just good practice.
  • Global consts should be written in SCREAMINGSNAKECASE. (opinion-based)
  • Add a proper shebang

Here's my take on refactoring your code:

#!/bin/env python3

from dataclasses import dataclass from string import ascii
uppercase

@dataclass class Question: text: str options: list[str] correctanswer: str

def check
answer(self, answer: str) -> bool: return self.correctanswer == answer

QUESTIONS = [ Question(text="Who created Python?", options=['Bjarne stroute', 'Guido Von Rossum', 'Alexander Cobol', 'Blaise Pascal'], correct
answer='B'), Question(text="From where is the name 'Python' originating?", options=['from the python snake', 'from his mother heart disease', 'from comedy group Monty Python'], correctanswer='C'), Question(text="What year did WW2 start?", options=['1939', '1969', '1200 AD', '2001'], correctanswer='A'), Question(text="What was Usain Bolt's time in 2009?", options=['9.2', '7.67', '9.58', '10.05'], correctanswer='C'), ]

def new
game(): guesses = []

for question in QUESTIONS: print('---------------------------------------------------------') print(question.text) for pos, option in enumerate(question.options): print(f"{asciiuppercase[pos]}: {option}") guess = input("enter (A, B, C or D): ").upper()

if question.check
answer(guess): print("CORRECT") else: print("WRONG")

guesses.append(guess)

displayscore(guesses) print()

def display
score(guesses: list[str]): print("-----------------------------------------") print("RESULT:") print("-----------------------------------------")

numcorrect = 0

for pos, (question, guess) in enumerate(zip(QUESTIONS, guesses)): print(f"Question {pos+1}: {guess} (actual: {question.correct
answer})") if question.checkanswer(guess): numcorrect += 1

scorepercent = round((numcorrect/len(guesses))*100) print(f"Score: {scorepercent} %")

def play
again(): play = input("do you wanna play again(y/n)? ").lower() if play == "y": return True else: False

def main(): newgame()

while play
again(): # loop that restarts the code if playagain is true newgame()

print("Bye")

if name == "main": main()
March 1, 2026 Score: 0 Rep: 221,123 Quality: Low Completeness: 40%

What happens when you try to modify your code? Is anything stopping you? What specific help do you need other than someone to do your work for you?

so if you can do built in function that'll help

What does that even mean?

Sorry, but “here’s some random code make it better k thx bye” isn’t really something this community generally does.

To learn about this community and how we can help you, please start with the Tour and read How To Ask and its linked resources.

March 1, 2026 Score: 0 Rep: 1 Quality: Low Completeness: 0%

sorry it is actually : ' so if you can do THAT built-in functions that WILL help '.Also i appreciate

March 1, 2026 Score: 0 Rep: 1 Quality: Low Completeness: 0%

"WITH" built-ins functions sorry

March 1, 2026 Score: 0 Rep: 23,654 Quality: Low Completeness: 20%

The thing I would criticize most about your code is the representation of your static data. Having questions and answers as a dict and then the possible answers as a separate 2d array seems confusing and inefficient... This screams to me that there wants to be a custom class for a single question that contains the question text, the possible answers and the correct answer. And then simply an array of those question objects.

Everything else is kind of opinion based and fine in my opinion. Once you have such a class you could make "check_answers" a member function of that class, for example.

Might give it a go when I have time in a couple of hours

March 1, 2026 Score: 0 Rep: 221,123 Quality: Low Completeness: 0%

You mean this?

i specifically wanna shorten this part downward

That’s not exactly what I’d call a “specific question”.