Re: [Tutor] don't understand iteration

2014-11-10 Thread Peter Otten
Clayton Kirkwood wrote:

> 
> 
>>-Original Message-
>>From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
>>Behalf Of Peter Otten
>>Sent: Sunday, November 09, 2014 5:47 PM
>>To: tutor@python.org
>>Subject: Re: [Tutor] don't understand iteration
>>
>>Clayton Kirkwood wrote:
>>
>>> I have the following code:
>>
>>>  blah =
>>> re.search(r'<\w\w>(\w{3}\.)\s+(\d{2}),\s+(\d{2}).+([AP]M)\s+(E[SD]T)',
>>> line)
>>
>>>  (month, day, time, ap, offset) = blah.group(1,2,3,4,5) This
>>> works fine, but in the (month... line, I have blah.group(1,2,3,4,5),
>>> but this is problematic for me. I shouldn't have to use that 1,2,3,4,5
>>> sequence. I tried to use many alternatives using:  range(5) which
>>> doesn't work, list(range(5)) which actually lists the numbers in a
>>> list, and several others. As I read it, the search puts out a tuple. I
>>> was hoping to just assign the re.search to month, day, time, ap,
>>> offset directly. Why wouldn't that work? Why won't a range(5) work? I
>>> couldn't find a way to get the len of blah.
>>
>>> What am I missing?
>>
>>
>>
>>While the direct answer would be
>>
>>month, day, time, ap, offset = blah.group(*range(1,6))
>>
>>there is also the groups() method
>>
>>month, day, time, ap, offset = blah.groups()
>>
>>which is appropriate when you want to unpack all capturing groups.
>>
> 
> Still seems odd to me. Blah is a tuple, and would think that there should
> be a natural way to pull it apart. 

It's not a tuple it's a

>>> type(re.search("", ""))


This type could implement iteration over its groups -- but it doesn't:

>>> list(re.search("", ""))
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '_sre.SRE_Match' object is not iterable

> One can iterate across a string or list
> easily, why not a tuple. I also would have thought that a range(5) would
> have worked. Isn't it an iterable?

group(*range(5)) gives you groups 0...4, but 0 is special (the whole match

>>> re.search("(a+)(b+)", "xxxaaabbzzz").group(0)

)and you don't seem to want that.

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


Re: [Tutor] don't understand iteration

2014-11-10 Thread Alan Gauld

On 10/11/14 00:34, Clayton Kirkwood wrote:


 if 'EST' in line or 'EDT' in line:  # look for Eastern Time
   blah = 
re.search(r'<\w\w>(\w{3}\.)\s+(\d{2}),\s+(\d{2}).+([AP]M)\s+(E[SD]T)', line)
   (month, day, time, ap, offset) = blah.group(1,2,3,4,5)

<_sre.SRE_Match object; span=(0, 28), match='Nov. 09, 07:15:46 PM EST'>

  PM Nov. EST 09 07



blah.group(1,2,3,4,5), but this is problematic for me. I shouldn’t have
to use that 1,2,3,4,5 sequence.


Why not? You have a hard coded search string so why not hard code
the corresponding group numbers? It's a lot more explicit and
hence reliable than using range() and it's faster too.

range() is great if you have to generate a long sequence (ie. tens or 
hundreds or more) of numbers or, more importantly, if you don't know

in advance how many numbers you need. But in your case its fixed by
the regex pattern you use.


  range(5) which doesn’t work, list(range(5)) which actually lists the
numbers in a list, and several others. As I read it, the search puts out
a tuple.


No, it 'puts out' (ie returns) a Match object which is nothing like a 
tuple. You have to access the result data using the methods of the object.


blah.groups() returns all the groups as a tuple, which seems to be
what you want, so

(month, day, time, ap, offset) = blah.groups()

should do what you want.


I couldn’t find a way to get the len of blah.


What would you expect the answer to be?
- The number of matches?
- The length of the matched strings?
- the lengths of the individual groups?

blah.span(0)

might be what you want but I'm not sure.

Incidentally, I don't think this has much to do with iteration as in the 
subject line, it's more about using regular expressions.


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

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


Re: [Tutor] don't understand iteration

2014-11-10 Thread Clayton Kirkwood


>-Original Message-
>From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
>Behalf Of Alan Gauld
>Sent: Monday, November 10, 2014 4:20 AM
>To: tutor@python.org
>Subject: Re: [Tutor] don't understand iteration
>
>On 10/11/14 00:34, Clayton Kirkwood wrote:
>
>>  if 'EST' in line or 'EDT' in line:  # look for Eastern Time
>>blah =
>re.search(r'<\w\w>(\w{3}\.)\s+(\d{2}),\s+(\d{2}).+([AP]M)\s+(E[SD]T)',
>line)
>>(month, day, time, ap, offset) = blah.group(1,2,3,4,5)
>>
>> <_sre.SRE_Match object; span=(0, 28), match='Nov. 09, 07:15:46 PM
>> EST'>
>>
>>   PM Nov. EST 09 07
>
>> blah.group(1,2,3,4,5), but this is problematic for me. I shouldn't
>> have to use that 1,2,3,4,5 sequence.
>
>Why not? You have a hard coded search string so why not hard code the
>corresponding group numbers? It's a lot more explicit and hence reliable
>than using range() and it's faster too.


Yes, great in a specific case such as this, but I am trying to learn the
general case: what if instead of 5 I had 25. I surely don't want to have to
type out every number in a program. Programs are there to do for me, not me
for it:<)) And again, this is why it would be useful to have a len or sizeof
a match object or tuple as argued below.

>
>range() is great if you have to generate a long sequence (ie. tens or
>hundreds or more) of numbers or, more importantly, if you don't know in
>advance how many numbers you need. But in your case its fixed by the
>regex pattern you use.
>
>>   range(5) which doesn't work, list(range(5)) which actually lists the
>> numbers in a list, and several others. As I read it, the search puts
>> out a tuple.

My mistake, search and match put out a match object. Match.group/s returns a
tuple for more than one argument returned.


>
>No, it 'puts out' (ie returns) a Match object which is nothing like a
>tuple. You have to access the result data using the methods of the
>object.



Also of confusion, the library reference says:

Match objects always have a boolean value of True. Since match() and
search() return None when there is no match, you can test whether there was
a match with a simple if statement:

match = re.search(pattern, string)
if match:
process(match)


blah =
re.search(r'<\w\w>(\w{3})\.\s+(\d{2}),\s+(\d{2}).+([AP]M)\s+(E[SD]T)', line)
>>> blah
<_sre.SRE_Match object; span=(45, 73), match='Nov. 09, 07:15:46 PM EST'>
>>> if blah: print(blah)
<_sre.SRE_Match object; span=(45, 73), match='Nov. 09, 07:15:46 PM EST'>
>>> if blah == True: print(blah)>
No print out

To me, this doesn't *appear* to be quite true.


>blah.groups() returns all the groups as a tuple, which seems to be what
>you want, so
>
>(month, day, time, ap, offset) = blah.groups()
>
>should do what you want.


Aye, that does it.


>> I couldn't find a way to get the len of blah.
>
>What would you expect the answer to be?
>- The number of matches?
>- The length of the matched strings?
>- the lengths of the individual groups?


I would expect len(sizeof, whatever)(blah) to return the number of (in this
case) matches, so 5. Doing a search suggests what is important: the number
of matches. Why else would you do a search, normally.
That could then be used in the range()
It would be nice to have the number of arguments.
I would expect len(blah.group()) to be 5, because that is the relevant
number of elements returned from group. And that is the basic thing that
group is about; the groups, what they are and how many there are. I
certainly wouldn't want len(group) to return the number of characters, in
this case, 28 (which it does:>{{{


>>> blah.group()
'Nov. 09, 07:15:46 PM EST'
>>> len(blah.group())
28

I didn't run group to find out the number of characters in a string, I ran
it to find out something about blah and its matches.


>
>blah.span(0)
>
>might be what you want but I'm not sure.
>
>Incidentally, I don't think this has much to do with iteration as in the
>subject line, it's more about using regular expressions.


No, call it pink, but it all has to do with knowing how many matches in a
re, or the count of tuples from a group.

Just some thoughts leaking out

Clayton
>
>--
>Alan G
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
>http://www.flickr.com/photos/alangauldphotos
>
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor



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


[Tutor] Help with Dice game

2014-11-10 Thread coryloghry
Hello, I can not for the life of me figure out where I have gone wrong.  I 
wrote the following code as a simulation for the table top game x-wing.  It 
basically simulates dice rolls but the issue is the fact that every time I 
choose a number of dice to roll, they all hit.  None of them ever miss.  Thank 
You.


import random
print("X-wing dice simulator")
x = int(input("How many dice will the offensive player be rolling?\n"))
y = int(input("How many dice will the defensive player be rolling?\n"))
hits = 0
crits = 0
dodges = 0
offense = 0
defense = 0
while offense < x:
odie = random.randint(1,8)
if odie <= 4:
hits = hits + 1
offense = offense + 1
if odie == 4:
crits = crits + 1
offense = offense + 1
else:
continue
while defense < y:
ddie = random.randint(1,8)
if ddie <= 3:
dodges = dodges + 1
defense = defense + 1
else:
continue
print("The offensive player lands", hits,"hits and", crits,"crits\n")
print("The defensive player dodges", dodges, "hits\n")
print("The offensive player deals", int((hits + crits) - dodges), "to the 
defensive player")









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


[Tutor] “has a value of True” versus “evaluates true” (was: don't understand iteration)

2014-11-10 Thread Ben Finney
"Clayton Kirkwood"  writes:

> Also of confusion, the library reference says:
>
> Match objects always have a boolean value of True. Since match() and
> search() return None when there is no match, you can test whether there was
> a match with a simple if statement:
>
> match = re.search(pattern, string)
> if match:
> process(match)

The documentation is incorrect, as you point out: “have a boolean value
of True” implies that the value is identical to the built-in ‘True’
constant, which is never the case for these objects.

Instead, the passage above should say “evaluates true in a boolean
context”.

Would you be so kind as to report a bug to that effect
http://bugs.python.org/>?

-- 
 \   “The Vatican is not a state.… a state must have people. There |
  `\are no Vaticanians.… No-one gets born in the Vatican except by |
_o__)an unfortunate accident.” —Geoffrey Robertson, 2010-09-18 |
Ben Finney

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


Re: [Tutor] don't understand iteration

2014-11-10 Thread Alan Gauld

On 10/11/14 23:08, Clayton Kirkwood wrote:


I couldn't find a way to get the len of blah.


What would you expect the answer to be?


I would expect len(sizeof, whatever)(blah) to return the number of (in this
case) matches, so 5.


But remember that search is matching the pattern, not the groups - 
that's a side effect. So len(matchObject) could just as validly

return the length of the string that matched the pattern.
It just depends on the content that you are using.


of matches. Why else would you do a search


To look for a pattern. I very rarely use groups in regex - and
I try not to use regex at all! If I can find the matching substring
I will likely be able to pull out the details using regular string 
methods or other tools. For example in your case I might have tried 
using time.strparse()




certainly wouldn't want len(group) to return the number of characters, in
this case, 28 (which it does:>{{{


But group() - with default group of 0 - returns the whole matched 
substring and len() on a string returns the number of characters.

You want len(match.groups()) to get the number of matches.


I didn't run group to find out the number of characters in a string, I ran
it to find out something about blah and its matches.


But group() - singular - returns a single group item which is always a 
string. You use group() to get the matching substring. You use groups to 
find all the substrings.



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

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


Re: [Tutor] Help with Dice game

2014-11-10 Thread Alan Gauld

On 10/11/14 20:57, corylog...@yahoo.com.dmarc.invalid wrote:


I wrote the following code as a simulation for the table top game
x-wing.


I don;t know it so can only give some general comments below...


import random
print("X-wing dice simulator")
x = int(input("How many dice will the offensive player be rolling?\n"))
y = int(input("How many dice will the defensive player be rolling?\n"))


Might be better to use more descriptive names like 'offense_turns'
and defense_turns'?


hits = 0
crits = 0
dodges = 0
offense = 0
defense = 0



while offense < x:
 odie = random.randint(1,8)


Is it really an 8 sided dice?


 if odie <= 4:
 hits = hits + 1
 offense = offense + 1
 if odie == 4:
 crits = crits + 1
 offense = offense + 1
 else:
 continue


You don't need the else: continue, the loop does that by itself.
Also by not incrementing the offense counter you effectively let the 
player have unlimited misses - is that really what you want?


Finally, the Pythonic idiom for incrementing a counter, n, is

n += 1

It saves a few keystrokes...


while defense < y:
 ddie = random.randint(1,8)
 if ddie <= 3:
 dodges = dodges + 1
 defense = defense + 1
 else:
 continue


Same comments as above

To help you debug this it might be worth adding a few print statements 
inside the loop, like so:


while offense < x:
   odie = random.randint(1,8)
   print('odie is: ',odie)
   if odie <= 4:
   hits = hits + 1
   offense = offense + 1
   print('hits, offense = ',hits,offense)


print("The offensive player lands", hits,"hits and", crits,"crits\n")
print("The defensive player dodges", dodges, "hits\n")
print("The offensive player deals", int((hits + crits) - dodges), "to
the defensive player")


Doing all the printing after the loop finishes gives you a
very limited view of what's happening inside.

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

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


Re: [Tutor] don't understand iteration

2014-11-10 Thread Clayton Kirkwood


>-Original Message-
>From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
>Behalf Of Ben Finney
>Sent: Sunday, November 09, 2014 8:25 PM
>To: tutor@python.org
>Subject: Re: [Tutor] don't understand iteration
>
>"Clayton Kirkwood"  writes:
>
>> >-Original Message-
>> >From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
>> >Behalf Of Dave Angel
>
>(Clayton, does your mail client not present messages written by their
>authors? The messages should not come to you “From:” the tutor list
>itself. It's awkward to follow whom you're quoting.)
>
>> >(month, day, time, ap, offset) = blah.group( *list (range (1, 6)))
>> >
>> >Should do it.
>
>> Where did you find that one?
>
>Working through the Python Tutorial (actually doing the examples, and
>working to understand them before moving on) teaches these and other
>Python concepts https://docs.python.org/3/tutorial/>.
>
>> What does the '*' do/why?
>
>Learn about sequence unpacking in function parameters at the Tutorial
>https://docs.python.org/3/tutorial/controlflow.html#tut-unpacking-
>arguments>.


This seems to be the only relevant words:
4.7.4. Unpacking Argument Lists

The reverse situation occurs when the arguments are already in a list or tuple 
but need to be unpacked for a function call requiring separate positional 
arguments. For instance, the built-in range() function expects separate start 
and stop arguments. If they are not available separately, write the function 
call with the *-operator to unpack the arguments out of a list or tuple:
>>> list(range(3, 6))# normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> list(range(*args))# call with arguments unpacked from a list
[3, 4, 5]

I fail to see the reference to '* list' except for possibly the '*-operator', 
for which there is no example or further guidance of this far too often used 
ability. And possibly later the reference to '*args' which appears to act as a 
wildcard/expander/replacement for the more exacting [3,6].

The only other places where I saw something like a '*list' was way off in 
PyObject and daughters suggesting a C char * or pointer. I'm sure I don't need 
to get into that just to find a reference to something so obvious as a '*list', 
something that I run across all of the time.

I'm gettin' older and I can see out of one eye and not so good out of the other 
so I have to rotate the page 180 degrees to read the other half of the page and 
sometimes readin' upside down gives me difficulty and I miss '*'s every once in 
a while.

Clayton

PS, I'd still like to find a discussion and usage examples of this all too 
common '*list'. Thanks



>
>> And why the weird range thing?
>
>Learn about the ‘range’ built-in at the Python library reference
>https://docs.python.org/3/library/functions.html#func-range>.
>
>--
> \ “I went to the museum where they had all the heads and arms |
>  `\  from the statues that are in all the other museums.” —Steven |
>_o__)   Wright |
>Ben Finney
>
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor


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


Re: [Tutor] don't understand iteration

2014-11-10 Thread Clayton Kirkwood


>-Original Message-
>From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
>Behalf Of Alan Gauld
>Sent: Monday, November 10, 2014 3:59 PM
>To: tutor@python.org
>Subject: Re: [Tutor] don't understand iteration
>
>On 10/11/14 23:08, Clayton Kirkwood wrote:
>
 I couldn't find a way to get the len of blah.
>>>
>>> What would you expect the answer to be?
>>
>> I would expect len(sizeof, whatever)(blah) to return the number of (in
>> this
>> case) matches, so 5.
>
>But remember that search is matching the pattern, not the groups -
>that's a side effect. So len(matchObject) could just as validly return
>the length of the string that matched the pattern.
>It just depends on the content that you are using.

Good point, although a nice side effect would be an added reference like
search.matches which when accessed would return the value. It knows the
value, it just isn't being made available.

>
>> of matches. Why else would you do a search
>
>To look for a pattern. I very rarely use groups in regex - and I try not
>to use regex at all! If I can find the matching substring I will likely
>be able to pull out the details using regular string methods or other
>tools. For example in your case I might have tried using time.strparse()

I'll explore

>
>
>> certainly wouldn't want len(group) to return the number of characters,
>> in this case, 28 (which it does:>{{{
>
>But group() - with default group of 0 - returns the whole matched
>substring and len() on a string returns the number of characters.
>You want len(match.groups()) to get the number of matches.
>
>> I didn't run group to find out the number of characters in a string, I
>> ran it to find out something about blah and its matches.
>
>But group() - singular - returns a single group item which is always a
>string. You use group() to get the matching substring. You use groups to
>find all the substrings.

I believe that is true only if you are receiving a single return value. If
it is more than one group, it returns a tuple (I guess of strings).


Clayton
>
>
>--
>Alan G
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
>http://www.flickr.com/photos/alangauldphotos
>
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor



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


Re: [Tutor] don't understand iteration

2014-11-10 Thread Alan Gauld

On 11/11/14 00:28, Clayton Kirkwood wrote:



This seems to be the only relevant words:
4.7.4. Unpacking Argument Lists

...when the arguments are already in a list or tuple but need to be unpacked
> > for a function call  If they are not available separately, 
write the function

> call with the *-operator to unpack the arguments
> out of a list or tuple:


list(range(*args))# call with arguments unpacked from a list

[3, 4, 5]


So it tells you that when you need to extract the arguments out of a 
list or tuple use the * operator and it gives an example of unpacking 
the list [3,6] (ie. args) when using the range() function.



I fail to see the reference to '* list' except for possibly the '*-operator',


That's the one.


for which there is no example


There is, the one above using it in range()


 later the reference to '*args' which appears to act as a

> wildcard/expander/replacement for the more exacting [3,6].

Its not a wild card but it is an expander. it says "unpack
the sequence and treat the members as individual values"


I saw something like a '*list' was way off in PyObject and daughters

> suggesting a C char * or pointer.

pointers in C are unrelated. Thankfully...


I'm sure I don't need to get into that just to find a reference

> to something so obvious as a '*list',


something that I run across all of the time.


Oddly I don't see it that often. but I'd agree its not a totally
rare feature, just not an every day one. Which is why it gets a
passing coverage in the tutorial.

For (slightly) more detail you can read the language reference:

https://docs.python.org/3/reference/compound_stmts.html#function-definitions

Where it says:
---
Function call semantics are described in more detail in section Calls. A 
function call always assigns values to all parameters mentioned in the 
parameter list, either from position arguments, from keyword arguments, 
or from default values. If the form “*identifier” is present, it is 
initialized to a tuple receiving any excess positional parameters, 
defaulting to the empty tuple. If the form “**identifier” is present, it 
is initialized to a new dictionary receiving any excess keyword 
arguments, defaulting to a new empty dictionary. Parameters after “*” or 
“*identifier” are keyword-only parameters and may only be passed used 
keyword arguments.



And the link to Calls:

https://docs.python.org/3/reference/expressions.html#calls

which says:
--
f there are more positional arguments than there are formal parameter 
slots, a TypeError exception is raised, unless a formal parameter using 
the syntax *identifier is present; in this case, that formal parameter 
receives a tuple containing the excess positional arguments (or an empty 
tuple if there were no excess positional arguments).


If any keyword argument does not correspond to a formal parameter name, 
a TypeError exception is raised, unless a formal parameter using the 
syntax **identifier is present; in this case, that formal parameter 
receives a dictionary containing the excess keyword arguments (using the 
keywords as keys and the argument values as corresponding values), or a 
(new) empty dictionary if there were no excess keyword arguments.


If the syntax *expression appears in the function call, expression must 
evaluate to an iterable. Elements from this iterable are treated as if 
they were additional positional arguments; if there are positional 
arguments x1, ..., xN, and expression evaluates to a sequence y1, ..., 
yM, this is equivalent to a call with M+N positional arguments x1, ..., 
xN, y1, ..., yM.


A consequence of this is that although the *expression syntax may appear 
after some keyword arguments, it is processed before the keyword 
arguments (and the **expression argument, if any – see below). So:

>>>
>>> def f(a, b):
...  print(a, b)
...
>>> f(b=1, *(2,))
2 1
>>> f(a=1, *(2,))
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: f() got multiple values for keyword argument 'a'
>>> f(1, *(2,))
1 2

It is unusual for both keyword arguments and the *expression syntax to 
be used in the same call, so in practice this confusion does not arise.


If the syntax **expression appears in the function call, expression must 
evaluate to a mapping, the contents of which are treated as additional 
keyword arguments. In the case of a keyword appearing in both expression 
and as an explicit keyword argument, a TypeError exception is raised.


Formal parameters using the syntax *identifier or **identifier cannot be 
used as positional argument slots or as keyword argument names.


--



Which may or may not help! :-)

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

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

Re: [Tutor] Help with Dice game

2014-11-10 Thread Lifeng Lin
I am not familiar with the game, but maybe using "offense += 1" and
"defense += 1" to replace the corresponding "continue" would help?

On Mon, Nov 10, 2014 at 2:57 PM,  wrote:

>  Hello, I can not for the life of me figure out where I have gone wrong.
> I wrote the following code as a simulation for the table top game x-wing.
> It basically simulates dice rolls but the issue is the fact that every time
> I choose a number of dice to roll, they all hit.  None of them ever miss.
> Thank You.
>
> import random
> print("X-wing dice simulator")
> x = int(input("How many dice will the offensive player be rolling?\n"))
> y = int(input("How many dice will the defensive player be rolling?\n"))
> hits = 0
> crits = 0
> dodges = 0
> offense = 0
> defense = 0
> while offense < x:
> odie = random.randint(1,8)
> if odie <= 4:
> hits = hits + 1
> offense = offense + 1
> if odie == 4:
> crits = crits + 1
> offense = offense + 1
> else:
> continue
> while defense < y:
> ddie = random.randint(1,8)
> if ddie <= 3:
> dodges = dodges + 1
> defense = defense + 1
> else:
> continue
> print("The offensive player lands", hits,"hits and", crits,"crits\n")
> print("The defensive player dodges", dodges, "hits\n")
> print("The offensive player deals", int((hits + crits) - dodges), "to the
> defensive player")
>
>
>
> Sent from Windows Mail
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] don't understand iteration

2014-11-10 Thread Alan Gauld

On 11/11/14 00:52, Clayton Kirkwood wrote:


But group() - singular - returns a single group item which is always a
string. You use group() to get the matching substring. You use groups to
find all the substrings.


I believe that is true only if you are receiving a single return value. If
it is more than one group, it returns a tuple (I guess of strings).


Yes, sorry. Thats what I meant. groups returns a tuple of substrings. So 
len(groups) is the len that you are looking for - the number of groups 
found.


But you should already know the answer since you are creating the groups 
in your pattern and the search only returns a valid match object if all 
the groups match. So the number of groups found is always the number of 
groups defined.


So unless you are doing something really messy, like defining the regex 
dynamically and adding groups on the fly you rarely need to find out how 
many groups are returned, because you wrote the pattern in the first place!


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

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


Re: [Tutor] “has a value of True” versus “evaluates true” (was: don't understand iteration)

2014-11-10 Thread Clayton Kirkwood
I reported it. I feel all grown up now. Kind of like one of the boys(girls...)

Clayton:<)


>-Original Message-
>From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
>Behalf Of Ben Finney
>Sent: Monday, November 10, 2014 3:24 PM
>To: tutor@python.org
>Subject: [Tutor] “has a value of True” versus “evaluates true” (was:
>don't understand iteration)
>
>"Clayton Kirkwood"  writes:
>
>> Also of confusion, the library reference says:
>>
>> Match objects always have a boolean value of True. Since match() and
>> search() return None when there is no match, you can test whether
>> there was a match with a simple if statement:
>>
>> match = re.search(pattern, string)
>> if match:
>> process(match)
>
>The documentation is incorrect, as you point out: “have a boolean value
>of True” implies that the value is identical to the built-in ‘True’
>constant, which is never the case for these objects.
>
>Instead, the passage above should say “evaluates true in a boolean
>context”.
>
>Would you be so kind as to report a bug to that effect
>http://bugs.python.org/>?
>
>--
> \   “The Vatican is not a state.… a state must have people. There |
>  `\are no Vaticanians.… No-one gets born in the Vatican except by |
>_o__)an unfortunate accident.” —Geoffrey Robertson, 2010-09-18 |
>Ben Finney
>
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor


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


[Tutor] Project tree

2014-11-10 Thread Wiktor Matuszewski

Hi,
let's assume I have this project tree:

project_name/
 |-src/
 |  |- __init__.py
 |  |- moda.py
 |  '- modb.py
 '- start.py

And individual files contain:

- modb.py: -
def hello(txt):
return "Hello " + txt + "!"

def plus1(num):
return num + 1


- moda.py: -
import modb

def hello_world():
return modb.hello("World")


- start.py: -
from src import moda, modb

print(moda.hello_world())
print(modb.plus1(41))


__init__.py is empty

(it's project for my purpose - I don't want to distribute it with pypi, 
I pulled out start.py form src folder to just run everything without 
opening src folder)


Ok, so now executing start.py works in Python 2.7(*), but doesn't work 
in Python 3.4(**). Can someone, please, explain me why? What changed 
between 2.x and 3.x versions?


*) - gives output:
Hello World!
42

**) - gives error message:
Traceback (most recent call last):
  File "E:\tests\project_name\start.py", line 1, in 
from src import moda
  File "E:\tests\project_name\src\moda.py", line 1, in 
import modb
ImportError: No module named 'modb'

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


Re: [Tutor] Project tree

2014-11-10 Thread Danny Yoo
> Traceback (most recent call last):
>   File "E:\tests\project_name\start.py", line 1, in 
> from src import moda
>   File "E:\tests\project_name\src\moda.py", line 1, in 
> import modb
> ImportError: No module named 'modb'


Hi Wiktor,


In Python 3, imports are not relative by default.  You might need to
adjust your imports to be explicitly relative.

Concretely, within moda.py, you'll want to change:

import modb

to:

from . import modb

See: https://docs.python.org/3/tutorial/modules.html#intra-package-references
for more details.

If you have more questions, please feel free to ask!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] don't understand iteration

2014-11-10 Thread Clayton Kirkwood


>-Original Message-
>From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
>Behalf Of Ben Finney
>Sent: Sunday, November 09, 2014 8:25 PM
>To: tutor@python.org
>Subject: Re: [Tutor] don't understand iteration
>
>"Clayton Kirkwood"  writes:
>
>> >-Original Message-
>> >From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
>> >Behalf Of Dave Angel
>
>(Clayton, does your mail client not present messages written by their
>authors? The messages should not come to you “From:” the tutor list
>itself. It's awkward to follow whom you're quoting.)

I think it does, I am replying to your email just as stated in the above 
indented header.

>
>> >(month, day, time, ap, offset) = blah.group( *list (range (1, 6)))
>> >
>> >Should do it.
>
>> Where did you find that one?
>
>Working through the Python Tutorial (actually doing the examples, and
>working to understand them before moving on) teaches these and other
>Python concepts https://docs.python.org/3/tutorial/>.

Actually Ben, I do turn to the documentation first and foremost, and only after 
hours of trying things and their variations do I grovel and come here.

>
>> What does the '*' do/why?
>
>Learn about sequence unpacking in function parameters at the Tutorial
>https://docs.python.org/3/tutorial/controlflow.html#tut-unpacking-
>arguments>.

As I said above.

>
>> And why the weird range thing?
>
>Learn about the ‘range’ built-in at the Python library reference
>https://docs.python.org/3/library/functions.html#func-range>.
>
>--
> \ “I went to the museum where they had all the heads and arms |
>  `\  from the statues that are in all the other museums.” —Steven |
>_o__)   Wright |
>Ben Finney
>
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor


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


Re: [Tutor] don't understand iteration

2014-11-10 Thread Clayton Kirkwood


>-Original Message-
>From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
>Behalf Of Alan Gauld
>Sent: Monday, November 10, 2014 5:07 PM
>To: tutor@python.org
>Subject: Re: [Tutor] don't understand iteration
>
>On 11/11/14 00:28, Clayton Kirkwood wrote:
>
>
>> This seems to be the only relevant words:
>> 4.7.4. Unpacking Argument Lists
>>
>> ...when the arguments are already in a list or tuple but need to be
>> unpacked
> > > for a function call  If they are not available separately,
>write the function  > call with the *-operator to unpack the arguments
>> out of a list or tuple:
>
> list(range(*args))# call with arguments unpacked from a
>list
>> [3, 4, 5]
>
>So it tells you that when you need to extract the arguments out of a
>list or tuple use the * operator and it gives an example of unpacking
>the list [3,6] (ie. args) when using the range() function.
>
>> I fail to see the reference to '* list' except for possibly the
>> '*-operator',
>
>That's the one.
>
>> for which there is no example
>
>There is, the one above using it in range()

Now that is really useful. It becomes quite obvious...not.

>
>>  later the reference to '*args' which appears to act as a
> > wildcard/expander/replacement for the more exacting [3,6].
>
>Its not a wild card but it is an expander. it says "unpack the sequence
>and treat the members as individual values"
>
>> I saw something like a '*list' was way off in PyObject and daughters
> > suggesting a C char * or pointer.
>
>pointers in C are unrelated. Thankfully...
>
>> I'm sure I don't need to get into that just to find a reference
> > to something so obvious as a '*list',
>
>> something that I run across all of the time.
>
>Oddly I don't see it that often. but I'd agree its not a totally rare
>feature, just not an every day one. Which is why it gets a passing
>coverage in the tutorial.

Uh, humor. In all my life I have never run across that until now.


>
>For (slightly) more detail you can read the language reference:
>
>https://docs.python.org/3/reference/compound_stmts.html#function-
>definitions

So you make my point, it is something in the deep dark past of animal history, 
yet to be seen by modern man. Don't tell Ben, as he seems to think that it is 
post modern knowledge.


>> *list(range(1,6))
  File "", line 1
SyntaxError: can use starred expression only as assignment target
>>> *list(range(*6))
  File "", line 1
SyntaxError: can use starred expression only as assignment target
>>> a = *list(range(*6))
  File "", line 1
SyntaxError: can use starred expression only as assignment target
>>> a = *list(range(5))
  File "", line 1
SyntaxError: can use starred expression only as assignment target
>>> a = list(*range(5))
Traceback (most recent call last):
  File "", line 1, in 
TypeError: list() takes at most 1 argument (5 given)
>>> a = list(range(*5))
Traceback (most recent call last):
  File "", line 1, in 
TypeError: type object argument after * must be a sequence, not int


As far as I can tell, everything that has been suggested doesn't work:
Has to be only in assignment target, I supply an assignment, still doesn't 
work. Perhaps the vaunted try it in the console, doesn't work. I am sure that I 
am missing something, but dang if I know what it is. I am not asking for the 
solution, I am asking for someplace that explains this incongruity.


>
>Where it says:
>---
>Function call semantics are described in more detail in section Calls. A
>function call always assigns values to all parameters mentioned in the
>parameter list, either from position arguments, from keyword arguments,
>or from default values. If the form “*identifier” is present, it is
>initialized to a tuple receiving any excess positional parameters,
>defaulting to the empty tuple. If the form “**identifier” is present, it
>is initialized to a new dictionary receiving any excess keyword
>arguments, defaulting to a new empty dictionary. Parameters after “*” or
>“*identifier” are keyword-only parameters and may only be passed used
>keyword arguments.
>
>
>And the link to Calls:
>
>https://docs.python.org/3/reference/expressions.html#calls
>
>which says:
>--
>f there are more positional arguments than there are formal parameter
>slots, a TypeError exception is raised, unless a formal parameter using
>the syntax *identifier is present; in this case, that formal parameter
>receives a tuple containing the excess positional arguments (or an empty
>tuple if there were no excess positional arguments).
>
>If any keyword argument does not correspond to a formal parameter name,
>a TypeError exception is raised, unless a formal parameter using the
>syntax **identifier is present; in this case, that formal parameter
>receives a dictionary containing the excess keyword arguments (using the
>keywords as keys and the argument values as corresponding values), or a
>(new) empty dictionary if there were no excess keyword arguments.
>
>If the synt

Re: [Tutor] http question

2014-11-10 Thread Clayton Kirkwood


>-Original Message-
>From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
>Behalf Of Steven D'Aprano
>Sent: Sunday, November 09, 2014 3:04 AM
>To: tutor@python.org
>Subject: Re: [Tutor] http question
>
>On Sat, Nov 08, 2014 at 09:53:33PM -0800, Clayton Kirkwood wrote:
>
>> >> but I also am aware of httplib2, but it still seems to be in
>> >> eternal alpha.
>> >
>> >What leads you to that conclusion? If you're talking about this:
>> >
>> >https://github.com/jcgregorio/httplib2
>> >
>> >I don't see any sign that it is alpha version software. According to
>> >the readme file, it is at version 0.8.
>> >
>> >I don't see any signs that the author publicly releases any alpha or
>> >beta versions, they all appear to be ready for production. But if you
>> >have seen something that suggests otherwise, please point it out,
>> >because I'm happy to be corrected.
>>
>> Well, I work from the premise that 0.anything is still a work in
>> progress
>
>All software is always a work in progress, until such time it is
>abandoned.

And how do you determine the abandoned timestamp? If I remember correctly,
this hasn't been updated for several years, and a job for a customer
shouldn't be based on 0.*, years old hypothetical's. It sounds like a very
usable product.

>
>> and hasn't gotten to a point where the author is comfortable with
>> general use. I am sure that you disagree.
>
>In the FOSS (Free and Open Source Software) community, version 0.x does
>not always carry connotations of being unready for use. It may, or it
>may not. But normally "alpha" software will have an "a" in the version
>number, e.g. 0.7a, 0.7b for beta, 0.7rc1 (release candidate 1), 0.7 is
>ready for production.
>
>What matters is not my opinion, or yours, but that of the author of the
>software, and I don't know what that is.
>
>
>--
>Steve
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor



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


Re: [Tutor] “has a value of True” versus “evaluates true” (was: don't understand iteration)

2014-11-10 Thread wesley chun
good catch, and definitely a distinction beginners should be more cognizant
of.

it's also good to recognize that a call to "bool(match)" would render that
statement correct, as the built-in/factory function will return what an
object evaluates to (True [re.match object] or/vs.False [None]).

On Mon, Nov 10, 2014 at 5:31 PM, Clayton Kirkwood 
wrote:

> I reported it. I feel all grown up now. Kind of like one of the
> boys(girls...)
>
> Clayton:<)
>
>
> >-Original Message-
> >From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
> >Behalf Of Ben Finney
> >Sent: Monday, November 10, 2014 3:24 PM
> >To: tutor@python.org
> >Subject: [Tutor] “has a value of True” versus “evaluates true” (was:
> >don't understand iteration)
> >
> >"Clayton Kirkwood"  writes:
> >
> >> Also of confusion, the library reference says:
> >>
> >> Match objects always have a boolean value of True. Since match() and
> >> search() return None when there is no match, you can test whether
> >> there was a match with a simple if statement:
> >>
> >> match = re.search(pattern, string)
> >> if match:
> >> process(match)
> >
> >The documentation is incorrect, as you point out: “have a boolean value
> >of True” implies that the value is identical to the built-in ‘True’
> >constant, which is never the case for these objects.
> >
> >Instead, the passage above should say “evaluates true in a boolean
> >context”.
> >
> >Would you be so kind as to report a bug to that effect
> >http://bugs.python.org/>?
> >
> >--
> > \   “The Vatican is not a state.… a state must have people. There |
> >  `\are no Vaticanians.… No-one gets born in the Vatican except by |
> >_o__)an unfortunate accident.” —Geoffrey Robertson, 2010-09-18 |
> >Ben Finney
> >
> >___
> >Tutor maillist  -  Tutor@python.org
> >To unsubscribe or change subscription options:
> >https://mail.python.org/mailman/listinfo/tutor
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
+wesley chun  : wescpy at gmail : @wescpy

Python training & consulting : http://CyberwebConsulting.com
"Core Python" books : http://CorePython.com
Python blog: http://wescpy.blogspot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor