Informatique

Question

ECRIRE UN ALGORITHME qui demande à l'utilisateur d'entrer un nombre compris entre 1 et 3 jusqu'à ce que la réponse convient

1 Réponse

  • Réponse :

    Bonjour,

    Explications :

    def isfloat(s):

       try:

           float(s)

           return True

       except ValueError:

           return False

    def ask_num(deb,fin,nature):

       ok=False

       while not ok:

           nb=input("quel est votre nombre "+str(deb)+"< x < "+str(fin)+" :"  )

           if isfloat(nb):

               x=float(nb)

               if x >= deb and x <= fin:

                   if nature=="N":

                       if int(x)==x:

                           ok=True

                           x=int(x)

                   else:

                       ok=True

       return x

    #----------------------------------------------------    

    if __name__ == '__main__':

       print ("nombre=",ask_num(2,5,"N") )

       print ("nombre=",ask_num(2,5,"R") )

Autres questions