vendredi 10 juin 2016

Making y>x using a while loop and random.randint (python 3.4.3)

In this code I want to create ten random question however if the operating sign(+,- or*) is - then i want the first number to be bigger than the second(so the answer will be positive.However this doesn't seem to be working as i'm getting "-" questions where y is smaller than x. The aim of this code is for y,r and z to all be randomly generated.

import random
    name=input("What is you name: ")
    x=0
    d=0
    op=['+','-','*']

    def easy():
        global x
        global d
        y=random.randint(1,12)
        z=random.randint(1,12)
        r=op[random.randint(0,2)]
        if (r==1):
            while (y<z):
                y=random.randint(1,12)   

        answer=eval('{}{}{}'.format(y,r,z))
        question=int(input("What is {}{}{}?".format(y,r,z)))
        if question==answer:
            print("Correct")
            x+=1
        else:
          print("Incorrect")
        d+=1
        while d<10:
            easy()
    easy()




Aucun commentaire:

Enregistrer un commentaire