Advertisement
informaticage

Indovina numero casuale

Feb 9th, 2021
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. # Loops
  2. # While, For
  3.  
  4. # Find divisors
  5. # Primality test
  6.  
  7. # n * ( n - 1 ) * ( n - 2 ) .... 1
  8.  
  9. # indovinda il numero
  10.  
  11. # 3) l'utente indovina => quanti tentativi ha fatto
  12. # 2) L'utente mette un numero troppo piccolo e lo informo
  13. # 1) L'utente mette un numero troppo grande e lo informo
  14.  
  15. # codice che genera numero casuale
  16.  
  17. import math
  18. import random
  19.  
  20. secret = random.randint(0, 100)
  21. read = int(input("Indovina il numero: "))
  22. i = 1
  23. while(read != secret):
  24.     # Se siamo qui allora l'utente ha sbagliato
  25.     i = i + 1
  26.     if(read > secret):
  27.         print("Troppo grande")
  28.     else:
  29.         print("Troppo piccolo")
  30.  
  31.     read = int(input("Indovina il numero: "))
  32.  
  33. # Codice se mi dice se ho sprecato tempo
  34. if(i > math.log(100, 2)):
  35.     print("Hai indovinato con %d tentativi" % i)
  36.     print("Il tuo metodo non è ottimale")
  37. else:
  38.     print("Hai indovinato con %d tentativi" % i)
  39.     print("Il tuo metodo sembra efficiente")
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement