I answered another question that says "Modify the guess_number program so
that the program's code is in a function called main(). Don't forget to call
main() so you can play the game." It seems to be working fine, but I would
like to have a second opinion if there was a better way to put it together.

# Guess My Number
#
# The computer picks a random number between 1 and 10
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random  

def display_instruct():
    print("\tWelcome to 'Guess My Number'!")
    print("\nI'm thinking of a number between 1 and 10.")
    print("Try to guess it in as few attempts as possible.\n")

# set ask_number function 
def ask_number(question, low, high, step = 1):
    """Ask for a number within a range."""
    response =  None
    while response not in range(low, high, step):
        response = int(input(question))
    return response

# guessing loop
def num():
    # set the initial values
    the_number = random.randint(1, 10)
    tries = 0
    guess = None
    while guess != the_number and tries < 4:
        guess = ask_number("Take a guess:", 1, 10)
        if guess > the_number:
            print("Lower...")
        else:
            print("Higher...")
        tries += 1

        if tries == 4:
            print("\nYou fail!")
            input("\n\nPress the enter key to exit.")
            break

    while guess == the_number:
            print("\nYou guessed it!  The number was", the_number)
            print("And it only took you", tries, "tries!\n")
            input("\n\nPress the enter key to exit.")
            break
        
def main():
    display_instruct()
    num()
      
# start the program
main()
input("\n\nPress the enter key to quit.")
-- 
View this message in context: 
http://old.nabble.com/Main-Function-tp31873480p31873480.html
Sent from the Python - tutor mailing list archive at Nabble.com.

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to