Skip to content

Instantly share code, notes, and snippets.

@cjauvin
Last active July 18, 2022 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjauvin/31e2724343cde18edf5485ecc4a9f4eb to your computer and use it in GitHub Desktop.
Save cjauvin/31e2724343cde18edf5485ecc4a9f4eb to your computer and use it in GitHub Desktop.
import sys
from random import randrange
if __name__ == "__main__":
while True:
print("Level: ", end="")
try:
level = int(input())
except ValueError:
level = False
if level > 0:
if level == 1:
number = 1
else:
number = randrange(1, level + 1)
while True:
print("Guess: ", end="")
try:
guess = int(input())
except ValueError:
guess = False
if guess > 0:
if guess < number:
print("Too small!")
elif guess > number:
print("Too large!")
else:
print("Just right!")
sys.exit()
@cjauvin
Copy link
Author

cjauvin commented Jul 18, 2022

Petit détail @jpallard6120 : même si Python est dynamiquement typé, c'est quand même toujours mieux de garder des valeurs d'un même type dans une variable. Donc au lieu de level = False et guess = False (donc des bool), quand tu veux invalider les valeurs d'input, j'utiliserais simplement la valeur -1, qui reste un int.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment