Hello all. I have just completed my very first python program just a
simple number guessing. I would like for someone to try it out if they
could and let me know how I did with it and where I could have
improved apon it. There are t files main.py and defs.py
Thanks
##############################################################################
#!/usr/bin/python
#main.py
###########################
from defs import *
import sys
###########################
choice() #Lets call the main menu up for the user
#This is used to ask if you would like to keep playing#
while 1:
play = raw_input("What is your choice? ") #Aks use to enter a choice
from the menu
if play == '3': #If user picks 3
print "\nHave a nice day!\n" #Tell them to have a nice day
sys.exit() #Then exit the entire thing
elif play == '2': #If user picks 2
instruct() #Then lets call the instructions function
elif play == '1': #If user picks 1
game() #Then lets call the game function for the user to play
elif play == '': #This is used is the menu leaves the screen so the
user can get it back
choice() #Bring back the menu
else:
print "\nYou need to pick 1, 2 or 3 or hit enter to see
choices\n"
#Let the user know that they can only pick 1, 2, 3 or just hit enter
and nothing else.
###########################################################################
#defs.py
#########
import random
import sys
#######################################################
#This is not used in the game yet
def yesno(question):
"""Asks the use a yes or no question"""
answer = None
while answer not in ('y','ye','yes','n','no'):
answer = raw_input(question).lower()
if answer not in ('y','ye','yes','n','no'):
print "Please enter Y or N"
return answer
########################################################
#This is the guessing game#
def game():
"""This is the main part of the guessing game"""
tries = 5 #Set the number of tries to 5
guess = random.randrange(2)+1 #Computer picks a number from 1 to 20
while 1:
try:
answer = int(raw_input("What is your guess? ")) #Ask
the user to
guess the number
if answer != guess: #Check to see if answer is not
equal to guessed number
tries -= 1 #if answer is wrong reduce the
number of tries by 1
print "Thats not it, you only have %d tries
left" %tries #Tell
user not right answer
if tries == 0: #Check to see if user has run
out of tries
print "\nYou lost this round!" #If no
tries left let the user know
raw_input("\n[Hit Enter to
Continue]\n") #Wait for user to go
back to main menu
choice() #Show main menu for user
break #Then break out of the loop
if answer == guess: #If the answer is equal to the
guessed number
print "\nCongrats you just Won this round!!"
#Tell the user he
just one in so many tries
raw_input("[Hit Enter to Continue]") #Wait for
user to go back to main menu
choice() #Show main menu for user
break #Break out of the loop
except ValueError: #Lets make sure that the user is only
typeing in
numbers and nothing else
print "\nYou can only use numbers\n" #Let user
know they can only
use numbers
#This is the main menu for the game
def choice():
"""The main menu of the game"""
print """
########################################
# #
# NUMBER GUESSING GAME #
# #
# - Created by Kevin J #
# - [EMAIL PROTECTED] #
# ------------------------------------ #
# 1 - Play #
# 2 - instructions #
# 3 - Quit #
########################################\n"""
#This is the instuctions on how to play the game that can be called
from the main menu
def instruct():
"""Instructions on how to play the
guessing game"""
print"""\n
HOW TO PLAY THE NUMBER GUESSING GAME
########################################################################
# To play this game all you need to do is guess a number from 1 to 20. #
# You will only have 5 tries to beat the game. Once you have beat the #
# game or you have used up all of your 5 tries you will be sent back #
# to the main screen were you can quit or start over again. #
########################################################################
[HIT ENTER FOR MAIN MENU]\n"""
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor