Skip to content

Instantly share code, notes, and snippets.

@FardinBehboudi
Created March 31, 2017 09:20
Show Gist options
  • Save FardinBehboudi/3774943107035c5fecc2bf5fd651caea to your computer and use it in GitHub Desktop.
Save FardinBehboudi/3774943107035c5fecc2bf5fd651caea to your computer and use it in GitHub Desktop.
computer chooses a number between 1 and 100 randomly and you have to guess it. Python
#in this game computer choses a number between 1 and 100 randomly and you have to guess it
import random
def is_valid_num(s):
if s.isdigit() and 1 <= int(s) <= 100:
return True
else:
return False;
def main():
rand_num = random.randint(1,100)
# print rand_num
guessed_number = False
guess = str(raw_input('please input your guess between 1 and 100 '))
num_of_guess = 0
while not guessed_number:
if not is_valid_num(guess) :
guess = str(input('please input only digits enter again pls '))
continue
else:
num_of_guess += 1
guess = int(guess)
if guess < rand_num :
guess = str(input('your number is small again pls '))
elif guess > rand_num :
guess = str(input('your number is bigger again pls '))
else :
print 'you find the number in ', num_of_guess ,'times'
guessed_number = True
print 'thank you for playing'
main()
############################################################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment