Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python2.7
- '''
- Random number guessing game
- Created on Jul 19, 2011
- @author: giladh
- '''
- import random # Random number generator
- import sys
- import csv
- import re #Regular expression module
- from itertools import islice
- def hscores ():
- hscoresfile="/tmp/scorefile2"
- try:
- hscoresfile
- except:
- with open('/tmp/scorefile2', 'w') as hscores_fh_w:
- pass #create the scorefile if it doesn't exist
- with open('/tmp/scorefile2', 'rb') as hscores_fh_r: #open hscores file for reading
- reader = csv.reader(hscores_fh_r) #call csv.reader to parse file
- sorted_scores = (sorted(reader, key=lambda x:float(x[1]) )) #sort numeric by score
- top_ten = list(islice(sorted_scores,10)) #slice off top 10 players
- format = "%-8s %-8s %8s" #generate print format
- print "The top 10 players are:\n"
- print format % ("RANK","PLAYER","SCORE")
- print format % ("----","------","-----")
- rank = 1
- for row in top_ten:
- name,score = row[0],row[1]
- print format % (rank,name,score)
- rank += 1
- print
- #Core game function
- def playgame ():
- global player
- randnum = random.randint(0,100)
- print "The random number is:", randnum
- print "\nThis is a random number guessing game... You are to guess a number between 1-100...\n"
- try: #Is player defined?
- player
- except: #Get player's name
- player = raw_input("Please enter your name: ")
- print "Hello " + player + ", welcome!\n"
- hscores()
- allguess = [] #Initialize array of player's guesses
- guess = [] #Initialize the 'guess' variable
- while guess != randnum:
- if allguess:
- guess = (raw_input("Enter your next guess: "))
- else:
- guess = (raw_input(player + ", please enter your first guess: "))
- try:
- int(guess)
- except:
- print "Sorry, you must enter a valid integer..."
- continue
- guess = int(guess) #make sure its a number
- if guess in allguess:
- print "You already guessed that number, please try again..."
- continue
- elif guess < 1 or guess > 100:
- print "Please enter a valid integer between 1 and 100..."
- continue
- allguess.append(guess) #Add the guess to the array
- if len(allguess) > 1 and guess != randnum: #Should we calculate warmer/colder?
- prev_gdiff = abs(randnum - allguess[-2])
- curr_gdiff = abs(randnum - allguess[-1])
- if curr_gdiff < prev_gdiff:
- print "you're getting warmer..."
- elif curr_gdiff > prev_gdiff:
- print "you're getting colder..."
- elif curr_gdiff == prev_gdiff:
- print "same difference..."
- num_tries = int(len(allguess))
- print "\n" + player + ", you guessed it in " + str(num_tries) + " tries!"
- print "Your guesses were: " + str(allguess) + "\n"
- #write to scorefile
- with open('/tmp/scorefile2', 'a') as hscores_fh_w: #open hscores file for append
- writer = csv.writer(hscores_fh_w)
- writer.writerow([player, num_tries])
- repeat() #See if user wants to play again, call repeat func
- #Repeat game function
- def repeat ():
- repeat = raw_input("Play Again? (Y/N): ").lower()
- if re.match('y', repeat):
- playgame()
- else:
- print
- hscores()
- print "Thanks for playing " + player + "!"
- sys.exit(0)
- #Start the game
- playgame()
Advertisement
Add Comment
Please, Sign In to add comment