-5

So as of right now the code needs to re-ask the problem given if the answer given was too high or too low. Also, if the answer is correct, it should tell them and then loop back to the question ('How many problems do you want?')

def main():

    gamenumber = int(input("How many problems do you want?\n"))
    count = 0
    while count < gamenumber:
        num_1 = randint(1,10)
        num_2 = randint(1,10)
        guess = int(input("What is " + str(num_1) + "x" + str(num_2) + "."))
        answer = (num_1*num_2)
        count += 1

        for guess in range(1):
            if guess == answer:
                print (' Thats Correct!')
        for guess in range(1):
            if guess > answer:
                print (' Answer is to high')
        for guess in range(1):
            if guess < answer:
                print ('Answer is to low')
main()
  • 1
    what is your question? – eyllanesc Jul 01 '17 at 07:13
  • How would I get the code to loop. everytime the user inputs an answer that is to high or two low the program is suppose to tell them that and then re-ask the problem but all it does is say its to high or low and then stops. – Damien L Dancy Jul 01 '17 at 07:16
  • 1
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Peter Wood Jul 01 '17 at 07:21
  • Put a while before `gamenumber = int(input("How many problems do you want?\n"))` – eyllanesc Jul 01 '17 at 07:22
  • Ok I think I explained my problem wrong. so I will try again. this is the code: – Damien L Dancy Jul 01 '17 at 07:27

2 Answers2

1

First of all can you please check your code. You have used "guess" variable in the for loop. When the program is executed the value of guess is say 40(4X10). When for statement is executed the guess values becomes 0 because of that you are getting the output as low. Make sure u change the variable you use in for loop to "num" and then check your output.

Why are you using 3 for loops you can do that in single for loop.

Please find the below code:-

from random import randint
def main():

    ans = 'y'
    while ans != 'n':
        gamenumber = int(input("How many problems do you want?\n"))
        count = 0
        while count < gamenumber:
            num_1 = randint(1,10)
            num_2 = randint(1,10)
            guess = int(input("What is " + str(num_1) + "x" + str(num_2) + "."))
            print(guess)
            answer = (num_1*num_2)
            print("=====>",answer)
            count += 1

            for num in range(1):
                if guess == answer:
                    print (' Thats Correct!')
                elif guess > answer:
                    print (' Answer is to high')
                elif guess < answer:
                    print ('Answer is to low')
        yes_no_input = str(input("Do you want to continue (y/n) ?"))
        ans = accept_ans(yes_no_input)
        if ans == 'n':
            print("thanks for the test")
            break;

def accept_ans(ans):

    if not ans.isalpha():
        print("Enter only y and n")
        ans = str(input("Do you want to continue (y/n) ?"))

    if ans == 'y' or ans == 'n':
        return ans

    if ans != 'y' or ans != 'n':
        print("please enter y for YES and n for NO")
        ans = str(input("Do you want to continue (y/n) ?"))
        if ans != 'y' or ans != 'n':
            accept_ans(ans)

if __name__ == '__main__':
    main()
maverick
  • 49
  • 1
  • 1
  • 6
0

After

print("Thats correct")

you need to call the

main()

function again.