I need help with python code

x = 4

print("Welcome to my quiz")

def din(x=0):
    a = input("What is 2 + 2? ")
    if a == "4":
        print("Good job, you have 1 point!")
        print("Wrong, try again.")
        x = x - 1
        return x
 while x < 2:
   x = din(x)

i want to make that if i get the question right the code ends.

i got the spaceing right but it got deleted when i copied the code over

points = 4

print(“Welcome to my quiz”)

def din():
  answer = input("What is 2 + 2?")
  if answer == “4”:
    print(“Good job, you have 1 point!”)
    points += 1
    print(“Wrong, please try again.”)

print("You have" + points)

I haven’t done python in a while, but that should fix it.

but my code was that you got 3 attempt and the problem whit the code was even if i got the question right it still reapeted(it was probably hard to see without spaceing)

So your idea is to ask a question, give 3 tries and in case of right answer one point and move on.
Trying to make it generic so you can use it for more questions that would be something like:

def ask_question(question, answer, current_score, tries=3):
  for i in range(tries, 0, -1):
    ans = input("your answer is: ")
    if ans == answer:
       current_score += 1
       print("Correct! Great, your score is:", current_score)
      return current_score
      print("Wrong answer. You have still" , i, "tries.")
  print("You failed answering the question")

You can just use it with:

ask_question("2+2= ?", "4", 0)

This should give you a start point and ask or google if something is not clear …

1 Like

@CoderElijah Does it seem like a school assignment? To me it looks a bit like it.

the original title said it was not, and now it is changed again … so go figure.
If what i suggested gets used without trying to understand it, the teacher will know fast

class MyQuiz:
    def __init__(this, _q: str, _a):
        this._question = _q
        this._answer = _a
        this._points = 0
        this._correct = False

    def start(this, _length: int):
        for x in range(_length):
            _input = input("-> ")
            if _input == this._answer:
                print("Correct | +1 point.")
                this._points += 1; this._correct = True
                print("Incorrect | -1 point.")
                this._points -= 1
        print(f"Points: {this._points}\nCorrect: {this._correct}")

if __name__ == "__main__":
    MyQuiz("2+2?", 4).start(5)

Does this code solve your issue?

A class is a bit too much …

1 Like

using this instead of self moment


I love using this or poka instead of self.


Says more about from which language you come or you prefer :slight_smile:


My first language was HTML/CSS, now I’m mainly a Python developer.

So how did you get about “this” then?

From a close friend of mine I admire.

It is against standard python styting not to use self and cls, but as long as you do things for fun it does not matter.

You created this program using a function, and forgot to use a while or a for loop.

You’re also passing the argument in the def function as x = 0, when in this program you don’t need to pass it any argument. Instead, you can write a program like this:

print("Welcome to my quiz!")

def din():
  points = 0 # Create a points variable to store the user's points.
  tries = 3
  while tries > 0: # Makes sure that the user's tries is above 0, when it hits 0, the program will stop.
    a = int(input("What is 2+2?: ")) # You need to add the int() statement so when the user gives their answer, it automatically converts it to an integer instead of a leaving it as a string.
    if a == 4: # Checks if the answer is == 4
      points += 1 # Add one to the user's score.
      print("Good job, you have",points,"point(s)!") # Output how many points the user has.
      tries = 0 # Sets the tries variable to 0, because they got it right, no need to continue.
    elif a != 4: # Checks if the answer is not equal to 4
      print("You got it wrong, please try again!") # Tells the user that they got it wrong.
      tries -= 1 # Takes away a try, the user will have 2 tries left after the first time it's taken away.
  return points # Returns the amount of points that the user has

print(din()) # Begins the quiz! Once the user is done, it prints the total amount of points they earned as a number.

Hopefully this helps you out, and if you have any other questions, feel free to reply!

Man was born and raised in JS

Classes make the code look cool though.

that is a bit overcomplicated… xD