[Tutor] Main Function

Christopher King g.nius.ck at gmail.com
Mon Jun 20 17:26:36 CEST 2011


Looks all good except for this:
while guess == the_number:
Since you break out at the end, an if statement would be a more logical
choice.
and also:
if tries == 4:
           print("\nYou fail!")
           input("\n\nPress the enter key to exit.")
           break
you don't need to break because the condition already requires the tries to
be under 4, so you don't need both the break statement and the tries < 4.

Something else you could do is instead of
# start the program
main()
input("\n\nPress the enter key to quit.")
you could do
if __name__ == __main__:
          # start the program
          main()
          input("\n\nPress the enter key to quit.")
which means that if its used as a module (say, you want the number chooser
thing)
it won't run the game

also, I liked the print("\nYou fail!")
very nice

On Fri, Jun 17, 2011 at 11:21 PM, Vincent Balmori
<vincentbalmori at yahoo.com>wrote:

>
> 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 at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110620/25f8699f/attachment.html>


More information about the Tutor mailing list