Sign in to follow this  
shadowisadog

Feedback on my first Python program- Guess the number

Recommended Posts

shadowisadog    3217
Hello everyone. I am a C++ and a C# programmer but I was bored recently so I decided to play around with Python for a little while. Here is my first Python program which is a simple little guess the number application. Any feedback would be greatly appreciated (Things I did wrong, things I could do better, things that I could have done easier, ect). Here is the code:
import random

def PlayGame(): pass
def AskNum(pNum,pTries):
	Guess=0
	try:
		print
		Guess=int(raw_input("Enter a number between 1 and 10: "))
	except ValueError:
		print
		print "Oops not a number! Try Again!"
		AskNum(pNum,pTries)
	if Guess<pNum:
		print
		print "Too Low"
		pTries+=1
		AskNum(pNum,pTries)
	elif Guess>pNum:
		print
		print "Too High"
		pTries+=1
		AskNum(pNum,pTries)
	else:
		pTries+=1
		print
		print "You got it in "+str(pTries)+" tries!"
		PA=""
		while PA!="y" and PA!="n":
			print
			PA=raw_input("Play again (y/n): ")
			if PA=="y":
				PlayGame()
			elif PA=="n":
				print
				print "See you again soon!"
				return
			else:
				print
				print "Please enter Y or N"
				return

def PlayGame():
	print "Welcome to Guess a number!"
	Tries=0
	mNum=random.randrange(1,10)
	AskNum(mNum,Tries)

PlayGame()
print
print("The End!")
print
print("Created by Donald May")

Any feedback would be great, thanks :).

Share this post


Link to post
Share on other sites
Your code looks pretty good. Here are two minor recommendations:

1) The first definition of the "PlayGame()" function isn't needed.

2) Instead of using recursion (AskNum calling AskNum calling AskNum...), you might want to use a while loop. I doubt the player would ever fill the stack, but I think it would be a better practice, in this case.

- Mike

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this