[Tutor] Beginners question

Jeff Johnson jeff at dcsoftware.com
Thu Oct 8 15:48:20 CEST 2009


gary littwin wrote:
> 
> Hi all -
> 
> Just started on "Python Programming for Absolute Beginners" and I've got 
> a question:
> 
> The program called 'Guess my Number' goes like this:
> # Guess My Number
> #
> # The computer picks a random number between 1 and 100
> # 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
> 
> print "\tWelcome to 'Guess My Number'!"
> print "\nI'm thinking of a number between 1 and 15."
> print "Try to guess it in as few attempts as possible.\n"
> 
> import random
> 
> 
> # set the initial values
> the_number = random.randrange(15) + 1
> guess = int(raw_input("Take a guess: "))
> tries = 1
> 
> # guessing loop
> while (guess != the_number):
>    if (guess > the_number):
>        print "Lower..."
>    else:
>        print "Higher..."
> 
> guess = int(raw_input("Take a guess: "))
> tries += 1
> 
> print "You guessed it! The number was", the_number
> print "And it only took you", tries, "tries!\n"
> 
> raw_input("\n\nPress the enter key to exit.")
> 
> So here's the question - the original code has parentheses around the 
> lines of code with *(guess !=the_number)* and *(guess* *> the_number)* . 
>  I tried to run the program without the parentheses and it runs just 
> fine.  So what are the parentheses for??
> 
> Thanks a lot for your time - 
> 
>              Gary
> 
The parentheses in this procedure are not required but don't do any 
harm.  I often use extra parentheses to clarify what I am doing so it is 
more readable when going back to look at it a couple of years later. 
Especially in long calculations or SQL with lots of ands and ors.

-- 
Jeff

Jeff Johnson
jeff at dcsoftware.com
Phoenix Python User Group - sunpiggies at googlegroups.com


More information about the Tutor mailing list