Re: [Tutor] Final review

2014-05-06 Thread Scott Dunning

On May 1, 2014, at 5:30 AM, Steven D'Aprano  wrote:
> 

Awesome, thanks everyone!  I understand lists a lot better now.  

I have another question.  I don’t understand why below would give an error?

>>> greeting = 'Hello World'
>>> greeting [len(greeting)]


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


Re: [Tutor] while loop

2014-04-01 Thread Scott Dunning

On Mar 31, 2014, at 5:15 AM, Dave Angel  wrote:
> 
> Do you know how to define and initialize a second local variable? 
> Create one called i,  with a value zero.
> 
> You test expression will not have a literal,  but compare the two
> locals. And the statement that increments will change i,  not
> n.

So like this?  
def print_n(s,n):
i = 0
while i < n:
print s,
i += 1

print_n('a',3)

So this is basically making i bigger by +1 every time the while loop passes 
until it passes n, then it becomes false right?  

Also, with this exercise it’s using a doctest so I don’t actually call the 
function so I can’t figure out a way to make the string’s print on separate 
lines without changing the doctest code?  

Thanks for all of the help!


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


Re: [Tutor] while loop

2014-03-31 Thread Scott Dunning

On Mar 31, 2014, at 1:39 AM, Mark Lawrence  wrote:
> 
> They say that the truth hurts, so if that's the best you can come up with, I 
> suggest you give up programming :(
You’re in the TUTOR section.  People in here are new to programming.  I’ve only 
been doing this for a couple months and I just learned about while loops two 
days ago.  If my questions are annoying you I suggest you not read them.  : (
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] while loop

2014-03-31 Thread Scott Dunning

On Mar 31, 2014, at 2:01 AM, Alan Gauld  wrote:

> 
> Incidentally, your assignment does not appear to require
> a while loop, just iteration? If thats the case you could
> use a for loop instead and it would actually be more
> suitable. Have you covered for loops yet?
> 

No, we haven’t got to for loops yet, that’s why it needs to be done with a 
while for now.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] while loop

2014-03-31 Thread Scott Dunning

On Mar 30, 2014, at 4:29 AM, Dave Angel  wrote:
> 
> You're getting closer.   Remember that the assignment shows your
> function being called with 10, not zero.  So you should have a
> separate local variable,  probably called I, which starts at
> zero, and gets incremented each time. 
> 
> The test in the while should be comparing them.
> 
So, this is what I have now and it ‘works’ but, instead of printing (s) on 
seperate lines they’re all on the same line?

def print_n(s,n):
while n < 10:
print s * n
break
assert isinstance(s, str)
assert isinstance(n, int)


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


Re: [Tutor] while loop

2014-03-31 Thread Scott Dunning

On Mar 30, 2014, at 4:29 AM, Dave Angel  wrote:
> 
> 
> You're getting closer.   Remember that the assignment shows your
> function being called with 10, not zero.  So you should have a
> separate local variable,  probably called I, which starts at
> zero, and gets incremented each time. 
The exercise just asks to print (s), (n) times using iteration.  The exersise 
is in a doctest which I didn’t really understand at first.  So, I guess while I 
was doing it “correctly” it wasn’t what the exercise is asking for.  This I 
guess is what the doctest is looking for.

"""Print the string `s`, `n` times.

Parameters
--
s -- A string
n -- an integer, the number of times to
 print `s'

Examples


>>> print_n("hello", 3)
hello
hello
hello

>>> print_n("bye", 0)

>>> print_n("a", 6)
a
a
a
a
a
a

"""
> 
> The test in the while should be comparing them.
> 
> Note that the number of times is specified in top level code, and
> implemented in the function.  You should not have a literal 10 in
> the function. 
Without out a break or placing that 10 in there I can’t think of a way to have 
the while loop stop once it reaches (n).  Any hints?  

SCott 








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


Re: [Tutor] while loop

2014-03-30 Thread Scott Dunning

On Mar 29, 2014, at 12:47 AM, Dave Angel  wrote:
> 
> So did your code print the string 10 times?  When asking for help,
> it's useful to show what you tried,  and what was expected,  and
> what actually resulted. 
> 
> You use * to replicate the string,  but that wasn't what the
> assignment asked for. So take out the *n part. You're supposed to
> use iteration,  specifically the while loop. 
> 
> Your while loop doesn't quit after 10 times, it keeps going.  Can
> you figure out why?

This works without a break.  Is this more a long the line of what the excercise 
was looking for you think?
> 
def print_n(s, n):
while n <= 10:
print s
n = n + 1

print_n("hello\n", 0)


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


Re: [Tutor] while loop

2014-03-30 Thread Scott Dunning

On Mar 29, 2014, at 12:47 AM, Dave Angel  wrote:
> 
> What are you uncertain about,  assert or isinstance?  Such
> statements are frequently used to make sure the function
> arguments are of the right type. 
I’m not sure exactly what it’s doing.  I guess I need to read up on it again.
> 
>> 
>> 
>> This is what I have so far but I’m not really sure it’s what the excersise 
>> is asking for?
>> 
>> n = 5
>> def print_n(s, n):
>>   while n > 0:
>>   print s * n
>> 
>> print_n("hello", 10)
>> 
> 
> So did your code print the string 10 times?  When asking for help,
> it's useful to show what you tried,  and what was expected,  and
> what actually resulted. 
Yes it repeated forever, I put a break after the print statement.  
> 
> You use * to replicate the string,  but that wasn't what the
> assignment asked for. So take out the *n part. You're supposed to
> use iteration,  specifically the while loop. 
That’s where I was confused, I wasn’t sure how to get the while loop to work 
just the n times and then stop.
> 
> Your while loop doesn't quit after 10 times, it keeps going.  Can
> you figure out why?
> 
I understand why it didn’t stop after 10 times, because I said while n is 
greater than 0 print s, and I have the value of n as 5 so it will never stop, 
that’s why I added a break after the print statement.  I’m sure there is a 
better way , I’m just not seeing it.  Any hints?  


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


Re: [Tutor] while loop

2014-03-30 Thread Scott Dunning

On Mar 28, 2014, at 10:36 PM, Ben Finney  wrote:
> 
> A good programming exercise will show an example input and the expected
> output, to give an unambiguous test case. Does the homework have that?
This is what the exercise has as examples…


"""Print the string `s`, `n` times.

Parameters
--
s -- A string
n -- an integer, the number of times to
 print `s'

Examples


>>> print_n("hello", 3)
hello
hello
hello

>>> print_n("bye", 0)

>>> print_n("a", 6)
a
a
a
a
a
a

"""
assert isinstance(s, str)
assert isinstance(n, int)

#TODO: Implement the function

> 
> If not, you're unfortunately left to your own interpretation of what the
> requirements mean.
> 
I’m not sure what assert isinstance means?  

This is what I have, it works but I’m not sure it’s what the exercise is asking 
for.

n = 5
def print_n(s, n):
while n > 0:
print s * n
break

print_n("hello\n", 10)



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


Re: [Tutor] Help with Guess the number script

2014-03-12 Thread Scott Dunning

On Mar 10, 2014, at 11:18 PM, Dave Angel  wrote:

> if guess < secret - 10 or guess > secret - 10:
> 
> Think about that line. You might even want to put in a separate
> function to test what it does.
> HINT: it's wrong.
> 
Got it!  I realized what I was doing wrong.  I needed that plus sign for the 
too high part of it and what you said helped me figure it out!  Thanks Dave, 
all you actually!

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


Re: [Tutor] Help with Guess the number script

2014-03-12 Thread Scott Dunning

On Mar 11, 2014, at 1:57 AM, Alan Gauld  wrote:
> OK so far, you don't need all the print statements
> but that's just a style issue. (You could just
> insert '\n' characters instead.)
You’re right, I’m actually not sure why I did it that way.

> 
>> if guess < secret - 10 or guess > secret - 10:
> 
> This is the right idea for cutting the line count but you
> have the comparison values wrong. Look back to earlier
> emails, you are repeating the same error as before.
> Manually think through what happens in the line above
> if guess == secret.
Oh, do you mean it should be <= and >=??  I’m not sure why that would work, 
because if guess==secret I have another statement in my code that takes care of 
that.  I didn’t want to add my whole code because it’s too long but that is in 
there.
> 
> And then once you get that fixed you can rewrite using
> the chained comparison trick to tidy it up.

> ie
> if not (lower limit < guess > upper limit):
I see what you’re saying about the chained comparison.  What I’m having 
problems with is condensing it to half the lines of code so I don’t have to 
have the same conditionals under both ‘too low’ and ‘to high’.   If that makes 
sense.  
> 
> 

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


Re: [Tutor] Help with Guess the number script

2014-03-08 Thread Scott dunning

> On Mar 8, 2014, at 3:57 AM, spir  wrote:
> 
>> On 03/08/2014 10:13 AM, Alan Gauld wrote:
>>> On 08/03/14 01:23, Scott W Dunning wrote:
>>> 
>>> On Mar 7, 2014, at 11:02 AM, Alan Gauld  wrote:
>>> 
>>> GOT IT!!  Finally!  Thanks for all of your help!!
>>> 
>>> This is what I got, not sure if it’s correct but it’s working!
>> 
>> Well done.
>> And now that you have the right set of tests you can
>> half the number of lines by combining your if
>> conditions again, like you had in the original
>> post. ie. Bring your hot/cold/warm tests together.
Yeah I'm gonna try that.  The reason I split it up in the first place is I 
couldn't get it to work properly being all together (ie Either too high or too 
low yet always either cold, warm, or on fire).

> 
> Yes, and note the relevant piece of data is the absolute value: 
> abs(secret-guess). This gives you at once on-fire / hot / warm / cold / icy 
> ... whatever you like ;-) (pretty sexy game, guess-my-number!).
> 
Hmm, not sure I understand.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-08 Thread Scott dunning

> On Mar 8, 2014, at 6:26 AM, Mark Lawrence  wrote:
> 
>> On 08/03/2014 01:23, Scott W Dunning wrote:
>> 
>> On Mar 7, 2014, at 11:02 AM, Alan Gauld  wrote:
>> 
>> GOT IT!!  Finally!  Thanks for all of your help!!
> 
> If at first you don't succeed... :)
> 
>> 
>> This is what I got, not sure if it’s correct but it’s working!
>> 
>> def print_hints(secret, guess):
>> if guess < 1 or guess > 100:
> 
> Only now do I feel that it's time to point out that the above line would 
> probably be written by an experienced Python programmer as:-
> 
> if 1 > guess > 100:
> 
OH!  I see what you're saying, ignore my last post.  Yes that looks cleaner.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-08 Thread Scott dunning

> On Mar 8, 2014, at 6:36 AM, Dave Angel  wrote:
> 
> Mark Lawrence  Wrote in message:
>>> On 08/03/2014 01:23, Scott W Dunning wrote:
>>> 
>>> 
>>> def print_hints(secret, guess):
>>> if guess < 1 or guess > 100:
>> 
>> Only now do I feel that it's time to point out that the above line would 
>> probably be written by an experienced Python programmer as:-
>> 
>> if 1 > guess > 100:
> 
> With an appropriate 'not' or its equivalent,  of course. 
> 
This is how our teacher wanted it all written under the print_hints function.  
Besides if I was an experienced python programmer I wouldn't be asking such 
trivial stuff under the tutor forum lol. 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Help with "Guess the Number" script

2014-02-28 Thread Scott Dunning
Hello, i am working on a project for learning python and I’m stuck.  The 
directions are confusing me.  Please keep in mind I’m very new to this.  The 
directions are long so I’ll just add the paragraphs I’m confused about and my 
code if someone could help me out I’d greatly appreciate it!  Also, we haven’t 
learned loops yet so just conditional operators and for some reason we can’t 
use global variables.  


from random import randrange
randrange(1, 101)

from random import seed
seed(129)

def print_description():
print """Welcome to Guess the Number.
I have seleted a secret number in the range 1 ... 100.
You must guess the number within 10 tries.
I will tell you if you ar high or low, and
I will tell you if you are hot or cold.\n"""
   
def get_guess(guess_number):
print "(",guess_number,")""Plese enter a guess:"
current_guess = raw_input()
return int(guess_number)

def main():
print_description()
secret = 50
current_guess = 1
get_guess(1)
if current_guess != secret():
print "Congratulations you win!!"
   
main()


Here are the instructions I’m having a hard time with and just not sure I’m 
doing it correctly.  I’m not sure the get_guess function is correct and I’m a 
little lost with the secret and current_guess variable.

From within the body of the main function, immediately after the call to print 
description, create variable secret and assign it a random number between 1 and 
100, generated using the randrange function. You will need to pass two argument 
to randrange, what do you think they should be? You should be able to use the 
python help system or online python documentation to make sure you understand 
the arguments to randrange.

After the end of the body of the print description function, define a new 
global function named get guess that takes a single parameter. Name the 
parameter guess number, because it will hold the index (1, 2, 3, ..., 10) of 
current guess attempt. Make the function ask the user to enter guess using the 
raw input function. The function will return the number entered by the user, 
after it has been converted to an integer.

Return to the main function after the statement that assigned a value to the 
secret variable. In a new variable named current guess store the result of 
calling the get guess function with an argument of 1. Run your program to make 
sure it works correctly.

At the end of the main function, check if the current guess matches the secret. 
If it matches, print ‘Congratulations, you win!’. If it does not, print ‘Please 
play again!’ 





Thanks again!!!



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