Re: [Tutor] guess-my-number programme

2011-09-24 Thread Kĩnũthia Mũchane

On 09/23/2011 11:28 PM, Wayne Werner wrote:
On Fri, Sep 23, 2011 at 2:25 PM, ADRIAN KELLY > wrote:




can anyone explain the *_tries_* part of this programme to me i
know its meant to count the number of guesses made by the user by
adding 1 but i just cant figure out how it does this..can
someone explain??  i.e. tries = 1, tries +1 etc cant get my
head around it...


The concept that's confusing you here is something called order of 
evaluation, or evaluation strategy: 
http://en.wikipedia.org/wiki/Evaluation_strategy


The best way to understand these things is to try it out, and the 
interactive interpreter is extremely handy.


I presume you understand the concept of assignment, correct? For example:

>>> tries = 1

Now tries contains 1:

>>> tries = 1
>>> tries
1

The variable 'tries' now contains the value 1 (In Python this is not 
technically true, but it's useful to describe it that way).

Why?


>>> tries = 4

Now 'tries' contains 4. Of course, just like your standard algebra 
variables, you can use your variables in python:


>>> tries * 4
16
>>> tries + tries
8
>>> tries / 1
4

when these expressions are /evaluated/ they produce the mathematical 
expression you probably expect. But you can do more than just evaluate 
expressions, you can store their results:


>>> lots_of_tries = tries * 100
>>> lots_of_tries
400

So what about the expression that confuses you?

>>> tries = tries + 1
>>> tries
5

Well, the first thing that happens when python sees the equals sign is 
that the right hand side of the expression is evaluated, so python takes:


tries = tries + 1

and turns it into

tries = 4 + 1
tries = 5

So each time your program sees 'tries = tries + 1' python evaluates 
the right side first, then assigns its value to the variable on the left.


HTH,
Wayne


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



--
Kĩnũthia

S 1º 8' 24”
E 36º 57' 36”
1522m

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


Re: [Tutor] paper scissors

2011-09-24 Thread bodsda
The %s signifies that a string will be provided to go there.

The % after the string signifies that the following variable/strings/tuple will 
contain the items to be placed where the %s's are (in order)

The tuple (wins, losses) are the two items that should replace the %s's 

Be aware that there are other place holders such as %i and %d that expect a 
certain data type. With %s, if it is not a string that is given to it, it will 
attempt to be converted to a string first.

Hope this helps,
Bodsda  
Sent from my BlackBerry® wireless device

-Original Message-
From: bob gailer 
Sender: tutor-bounces+bodsda=googlemail@python.org
Date: Fri, 23 Sep 2011 15:02:44 
To: *tutor python
Subject: Re: [Tutor] paper scissors

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

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


[Tutor] Infinite Loop

2011-09-24 Thread Cameron Macleod
Hi,

I've been trying to code a simple guess my number game as a starter
programming project but I've generated an infinite loop accidentally. Since
I'm new to programming in general, I can't really figure out what's causing
it. Thanks

===

import random

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

#set the initial values
the_number = random.randint(1, 100)
guess = int(input("Take a guess: "))
tries = 1

# guessing loop
while guess != the_number:
if guess > the_number:
print("Lower...")
else:
print("Higher...")

guess = int(input("Take a guess: "))
tries += 1

print("You guessed it! The number was", the_number)
print("And it only took you", tries, "tries!\n")

if tries <= 5:
print("I didn't know roadrunners could type!")
elif tries >= 99:
print("P'raps 'Only' wasn't the right word...")
elif tries == 100:
print("0_0 You are the unluckiest person in the world. Or the
stupidest...")

input("\n\nPress the enter key to exit.")
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Infinite Loop

2011-09-24 Thread nehal dattani
Hi,

Please modify your else block .

else:
print("Higher...")
guess = int(input("Take a guess: "))
tries += 1


This should work for you.

Regards,
Nehal Dattani

On Sat, Sep 24, 2011 at 8:13 PM, Cameron Macleod wrote:

> Hi,
>
> I've been trying to code a simple guess my number game as a starter
> programming project but I've generated an infinite loop accidentally. Since
> I'm new to programming in general, I can't really figure out what's causing
> it. Thanks
>
> ===
>
> import random
>
> print("\tWelcome to 'Guess my Number'!")
> print("\nI'm thinking of a number between 1 and 100.")
> print("Try to guess it in as few attempts as possible.\n")
>
> #set the initial values
> the_number = random.randint(1, 100)
> guess = int(input("Take a guess: "))
> tries = 1
>
> # guessing loop
> while guess != the_number:
> if guess > the_number:
> print("Lower...")
> else:
> print("Higher...")
>
> guess = int(input("Take a guess: "))
> tries += 1
>
> print("You guessed it! The number was", the_number)
> print("And it only took you", tries, "tries!\n")
>
> if tries <= 5:
> print("I didn't know roadrunners could type!")
> elif tries >= 99:
> print("P'raps 'Only' wasn't the right word...")
> elif tries == 100:
> print("0_0 You are the unluckiest person in the world. Or the
> stupidest...")
>
> input("\n\nPress the enter key to exit.")
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Infinite Loop

2011-09-24 Thread nehal dattani
Hi cameron,

Sorry, I didn't tested it properly.

Actual block should go like this


while guess != the_number:
if guess > the_number:
print("Lower...")
else:
print("Higher...")
guess = int(input("Take a guess: "))
tries += 1



On Sat, Sep 24, 2011 at 8:13 PM, Cameron Macleod wrote:

> Hi,
>
> I've been trying to code a simple guess my number game as a starter
> programming project but I've generated an infinite loop accidentally. Since
> I'm new to programming in general, I can't really figure out what's causing
> it. Thanks
>
> ===
>
> import random
>
> print("\tWelcome to 'Guess my Number'!")
> print("\nI'm thinking of a number between 1 and 100.")
> print("Try to guess it in as few attempts as possible.\n")
>
> #set the initial values
> the_number = random.randint(1, 100)
> guess = int(input("Take a guess: "))
> tries = 1
>
> # guessing loop
> while guess != the_number:
> if guess > the_number:
> print("Lower...")
> else:
> print("Higher...")
>
> guess = int(input("Take a guess: "))
> tries += 1
>
> print("You guessed it! The number was", the_number)
> print("And it only took you", tries, "tries!\n")
>
> if tries <= 5:
> print("I didn't know roadrunners could type!")
> elif tries >= 99:
> print("P'raps 'Only' wasn't the right word...")
> elif tries == 100:
> print("0_0 You are the unluckiest person in the world. Or the
> stupidest...")
>
> input("\n\nPress the enter key to exit.")
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Infinite Loop

2011-09-24 Thread xDog Walker
On Saturday 2011 September 24 07:43, Cameron Macleod wrote:
> Hi,
>
> I've been trying to code a simple guess my number game as a starter
> programming project but I've generated an infinite loop accidentally. Since
> I'm new to programming in general, I can't really figure out what's causing
> it. Thanks
>
> ===
>
> import random
>
> print("\tWelcome to 'Guess my Number'!")
> print("\nI'm thinking of a number between 1 and 100.")
> print("Try to guess it in as few attempts as possible.\n")
>
> #set the initial values
> the_number = random.randint(1, 100)
> guess = int(input("Take a guess: "))
> tries = 1
>
> # guessing loop
> while guess != the_number:
> if guess > the_number:
> print("Lower...")
> else:
> print("Higher...")
>
> guess = int(input("Take a guess: "))
> tries += 1
>
> print("You guessed it! The number was", the_number)
> print("And it only took you", tries, "tries!\n")
>
> if tries <= 5:
> print("I didn't know roadrunners could type!")
> elif tries >= 99:
> print("P'raps 'Only' wasn't the right word...")
> elif tries == 100:
> print("0_0 You are the unluckiest person in the world. Or the
> stupidest...")
>
> input("\n\nPress the enter key to exit.")

I suspect that your test procedure was invalid. When I converted your code
to run with python 2.5 and lowered the randint to (1, 5), it ran alright.
-- 
I have seen the future and I am not in it.

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


Re: [Tutor] Infinite Loop

2011-09-24 Thread Alan Gauld

On 24/09/11 15:43, Cameron Macleod wrote:

Hi,

I've been trying to code a simple guess my number game as a starter
programming project but I've generated an infinite loop accidentally.
Since I'm new to programming in general, I can't really figure out
what's causing it.


Nehal told you how to fix it but didn't explain why.

You have the input() call indented to the same level as the else block 
so it only gets executed as part of that block. If the guessed number is 
greater than the target the loop repeats infinitely.


By moving the input() line out you get a new number each time.

HTH


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


[Tutor] Logging to different directories

2011-09-24 Thread Brett Ritter
I commonly prefer to have my log files in //MM/DD/ directories (it
allows for lots of fun command-line actions).  I've not yet sussed out
a nice way to do this with the logging module.  Before I ask the main
list, is there a fundamental feature I've missed?

The TimedRotatingFileHandler will let me write to filenames based on
date, but it won't alter the path (in fact, the entire logging module
seems to be built on the assumption of no subdirectories).  Have I
missed something?

-- 
Brett Ritter / SwiftOne
swift...@swiftone.org
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor