Jump to content
  • Advertisement
Sign in to follow this  
shadowisadog

Feedback on my first Python program- Guess the number

This topic is 4088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
Thanks. I didn't even think about the stack, good point.

I originally had a while loop but I replaced it becuase I figured this way would be better.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!