Advertisement
asweigart

guess1

Jun 15th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. import random
  2. secretNumber = random.randint(1,100) # Returns a random integer from 1 to 100.
  3. print('I am thinking of a number')
  4. print('between 1 and 100.')
  5. print('Take a guess:')
  6. userGuess = int(input()) # Whatever the user types is converted to an integer value and stored in userGuess.
  7.  
  8. # Keep looping as long as userGuess does not equal secretNumber
  9. while userGuess != secretNumber:
  10.     if userGuess < secretNumber:
  11.         print('That guess is too low.')
  12.     elif userGuess > secretNumber:
  13.         print('That guess is too high.')
  14.  
  15.     # Ask the user to guess again.
  16.     print('Take a guess:')
  17.     userGuess = int(input())
  18.  
  19.     # Go back to the start of this loop, and if the condition is True, repeat the code in the loop.
  20.  
  21. # The loop only stops looping when userGuess != secretNumber is False, that is,
  22. # when userGuess == secretNumber. So if the execution has reached this point, the player has guessed correctly.
  23. print('That guess is correct!')
  24. print('Goodbye!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement