0

I am trying to make a program which asks the user if a (random generated number) is divisible by a number from 1 to 12 (also random generated).

from random import randint

def questionnaire_for_divisibility():  # call a function named questionnaire

count_timer = 0  # set counter to zero

count_limit = 10  # limit is 10.

count_wrong = 0  # We will count how many wrong questions the user did

count_right = 0  # We will count how many right questions the user did

while count_timer < count_limit:  # while count is less than count_limit:

    random_number = randint(0, 1 * 10 ** 6)  # generate a random number from 1 to a million

    random_divisibility_number = randint(1, 12)  # generate a random number from 1 to 12. This is the divisor.

    question_to_user = input(f"Is {random_number} divisible with {random_divisibility_number}?: ")  # ask the
    # user if the random generated number is divisible by the divisor (we want integers, not floats.)

    x = random_number % random_divisibility_number  # let x be the value of the remainder of random number
    # divided by divisor

    count_timer += 1  # count increments by one each run.

    if random_number % random_divisibility_number == 0:  # if the random number divided by the divisor = 0
        # (no remainder) :

        if question_to_user == "Yes" or "yes":  # if the user says that the random number is divisible by the
            # divisor

            print("Correct!")  # print "Correct!"
            count_right += 1  # increase the count right by one each time the user gets the question right

        elif question_to_user == "No" or "no":  # else if the user says that the random number is not divisible by
            # the divisor even though it is:

            print("Wrong!")  # print out "Wrong!"

            count_wrong += 1  # increase the count wrong by one each time the user gets the question wrong.

    elif random_number % random_divisibility_number == x >= 1:  # if the remainder is greater than or equals to 1:

        if question_to_user == "No" or "no":  # If the user said that it is not divisible without giving a
            # remainder and that is True:

            print("Correct!")  # print out "Correct!"
            count_right += 1  # increase the count right by one each time the user gets the question right.

        elif question_to_user == "Yes" or "yes":  # else if the user said that it is divisible even though it's not:

            print("Wrong")  # print out "Wrong!"
            count_wrong += 1  # # increase the count wrong by one each time the user gets the question wrong.

    if count_timer == count_limit:  # When the timer equals to the timer limit

        print(f"Amount of right answered questions: {count_right}. ")  # print the amount of right answered.

        if count_right >= (1 / 2 * count_limit):  # if the user got more than half the questions right
            print("Awesome work!")  # praise the user.

        print(f"Amount of wrong answered questions: {count_wrong}")  # print the amount of wrong answered.

        if count_wrong >= (80 / 100 * count_limit):  # if the amount of wrong answered questions is >= than 80%

            print("Do a little better next time, okay buddy? I believe in you!")  # Give a motivational comment

        p = input("Would you like to see your results in percentage form?: ")

        if p == "Yes":
            print(f"Amount of right answered questions (in percentage): {(count_right / 10) * 100} %")
            print(f"Amount of wrong answered questions (in percentage): {(count_wrong / 10) * 100} %")

        print("Take an eye break now")  # print that the user should take an eye break.


  questionnaire_for_divisibility()

At the end of my program though, sometimes it says that a number is divisible by a divisor even though it is False. My program seems to be working, and I have tried to find ways to fix it, but to no prevail.

Could someone please help me?

matszwecja
  • 6,357
  • 2
  • 10
  • 17
  • 1
    This is one of those cases where simplifying the code would almost certainly make the problem easy to spot. – Mark Ransom Jun 06 '22 at 14:42
  • Your sample code should be minimal and reproducible. For example, "When my numerator is X and my denominator is Y, then the output is Z, which is wrong". See the following link on creating a [mcve] and please [edit] your code for indentation and clarity – G. Anderson Jun 06 '22 at 14:45
  • 2
    Do you really need the comment `# print "Correct!"` to explain what the line `print("Correct!")` does? Too many comments makes code less readable rather than more readable. – John Coleman Jun 06 '22 at 14:45
  • 2
    The problem seems to be all the lines like this one: `question_to_user == "Yes" or "yes"` - rewrite these as `question_to_user in ["Yes", "yes"]`, or better, convert the string to lower case `question_to_user = question_to_user.lower()`. – Stuart Jun 06 '22 at 14:46

0 Answers0