[Tutor] Here is my program that I made

Kevin python_newbie at vedorian.com
Sun Aug 22 22:14:47 CEST 2004


Ok I have attached the two files that go together. There are two 
problems that I can't figure out. One when you start the program it will 
ask you to enter  choice. When you enter a letter it will rais the 
exception error, but if you enter a number other then 1 or 2 it will 
just keep asking to make  choice. The second problem is when you go 
though the game when you guess the right number it will ask if you want 
to play again, so you need to enter y or n if you enter y you will start 
to play again if you enter n it will say thank you for playing then ask 
would you like to play again if you hit anything it will just keep 
asking you if you would like to play again. Any suggestions on how to 
fix that?

Thanks
Kevin
-------------- next part --------------
from defines import *
print """
        #####################################
        #                                   #
        #    The number guessing game       #
        #    ------------------------       #
        #  Please enter a choice:           #
        #                                   #
        #  1)Play the game                  #
        #  2)Exit                           #
        #####################################"""
while True:
    enter = raw_input("Please make a choice: ")
    try:
        if int(enter) == 1:
            game()
        if int(enter) == 2:
            print "Have a nice day"
            break
    except ValueError:
            print 'That is not a choice'
-------------- next part --------------
import random

def game():
    rand = random.randrange(1, 10)
    while True:
        x=raw_input("Enter a number: ")
        try:
             if int(x) != rand: 
                print "\r\nThat's not it!\r\n"
             if int(x) == rand:
                print "\r\nYAY! you guessed it right!"
                YesNo()
                break
        except ValueError:
             print "\r\nYour guess must be a number!\r\n"
        
def YesNo():
     while True:
         yn = raw_input("\r\nWhould you like to play again? ")
         if yn == 'y':
             game()
         elif yn == 'n':
             print "Thank you for playing!"
             break
         else:
             print "You must type y or n!"


More information about the Tutor mailing list