Did you know there is game you can create from scratch within a minute? It is called the guessing game or the guess my number game. In its basic form here is the algorithm for this game:
The following is an actual implementation in Python 2.x
Have a pleasant day!
secret_number = random_number_between_1_and_100 entered_number = 0 print "Guess my secret number between 1 and 100" while(entered_number is not equal to secret_number){ entered_number = read_user_input() if(entered_number is not an integer){ print "Please enter only whole numbers between 1 and 100" continue to top of while loop } if(entered_number < secret_number){ print entered_number + " is too low" } else if(entered_number > secret_number){ print entered_number + " is too high" } else{ print "You are great! " + secret_number + " was my secret number"
break from while loop
} }
The following is an actual implementation in Python 2.x
import random def guess_my_number(): secret_number = random.choice(range(1, 101)) entered_number = 0 while(entered_number != secret_number): entered_number = raw_input("Guess my number between 1 and 100\n") if not entered_number.isdigit(): print ("Please on enter whole number between 1 and 100") continue if int(entered_number) < secret_number: print (entered_number + " is too low") elif int(entered_number) > secret_number: print (entered_number + " is too high") else: print ("You are great! %s was my secret number" % secret_number) break guess_my_number()
Have a pleasant day!
No comments:
Post a Comment