Stuck on a small project in Python

I am trying to fish a project where I create an app that lets the user input how many questions they want to answer and randomly generate questions for them to answer as well. At the end it will tell them how many they got correct out of the amount of questions they picked.

The problem I have having is that no matter what it tells me the answer is incorrect. I’m not sure why

import random
count = 0
questionFlag = True ##This flag makes sure the user can reenter their amtQuestions again
correct = 0 #Counter for correct questions
incorrect = 0 ##Counter for incorrect questions

amtQuestions = input("How many probems would you like to solve?: ")
    amtQuestions = int(amtQuestions)
    print("ERROR: Enter only numerical values")
    amtQuestions = input("How many questions would you like to solve?: ")
    questionFlag = False
    ##Numbers and equations
    percent = random.randint(1,99)
    percent2 = percent//100
    wholeNum = random.randint(1,999)
    ##Questions loop
    for range in (amtQuestions,0,-1): ##Counting down based on how many questions the user said they would like to solve
        userAnswer = float(input("What is " + str(percent) + " percent of " + str(wholeNum) + "?: "))
        answer = wholeNum * percent2
        if userAnswer == answer:
            print("That is correct")
            correct +=1
        elif userAnswer != answer:
            print("That is incorrect")
            incorrect +=1

print("You got " + str(correct) + " out of " + str(amtQuestions)+ " answers correct")

There could be something to do with float rounding errors here:

Also, your // seems to be causing issues here:

The reason is that // does integer division, meaning your percent // 100 actually rounds percent divided by 100 to the nearest integer-which is either 1 or 0.

You also might want to look at having this code inside the loop:

As there will only ever be one question, since this code is only run once. (If a user wants more than one question, they will just get the same one repeated.)

I hope this helps!

1 Like

Thank you that worked! I am having another issue I can’t figure out. It only loops 3 times even if I enter 1, 2, 4 or 5+ for amtQuestions. I’m not sure why… I think my for range in (amtQuestions,0,-1) is fine.

When looping over a range in Python, the for loop syntax looks like this:

for variable in range(start, stop, step):
  #code here

Does yours look like that?

1 Like

Oh I see that worked. I don’t know why I just put for range in(start,stop,step) So now it loops through all the number of questions the user picked. Thank you!

1 Like