Created
November 25, 2014 19:57
-
-
Save ajoy39/b3068abc01b496f68670 to your computer and use it in GitHub Desktop.
Random Number Guessing Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Give a random number for the computer to guess. | |
num_input=int(input("Please enter a number for the computer to guess: ")) | |
while num_input < 1 or num_input > 100: | |
if num_input > 100: | |
print("Please enter a number less than or equal to 100.") | |
if num_input < 1: | |
print ("Please enter a number greater than or equal to 1.") | |
#Guess counter for computer. | |
compguess_count=0 | |
#Let computer guess random number. | |
import random | |
computer_guess=random.randint (1, 100) | |
print("The computer guesses", computer_guess,".") | |
while computer_guess != num_input: | |
if computer_guess > num_input: | |
computer_guess=random.randint (1, computer_guess) | |
print("The computer guesses",computer_guess,".") | |
compguess_count+=1 | |
if computer_guess < num_input: | |
computer_guess=random.randint (computer_guess, 100) | |
print("The computer guesses",computer_guess,".") | |
compguess_count+=1 | |
if computer_guess == num_input: | |
total=compguess_count | |
print("The computer got my number in",total,"tries.") | |
if compguess_count == 7: | |
print("Computer guessed 8 times without getting the number.") | |
print("The number was",num_input,".") | |
break | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment