x = 4
print("Welcome to my quiz")
a = input("What is 2 + 2? ")
if a == "4":
print("Good job, you have 1 point!")
print("Wrong, try again.")
x = x - 1
while x < 2:
x = din(x)
i want to make that if i get the question right the code ends.
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)
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 …
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!")
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!