Re: [Tutor] Creating a list from other lists

2012-10-23 Thread Walter Prins
>> It's an HTML post.
>
> Not according to the version of Thunderbird I am using, which shows it
> as a plain text email.
>
> I suspect that the HTML attachment may be invalid HTML, understandable
> by Gmail and possibly nothing else. Typical of Google :(

Looking at the email source it clearly shows as being mimetype
"multipart" with both a plaintext and HTML part.  I don't think you
can much criticise GMail for interpreting and attempting to render an
HTML email as HTML.  The biggest culprit here IMHO was the sending
mail client that generated a broken text mode version of the email's
intended formatting by dropping the indentation (and if the HTML is
broken, generated broken HTML to boot as well.)

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


Re: [Tutor] Creating a list from other lists

2012-10-23 Thread Steven D'Aprano

On 23/10/12 19:51, Walter Prins wrote:

It's an HTML post.


Not according to the version of Thunderbird I am using, which shows it
as a plain text email.

I suspect that the HTML attachment may be invalid HTML, understandable
by Gmail and possibly nothing else. Typical of Google :(


Looking at the email source it clearly shows as being mimetype
"multipart" with both a plaintext and HTML part.  I don't think you
can much criticise GMail for interpreting and attempting to render an
HTML email as HTML.


Perhaps not, but I can criticize Gmail for offering such an anti-feature
in the first place.



The biggest culprit here IMHO was the sending
mail client


That would be Gmail, almost certainly.



that generated a broken text mode version of the email's
intended formatting by dropping the indentation (and if the HTML is
broken, generated broken HTML to boot as well.)


I don't actually know if the HTML part is broken or not. I saved it and
opened it in Firefox, and Firefox rendered it as raw text showing the
tags. Perhaps that just means my test was faulty.

Either way though, I'm sick to the back teeth of Google-related
technologies being user hostile. Whether it is the horror that Google
Groups has become, Google's search engine tracking and bubbling you
when you search, Google illegally installing tracking cookies AND
THEN LYING ABOUT IT to the American FTC, the awfulness of their image
search interface (which just about every search engine now apes), and
their efforts to link everything you do on the Internet to a Google
account which can be matched to your offline identity, I think their
motto is now best described as "Do be evil".


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


Re: [Tutor] Which is better Practice and why

2012-10-23 Thread Steven D'Aprano

On 23/10/12 12:53, Devin Jeanpierre wrote:

On Mon, Oct 22, 2012 at 8:54 PM, Steven D'Aprano  wrote:



package.__main__.py is designed to be run as a script, and not to be
imported. But that doesn't mean that there's no good purpose for
importing it. If your package is non-trivial, you ought to have tests
for it, including package.__main__. Those tests will probably want to
import the module, not necessarily run it as a script.


I think you will find that it is never the case that __name__ !=
'__main__' in a file called "__main__.py".


I think you'll find that you're wrong there :)

[steve@ando ~]$ mkdir package
[steve@ando ~]$ touch package/__init__.py
[steve@ando ~]$ echo "print(__name__)" > package/__main__.py
[steve@ando ~]$ python package
__main__
[steve@ando ~]$ python -c "import package.__main__"
package.__main__



If I want to test something, I put it in another file.


Yes. And how do you import the __main__.py module to test it, without
it executing?



My __main__.py file will generally look something like this:

 from . import game
 game.Game().run()


Well, that's probably trivial enough that it requires no testing
other than "does __main__.py exist?".



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


Re: [Tutor] Creating a list from other lists

2012-10-23 Thread eryksun
On Tue, Oct 23, 2012 at 4:51 AM, Walter Prins  wrote:
>
> Looking at the email source it clearly shows as being mimetype
> "multipart" with both a plaintext and HTML part.  I don't think you
> can much criticise GMail for interpreting and attempting to render an
> HTML email as HTML.  The biggest culprit here IMHO was the sending
> mail client that generated a broken text mode version of the email's
> intended formatting by dropping the indentation (and if the HTML is
> broken, generated broken HTML to boot as well.)

The message looks to have been sent with Gmail's webmail client. The
problem I think starts with Webkit. When the user presses  in a
rich text box, instead of tabbing between fields on the page, it
inserts a 
element.

I tested how Gmail handles this by creating a simple HTML file
containing the following:

for x, y , z in zip(a, b, c): L.extend([x, y,
z])print L

I rendered this in Firefox and copied it into a new rich text email.
Gmail stripped out the tab characters before rendering the plain text
section as follows:

for x, y , z in zip(a, b, c):
L.extend([x, y, z])
print L

They certainly could be smarter when it comes to spans with
"whitespace:pre" styling, but it's better if users would just think to
use plain text when posting code.

That said, plain text with Gmail has its own pitfalls. To render it
correctly with a monospace font in the web client you need to use
something like Stylish to hack Gmail's CSS. But, more importantly, you
have to be wise to Google's line wrapping at 69 characters (no RFC
requires this). It doesn't give you a live preview. Instead you have
to manually wrap code at 69 columns in an IDE and paste it into the
text box. Otherwise your code will probably get mangled.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Which is better Practice and why

2012-10-23 Thread Devin Jeanpierre
On Tue, Oct 23, 2012 at 5:59 AM, Steven D'Aprano  wrote:
> I think you'll find that you're wrong there :)

Oops.

>> If I want to test something, I put it in another file.
> Yes. And how do you import the __main__.py module to test it, without
> it executing?

How do you test the body of code in the "if __name__ == '__main__'"
block, without it executing?

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


Re: [Tutor] Which is better Practice and why

2012-10-23 Thread Oscar Benjamin
On 23 October 2012 02:17, Devin Jeanpierre  wrote:
> On Mon, Oct 22, 2012 at 8:42 PM, Steven D'Aprano  wrote:
>> If you do that, and the module directly or indirectly imports itself
>> while it is running as a script, you may run into trouble. But writing
>> a dual-purpose module that is usable as an importable module or as a
>> stand-alone script is not problematic in itself.
>
> Yes. However, it is somewhat hard to guarantee that a module won't
> indirectly import itself for a large codebase. It certainly sometimes
> happens by accident.

I guess this is the crux of the issue. If your script is part of and
depends on a large codebase then you may as well place all of its code
elsewhere in the codebase. The script then serves simply as an entry
point and its presence in the project won't hinder your ability to
move all the code it uses around to suit its relationship to the rest
of the codebase.

Importable scripts are useful when you have a codebase that is
smaller. In particular if your codebase is one single script (a common
case) then you should always use if __name__ == "__main__" unless the
script is trivial and/or, as Steven says, you're feeling lazy. Doing
this means that you can test your script and its functions by
importing the script in the interactive interpreter and that you can
reuse the script as a module in some other project

If your codebase is a single module rather than a script, giving it an
if __name__ == "__main__" block allows someone to perform a common
task with the module (or perhaps run its tests) using the -m
interpreter option without needing to turn a single file project into
a double file project:

$ python -m mymodule arg1 arg2

This is often better than creating a separate script to serve as a
redundant entry point (redundant since the module can already serve as
its own entry point).

Once you have one importable script A.py you can then place another
script B.py in the same folder have B import and use some of A's code.
As Eryksun has said making this work both ways (having B also import
from A) can lead to problems. If you find yourself wanting to do that
then you have probably organised your code badly. As a solution
consider moving all of the common code into one of A or B or into a
new module/script C.

Another factor not mentioned yet is that the use of if __name__ ==
"__main__" is so ubiquitous that using it in your own script
communicates something to most people who read it (including
yourself). If I know that X.py is intended to be used as a script and
I want to read it to find out how it works, one of the first things I
would do is search for that line. My natural assumption is that no
non-trivial execution occurs outside of that if-block (this is almost
always true if I wrote the script and it is longer than about 10
lines).


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


Re: [Tutor] Iterators, example (need help)

2012-10-23 Thread eryksun
On Mon, Oct 22, 2012 at 6:30 PM, Bryan A. Zimmer  wrote:
>
> ignore1='''
>
> This program works to do what I set out to do as a first step
> BAZ 10/19/2012
>
> '''

If this isn't meant to be a module docstring, leave it where it is,
but remove the assignment to ignore1. Unassigned triple-quoted strings
are the closest thing in Python to C-style /* multi-line comments */.
This works because the compiler ignores unassigned literal
strings/numbers, as the following example shows:

>>> code = compile('''\
... 0
... """
... multi-line
... comment
... """
... ''', filename='', mode='exec')

>>> code.co_consts
(None,)

If the number 0 and the string were't ignored, they'd be in co_consts.
This is a tuple of immutable objects used by Python bytecode, such as
literal numbers, strings (including unicode), tuples, and the
singleton None. 3.x adds True, False, Ellipsis, and frozenset. (I
probably forgot something, but you get the idea.)

If the string is meant to be a docstring, on the other hand, put it on
the first line before your imports. In this special case, the compiler
creates a __doc__ constant, which is assigned as an attribute of the
module. Here's a skeleton of the process:

>>> code = compile('"a docstring"', '', 'exec')
>>> dis.dis(code)
  1   0 LOAD_CONST   0 ('a docstring')
  3 STORE_NAME   0 (__doc__)
  6 LOAD_CONST   1 (None)
  9 RETURN_VALUE

>>> mod = types.ModuleType('mod')
>>> vars(mod)
{'__name__': 'mod', '__doc__': None}

>>> exec code in vars(mod)

>>> vars(mod).keys()
['__builtins__', '__name__', '__doc__']
>>> mod.__doc__
'a docstring'

Executing the code in the module also added __builtins__ to the
namespace, so built-in functions and types are available. A normal
import would also set the __file__ and __package__ attributes.


Regarding your Env class, I hope you'd use os.environ.iteritems() in practice.

> def __next__(self,master=None):

In 2.x, this should be next(). In 3.x, it's __next__().

http://docs.python.org/library/stdtypes.html#iterator.next
http://docs.python.org/py3k/library/stdtypes.html#iterator.__next__


StringVar objects s1 and s2 should be attributes of the App instance,
not global variables.  Also, as it is, since you aren't rebinding s1
or s2 in next(), there's no technical reason to declare them with the
'global' keyword in next().

http://docs.python.org/reference/simple_stmts.html#the-global-statement

Don't needlessly parenthesize expressions in statements such as if,
return, etc. Python doesn't require it, so it's just symbol noise. For
example, use:

if x:
return x

x, y, d, v, and i are unhelpful attribute names. I strongly recommend
using descriptive names such as current_key, current_value. It helps a
lot when you're updating code later on, especially if you aren't the
original author.

As to the next() method itself, I'd keep it simple. Return the
key/value tuple if self._index < len(self.keys), and otherwise raise
StopIteration. I wouldn't conflate the implementation of next() with
updating the StringVar instances s1 and s2. The "Next" button can use
a method defined in App to iterate the Env instance and update the
strings.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] iterators, need help

2012-10-23 Thread Bryan A. Zimmer
I just wanted to say thanlk you to those who took the time to answer my
request for help. It is very much appreciated.

 

Bryan A Zimmer 

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


[Tutor] blackJack problem

2012-10-23 Thread Matthew Dalrymple




player1.py > http://pastebin.com/jzv1Hhs1blackjack.py > 
http://pastebin.com/Vj3sp3Ca ok the problem im having is with the GetBet 
function...i need it so you start with 1000 it will bet 5 everytime...if i win 
it adds 5 if i lose it subtracts 5 if we tie nothing changes. I think this is 
right for the most part except it says that it can call it before reference. I 
thought by having a value outside of the functions made the value global. Am i 
somewhat on the right track?  any help would be appriciated... Oh i also forgot 
to mention that even though im not using all the parameters they have to be 
there because the professor wants to be able to run other peoples "player1.py" 
files on my game. thanks
Matthew D ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] blackJack problem

2012-10-23 Thread Alan Gauld
Matthew Dalrymple  writes:

> I thought by having a value outside
> of the functions made the value global. 

Not quite.
To change the global value in a function you also need to declare it as
global inside the function. Otherwise it will create a new local
variable that has no effect on the global one.

Also by declaring a parameter of your function with the same name as the
global you confuse things. Don't do that.


Am i somewhat on the right track?
>  
> any help would be appriciated...
>  
> Oh i also forgot to mention that even though im not using all the parameters
> they have to be there because the professor wants to be able to run other
> peoples "player1.py" files on my game.

Interesting concept. And if the other peoples files are calling your
functions with values in those parameters you will just ignore them I
presume? Hmmm, that should produce some interesting results...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
Playing with gnus and possibly screwing it up!!

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


[Tutor] Retrieving numbers from a text

2012-10-23 Thread Rufino Beniga
*Problem*: Analyze a chaotic one-dimensional map: Write a program
OneDMapAnalyze.py that reads the data in OneDMap.txt. Calculate the mean
value of the iterates, printing the result to the terminal; again annotate
the output so that it is understandable. Determine the fraction of iterates
that have x > 0.5. Also, print out that result.

*The previous problem that I have already done:*
Iterate a chaotic one-dimensional map: Write a program OneDMapIterate.py that
iterates the function f(x) = 4.0 x ( 1 - x ). First, set the initial
condition x = 0.3. Then apply f(x) repeatedly to each new x. Iterate for
100 steps, printing on each line the iteration number and the successive x
values to file OneDMap.txt.

My program:

#Imports math functions
from math import *

#Initial condition for x
x = 0.3

#Opens onedmap.txt file and writes to it
file = open('onedmap.txt','w')

#Loop, replaces old x with new x i amount of times
for i in range(1,101):
x = 4.0*x*(1.0-x)

#Writes contents of loop to file
print >> file, 'Iterate number ', i, ': ', x

#Closes file
file.close()

My problem is, I don't know how to get ONLY the numbers from the
OneDMap.txt file. Also, assuming that I am able to do just that, how would
I get them into an array?

-- 
tell me i can't do it, then i'll prove you wrong!
facebook me
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Retrieving numbers from a text

2012-10-23 Thread Alan Gauld

On 23/10/12 23:45, Rufino Beniga wrote:

First, you realize I hope that we don;t do homework for you but we will 
try to help steer you the right way...


So the simple answer is...

> --
> tell me i can't do it, then i'll prove you wrong!

OK, You can't do it.

Is that enough?

I thought not, read on...


*_The previous problem that I have already done:_*
Iterate a chaotic one-dimensional map: Write a program
OneDMapIterate.py that iterates the function f(x) = 4.0 x ( 1 - x ).
First, set the initial condition x = 0.3. Then apply f(x) repeatedly to
each new x. Iterate for 100 steps, printing on each line the iteration
number and the successive x values to file OneDMap.txt.

My program:



#Writes contents of loop to file
 print >> file, 'Iterate number ', i, ': ', x


Note you are writing more than you were asked to to the file.
This slightly complicates the second problem... but only slightly.


My problem is, I don't know how to get ONLY the numbers from the
OneDMap.txt file. Also, assuming that I am able to do just that, how
would I get them into an array?


One thing at a time.
Can you read a line from the file as a string?
Can you split a string into its constituent parts? In this case they 
should be:


'Iterate number '
i
':'
x

Can you extract the 2nd and 4th items (do you see why writing
more than you were asked complicates it?)

Can you convert strings to numbers, specifically integers and floats?

Now the second question:
Can you create a new empty list?
Can you append items to that list?

Putting them together:
Can you write a loop that
-reads a line from the file
-extracts the data you want
-appends it to your list

If you can do the above you have pretty much 'proved me wrong'...

If not come back with more specific questions.


--
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


Re: [Tutor] Retrieving numbers from a text

2012-10-23 Thread Steven D'Aprano

On 24/10/12 09:45, Rufino Beniga wrote:


My problem is, I don't know how to get ONLY the numbers from the
OneDMap.txt file. Also, assuming that I am able to do just that, how would
I get them into an array?



What we need to see is typical contents of your file. I expect it looks
something like this:

iterate number 562 0.4532888
iterate number 563 0.3872701
iterate number 564 0.9100345
iterate number 565 0.0027318
iterate number 566 0.6661924


and so forth.

In that case, you can read the final column by reading each line, splitting
into words, and taking only the last one. Since this is homework, I'll give
you the pieces you need to use, you should be able to assemble them into
working code yourself.

To read each line in a file:


for line in open("name of file"):
process line


Replace "process line" with the actual code you need.


To build up a list of numbers:

numbers = []
numbers.append(0.3)
numbers.append(0.9736)
numbers.append(0.3209)


Normally you would put the "numbers.append(x)" into a loop,
something like this:

numbers = []
for item in many_items:
value = process(item)
numbers.append(value)


Hint: reading a file gives you a loop, one for each line. You
want to process each line to get a number, then append that
number to the list of numbers.


You will have lines of text that look like this:

line = "iterate number 563 0.3872701"

which is a string, you need to extract the last column and
turn it into a float. Hints:


use line.split() to split the line into four words:

words = line.split()
print words
=> prints ['iterate', 'number', '563', '0.3872701']


use words[-1] to extract the last word


use function float(word) to convert it from a string to a
number.


If you put all those pieces together, it should take no more
than a dozen lines of code to read the file, and extract the
final column into a list of numbers.



Good luck!




--
Steven

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


[Tutor] Strings.

2012-10-23 Thread Nitin Ainani
Dear Sir/Madam,

I am  new to python I have a question. It is as follows:

Suppose *s* is a variable and *s* stores empty string

s=""
Now if we write following statement


print(s[0])  # it gives error


print(s[0:])# it does not give error
print (s[13:13])   # this too does not give erro
why?



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


Re: [Tutor] Doubt!

2012-10-23 Thread Dave Angel
On 10/23/2012 07:14 PM, Nitin Ainani wrote:
> Dear Sir/Madam,
>
> I am  new to python I have a question. It is as follows:
>
> Suppose *s* is a variable and *s* stores empty string
>
> s=""
> Now if we write following statement
>
>
> print(s[0])  # it gives error

There is no 0th character, so it's an error.  If they had wanted to
avoid an error, they'd have had to return None or some other flag, and
that's not pythonic.

>
> print(s[0:])# it does not give error

A slice is more tolerant, by definition.  It gives you all the items in
the range you specify, and the resulting string (in this case) is equal
to the original one.

> why?
>
>

-- 

DaveA

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


Re: [Tutor] Doubt!

2012-10-23 Thread Steven D'Aprano

On 24/10/12 10:14, Nitin Ainani wrote:

Dear Sir/Madam,

I am  new to python I have a question. It is as follows:

Suppose *s* is a variable and *s* stores empty string

s=""
Now if we write following statement


print(s[0])  # it gives error


print(s[0:])# it does not give error

why?



Because indexing a string returns the character at that index. If
the index is out-of-bounds, there is nothing to return and an
error will be raised:


py> s = "hello world"
py> s[0]
'h'
py> s[6]
'w'
py> s[100]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: string index out of range



The colon : tells Python to use a slice. Slices look like:

[start:end:step]

where step defaults to 1 if it is not given, start defaults to 0,
and end defaults to the length of the string.

py> s = "hello world"
py> s[0:5]
'hello'
py> s[:5]
'hello'
py> s[1:9]
'ello wor'

If either start or end is out of bounds, the result is truncated
at the ends of the string:

py> s[1:100]
'ello world'


If start is greater-or-equal to end, it is an empty slice, and
the empty string is returned:

py> s[6:3]
''

So the slice s[0:] says "give me the slice of the string s starting
at position 0 and extending up to the end of the string".

In the case of the empty string, since the string is empty, there
are no characters and every index is out-of-bounds. So s[0] will
raise an error. But the slice s[0:] is an empty slice, and will
return the empty string.

Slicing can be considered to be a short-cut for using a for-loop
and range.

s[start:end:step] is like this function:

def slice_copy(s, start, end=None, step=1):
if end is None:
end = len(s)
new = []
for i in range(start, end, step):
new.append(s[i])
return ''.join(new)


(except that real slicing can deal with out-of-bounds start and
end as well.)

If you study slice_copy, you should understand why an empty slice
returns the empty string.



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


Re: [Tutor] Doubt!

2012-10-23 Thread emile

On 10/23/2012 04:14 PM, Nitin Ainani wrote:

Dear Sir/Madam,

I am  new to python I have a question. It is as follows:

Suppose *s* is a variable and *s* stores empty string

s=""
Now if we write following statement


print(s[0])  # it gives error


print(s[0:])# it does not give error

why?


See http://docs.python.org/tutorial/introduction.html and in particular 
section 3.1.2 on Strings where you'll find


"Degenerate slice indices are handled gracefully: an index that is too 
large is replaced by the string size, an upper bound smaller than the 
lower bound returns an empty string."


HTH,

Emile

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


Re: [Tutor] Strings.

2012-10-23 Thread Alexander
On Tue, Oct 23, 2012 at 7:24 PM, Nitin Ainani  wrote:
> Dear Sir/Madam,
>
> I am  new to python I have a question. It is as follows:
>
> Suppose s is a variable and s stores empty string
>
> emptyString = ""
#consider the string to be non-empty, that is, let's say 13 characters
long (the number you chose below)
string13 = "1234567890ABC"
print ( string13 [ 0 ] )

>Now if we write following statement
>
> print (  s [ 0 ]  )  # it gives error
***what error? ***
The error and your example can be used to understand indexes.
 s [ 0 ] is asking for the value of a specific piece of s.
s[0] is asking for the value at index Zero of string s.
S has nothing inside of it.

>
> print(s[0:])# it does not give error
#what does it give?
> print (s[13:13])   # this too does not give error
#What is the output
> why?

And with your other two examples I suggest understanding slicing.
slice! I want the whole pie, myPie [ 0: ]  #the : around an index has
a special meaning.
Try a string with text and observe the output of your questions.
aString [ : ]
aString [ : 0 ]
aStr [ 1: ]

etc
Check it out, mess around, get back to me.
Alexander
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Strings.

2012-10-23 Thread Dave Angel
On 10/23/2012 07:48 PM, Alexander wrote:
> On Tue, Oct 23, 2012 at 7:24 PM, Nitin Ainani  wrote:
>> Dear Sir/Madam,
>>
>> I am  new to python I have a question. It is as follows:
>>
>> Suppose s is a variable and s stores empty string
>>
>> emptyString = ""
> #consider the string to be non-empty, that is, let's say 13 characters
> long (the number you chose below)
> string13 = "1234567890ABC"
> print ( string13 [ 0 ] )
>
>> Now if we write following statement
>>
>> print (  s [ 0 ]  )  # it gives error
> ***what error? ***
> The error and your example can be used to understand indexes.
>  s [ 0 ] is asking for the value of a specific piece of s.
> s[0] is asking for the value at index Zero of string s.
> S has nothing inside of it.
>
>> print(s[0:])# it does not give error
> #what does it give?
>> print (s[13:13])   # this too does not give error
> #What is the output
>> why?
> And with your other two examples I suggest understanding slicing.
> slice! I want the whole pie, myPie [ 0: ]  #the : around an index has
> a special meaning.
> Try a string with text and observe the output of your questions.
> aString [ : ]
> aString [ : 0 ]
> aStr [ 1: ]
>
> etc
> Check it out, mess around, get back to me.
> Alexander
Nitin:

Or even better, stick to the original thread, with the dubious title
"Doubt!"  What's the point of starting a second nearly-identical thread
within 10 minutes?


-- 

DaveA

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


Re: [Tutor] Strings.

2012-10-23 Thread Mark Lawrence

On 24/10/2012 00:24, Nitin Ainani wrote:

Dear Sir/Madam,

I am  new to python I have a question. It is as follows:

Suppose *s* is a variable and *s* stores empty string


Pythonistas prefer the use of name and not variable.



s=""
Now if we write following statement


print(s[0])  # it gives error


print(s[0:])# it does not give error
print (s[13:13])   # this too does not give erro
why?


This was the design decision made by Guido van Rossum many, many years ago.

Other people have already given you other answers on the other thread 
that you started with the same question but a different subject, please 
don't do that, thanks.




Regards,
Nitin



--
Cheers.

Mark Lawrence.

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


Re: [Tutor] blackJack problem

2012-10-23 Thread Matthew D


 > To: tutor@python.org
> From: alan.ga...@btinternet.com
> Date: Tue, 23 Oct 2012 23:46:23 +0100
> Subject: Re: [Tutor] blackJack problem
> 
> Matthew Dalrymple  writes:
> 
> > I thought by having a value outside
> > of the functions made the value global. 
> ok...is there an easier way to write this? the reason i was calling that 
> global value the same as the currentBet is because i need to add that value 
> to 5...I have to start at 1000 and if i win add 5 and if i lose subtract 5 
> and store that value. I have been staring at this stuff for like 3 hours :/ 
> and still cant get even a slight idea as of what should work
> Not quite.
> To change the global value in a function you also need to declare it as
> global inside the function. Otherwise it will create a new local
> variable that has no effect on the global one.
> 
> Also by declaring a parameter of your function with the same name as the
> global you confuse things. Don't do that.
> 
> 
> Am i somewhat on the right track?
> >  
> > any help would be appriciated...
> >  
> > Oh i also forgot to mention that even though im not using all the parameters
> > they have to be there because the professor wants to be able to run other
> > peoples "player1.py" files on my game.
> 
> Interesting concept. And if the other peoples files are calling your
> functions with values in those parameters you will just ignore them I
> presume? Hmmm, that should produce some interesting results...
it should work in theory correct?
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> Playing with gnus and possibly screwing it up!!
> 
> ___
> 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] blackJack problem

2012-10-23 Thread Alan Gauld

On 24/10/12 01:00, Matthew D wrote:


 > > I thought by having a value outside
 > > of the functions made the value global.
 >
ok...is there an easier way to write this? the reason i was calling that
global value the same as the currentBet is because i need to add that
value to 5...


You need to read about global variables and namespaces in Python.

But using a global name as a parameter name will not make the function 
use the global name as the first parameter, in fact the opposite will 
happen in that the parameter will hide the global value.


If you want to pass the global value into your function then call it 
with the global name as the first argument. If you want to change the 
global value from your function then you need to declare the name global 
inside your function:


##
readMe = 42   # global var we will read
changeMe=666  # global valuer we will change

def foo(param1):
print param1
print readMe# accesses the global value

def bar():
   global changeMe   # use global value rather than local
   print 'old: ', changeMe
   changeMe = 99
   print 'new: ', changeMe

print readMe, changeMe   # print original values

foo(readMe)  #prints 42 twice, once as param1, once as global value

bar()   # prints old and new values

print readMe, changeMe# print after values

##


l...still cant get even a slight idea as of what should work


Try the above and variations. If you are still stuck ask here for more 
detail.


You can also try reading the "Whats in a Name?" topic of my tutorial for 
a different explanation.



 > > peoples "player1.py" files on my game.
 >
 > Interesting concept. And if the other peoples files are calling your
 > functions with values in those parameters you will just ignore them I
 > presume? Hmmm, that should produce some interesting results...
it should work in theory correct?


No, in theory users will get unpredictable results since other people 
will assume that the values they pass into your functions will influence 
the result. They will not expect you to ignore them and apply some 
totally arbitrary values of your own. The theory is that the parameters 
in a function control the behaviour, not act as simply placeholders for 
random data. If your teacher wants the function to have certain 
parameters I suspect he wants you to use them in your function!


--
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


Re: [Tutor] blackJack problem

2012-10-23 Thread Matthew D

Thanks Alan Gauld for your help. After taking a little break and coming back it 
all seemed to make sense...i understand why there is gonna be some really weird 
outputs to the interchanging of the player1.py files...that really could get 
interesting LOL...I seemed to have got the program running good now i have only 
a few pretty simple questions, If you have the GetBet in the player1.py file 
you have to pass in the total value right?...that is the only way to get the 
total into the function? the professor told us straight out that he doesnt 
really know what he is doing...he wanted us to put in (currentBet, previousBet, 
and win/lose). I think it only makes sense if you put in the total value in as 
prevousBet. also he wanted us to pass in (player_hand, dealer_hand) in as 
parameters for the HitStand function...why would we want to total the hand more 
than once if we already have the total value? wouldn't we want to just pass in 
those values instead of the hand itself?
 Im starting to think im not really learning anything from this class...if 
anything its just confusing me and teaching me the wrong way to go about things 
thanks again for your help :)> To: tutor@python.org
> From: alan.ga...@btinternet.com
> Date: Wed, 24 Oct 2012 01:23:30 +0100
> Subject: Re: [Tutor] blackJack problem
> 
> On 24/10/12 01:00, Matthew D wrote:
> 
> >  > > I thought by having a value outside
> >  > > of the functions made the value global.
> >  >
> > ok...is there an easier way to write this? the reason i was calling that
> > global value the same as the currentBet is because i need to add that
> > value to 5...
> 
> You need to read about global variables and namespaces in Python.
> 
> But using a global name as a parameter name will not make the function 
> use the global name as the first parameter, in fact the opposite will 
> happen in that the parameter will hide the global value.
> 
> If you want to pass the global value into your function then call it 
> with the global name as the first argument. If you want to change the 
> global value from your function then you need to declare the name global 
> inside your function:
> 
> ##
> readMe = 42   # global var we will read
> changeMe=666  # global valuer we will change
> 
> def foo(param1):
>  print param1
>  print readMe# accesses the global value
> 
> def bar():
> global changeMe   # use global value rather than local
> print 'old: ', changeMe
> changeMe = 99
> print 'new: ', changeMe
> 
> print readMe, changeMe   # print original values
> 
> foo(readMe)  #prints 42 twice, once as param1, once as global value
> 
> bar()   # prints old and new values
> 
> print readMe, changeMe# print after values
> 
> ##
> 
> > l...still cant get even a slight idea as of what should work
> 
> Try the above and variations. If you are still stuck ask here for more 
> detail.
> 
> You can also try reading the "Whats in a Name?" topic of my tutorial for 
> a different explanation.
> 
> >  > > peoples "player1.py" files on my game.
> >  >
> >  > Interesting concept. And if the other peoples files are calling your
> >  > functions with values in those parameters you will just ignore them I
> >  > presume? Hmmm, that should produce some interesting results...
> > it should work in theory correct?
> 
> No, in theory users will get unpredictable results since other people 
> will assume that the values they pass into your functions will influence 
> the result. They will not expect you to ignore them and apply some 
> totally arbitrary values of your own. The theory is that the parameters 
> in a function control the behaviour, not act as simply placeholders for 
> random data. If your teacher wants the function to have certain 
> parameters I suspect he wants you to use them in your function!
> 
> -- 
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] blackJack problem

2012-10-23 Thread wrw
On Oct 23, 2012, at 11:01 PM, Matthew D  wrote:

> Thanks Alan Gauld for your help. After taking a little break and coming back 
> it all seemed to make sense...i understand why there is gonna be some really 
> weird outputs to the interchanging of the player1.py files...that really 
> could get interesting LOL...I seemed to have got the program running good
>  
> now i have only a few pretty simple questions, If you have the GetBet in the 
> player1.py file you have to pass in the total value right?...that is the only 
> way to get the total into the function? the professor told us straight out 
> that he doesnt really know what he is doing...he wanted us to put in 
> (currentBet, previousBet, and win/lose). I think it only makes sense if you 
> put in the total value in as prevousBet. also he wanted us to pass in 
> (player_hand, dealer_hand) in as parameters for the HitStand function...why 
> would we want to total the hand more than once if we already have the total 
> value? wouldn't we want to just pass in those values instead of the hand 
> itself?
>  
> Im starting to think im not really learning anything from this class...if 
> anything its just confusing me and teaching me the wrong way to go about 
> things
>  
> thanks again for your help :)


The best way to learn to program is to program.  Doing it in the context of a 
class has several advantages (even if the prof isn't the sharpest tack in the 
box)…

1)  The class discussion of various approaches to the assignment gives you a 
range of possible solutions, both good and bad

2)  The enforced discipline of generating a solution to the assignment makes 
you think

3)  Learning (almost) any programming language forces you to learn how to 
reduce a problem to discrete tasks and then steps.  Once you've learned ANY 
language, the next one is easier.  That process iterates.

4)  Python is an ideal language and environment in which to learn.  That is, 
it's a really good starting place.

Just saying…

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


Re: [Tutor] blackJack problem

2012-10-23 Thread Mark Lawrence

On 24/10/2012 04:01, Matthew D wrote:




big top post snipped.





Any chance of your prof teaching you *NOT* to top post and to use plain 
English while you're at it?


--
Cheers.

Mark Lawrence.

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


Re: [Tutor] blackJack problem

2012-10-23 Thread Matthew D


 > To: tutor@python.org
> From: breamore...@yahoo.co.uk
> Date: Wed, 24 Oct 2012 04:33:14 +0100
> Subject: Re: [Tutor] blackJack problem
> 
> On 24/10/2012 04:01, Matthew D wrote:
> >
> 
> big top post snipped.
> 
> >
>  wow was this really necessary? no need to be an ass...
> Any chance of your prof teaching you *NOT* to top post and to use plain 
> English while you're at it?
> 
> -- 
> Cheers.
> 
> Mark Lawrence.
> 
> ___
> 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] blackJack problem

2012-10-23 Thread Mark Lawrence

On 24/10/2012 04:36, Matthew D wrote:



  > To: tutor@python.org

From: breamore...@yahoo.co.uk
Date: Wed, 24 Oct 2012 04:33:14 +0100
Subject: Re: [Tutor] blackJack problem

On 24/10/2012 04:01, Matthew D wrote:




big top post snipped.




  wow was this really necessary? no need to be an ass...


If you can't be bothered to check up before you post and you can't write 
in plain English then yes.


--
Cheers.

Mark Lawrence.

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