mardi 18 avril 2017

What random number did the program pick

Below I have given a source code and I would like to know how to determine the value of 'secret_num' if the source code wasnt given. In other words lets say I have this program running(no way to see the source code) and my task is to find that secret number, how should I code to get that number? (Code was taken from: http://ift.tt/2oMfE40)

import random

max_num = 100  # upper bound, inclusive
secret_num = random.randint(1, max_num)
print("I'm thinking of a number between 1 and " + str(max_num) + "...")

max_guesses = 3
guesses_left = max_guesses

while guesses_left:
  print("Guesses remaining: " + str(guesses_left))
  guess = int(input("Enter your guess: "))
  if guess == secret_num:
    print("That's correct!")
    break
  elif guess < secret_num:
    print("That's too low.")
  else:   # or, elif guess > secret_num: 
    print("That's too high.")
  guesses_left -= 1
else:
  print("Sorry, you are out of guesses.")

Aucun commentaire:

Enregistrer un commentaire