[Tutor] Basic question on spaces

2011-07-19 Thread Alexander Quest
Hello; I'm a new student of Python using "Python Programming for Absolute
Beginners" 3rd edition by Michael Dawson as my guide. This is a basic
question regarding spaces. I'm not sure how to make it so spaces do not show
up between variables and basic strings, particularly before commas and after
dollar signs, as in the simple "tipper" program I have below.


#Tip program: calculates 15% and 20% tip for a given bill.

bill = int(input("Hello! Welcome to the tipper program. \nWhat is the amount
of "
 "your bill, in dollars please: "))

percent15 = bill * .15
percent20 = bill * .20
print("\nOkay, based on that bill, a 15% tip would be $", percent15, ", and
\n"
  "a 20% tip would be $", percent20, ".")
input("\n\nPress the enter key to exit.")



As you can see, this is quite rudimentary; I have not discovered any special
function that eliminates spaces yet, if such a function exits. The problem
is, as stated above, unwanted spaces after both dollar signs, before the
comma after '15.0' and before the period after '20.0." Apologies for asking
such a basic question, but any help will be appreciated.

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


[Tutor] Basic program question

2011-07-24 Thread Alexander Quest
Hello- I am running Python v 3.1.1. As an exercise, I wrote a simple coin
flipper program, where the computer flips a coin 100 times and then prints
out the number of heads and tails. My program crashes immediately if I run
it normally through the command line, but if I go to "Run- Run Module," it
seems to work just fine. I can't seem to figure out why. I've pasted the
relevant code below- any help will be greatly appreciated. Thanks!

import random
print("\tWelcome to the 'Coin Flipper' program!")

counter = 0
heads = 0
tails = 0

while counter < 100:
the_number = random.randint(1, 2)
if the_number == 1:
heads += 1
else:
tails += 1

counter += 1

print("\nI flipped the coint 100 times.")
print("It came up heads", heads, "times and tails", tails, "times.")

print("\n\nPress the enter key to exit.")

_

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


[Tutor] Assigning range

2011-07-27 Thread Alexander Quest
Does anyone know how to assign a certain numerical range to a variable, and
then choose the number that is the middle of that range? For example, I want
to assign the variable "X" a range between 1 and 50, and then I want to have
the middle of that range (25) return with some command when I call it
(perhaps rangemid or something like that?). In pseudocode, I am trying to
say X = range [1,50], return middle of range (which should return 25) but I
don't know how to code it. This is for a basic program I'm trying to write
where the player thinks of a number and the computer tries to guess the
number in as few tries as possible. Thanks for any help!

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


Re: [Tutor] Assigning range

2011-07-27 Thread Alexander Quest
Thanks Steven- I'll try that out.

-Alex

On Wed, Jul 27, 2011 at 5:40 PM, Steven D'Aprano wrote:

> Alexander Quest wrote:
>
>> Does anyone know how to assign a certain numerical range to a variable,
>> and
>> then choose the number that is the middle of that range? For example, I
>> want
>> to assign the variable "X" a range between 1 and 50, and then I want to
>> have
>> the middle of that range (25) return with some command when I call it
>> (perhaps rangemid or something like that?). In pseudocode, I am trying to
>> say X = range [1,50], return middle of range (which should return 25) but
>> I
>> don't know how to code it. This is for a basic program I'm trying to write
>> where the player thinks of a number and the computer tries to guess the
>> number in as few tries as possible. Thanks for any help!
>>
>
>
> Forget about using range, that just adds meaningless complexity.
>
> What is important is that you have a lower bound, and a higher bound: two
> numbers, instead of how ever many (possible thousands, or millions!) in
> range(low, high).
>
> middle = (low+high)//2
>
>
>
> --
> Steven
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<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] Assigning range

2011-07-27 Thread Alexander Quest
Thanks for that Donald!

-Alex

On Wed, Jul 27, 2011 at 8:16 PM, Donald Wilson  wrote:

> You could start with an anonymous function using the lambda operator, such
> as:
>
> 
>
> mid_range = lambda x: x[len(x) // 2]
>
> Note: If you use len(x) / 2 in python 3.x you will get a TypeError because
> the division operator / returns a float. Floor // returns an int in 2.x and
> 3.x.
>
> Then use either:
>
> x = range(1000, 4001)
> mid_x = mid_range(x) # mid_x == 2500
>
> or…
>
> mid_x = mid_range(range(500, 751)) # mid_x == 625
>
> etc. to retrieve the middle element.
>
> You can extract the mid point of any sequence type, such as a string, using
> this function.
>
> mid_x = mid_range(‘12345678987654321’) # mid_x == ‘9’
>
> 
>
> middle_number = lambda lo, hi: abs(lo - hi) // 2
>
> will work if you just need the mid point of two numbers; either ints or
> floats.
>
> mid_x = middle_number(0, 1000) # mid_x = 500
>
> DW
>
> On Jul 27, 2011, at 8:16 PM, Alexander Quest wrote:
>
> > Does anyone know how to assign a certain numerical range to a variable,
> and then choose the number that is the middle of that range? For example, I
> want to assign the variable "X" a range between 1 and 50, and then I want to
> have the middle of that range (25) return with some command when I call it
> (perhaps rangemid or something like that?). In pseudocode, I am trying to
> say X = range [1,50], return middle of range (which should return 25) but I
> don't know how to code it. This is for a basic program I'm trying to write
> where the player thinks of a number and the computer tries to guess the
> number in as few tries as possible. Thanks for any help!
> >
> > -Alex
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Running files from command prompt

2011-07-28 Thread Alexander Quest
I downloaded the google's python exercise files from their website (
http://code.google.com/edu/languages/google-python-class/set-up.html),
unzipped them, and placed them in C.
I then added the following to the PATH variable under system settings so
that I could type "python" in command prompt and have Windows start the
interpreter: C:\Python31;C:\Python31\Tools\Scripts

When I type in "python" in the command prompt, the interpreter opens, but
when I try to open one of the programs from the Google exercise files
(hello.py), I get the following error:
Traceback :
   File "", line 1, in 
NameError: name 'hello' is not defined

Or, if I just type in "python hello.py" first in the command prompt (as
opposed to typing in python, hitting enter, and THEN typing in hello.py, as
above), I get the following error:


python: can't open file 'hello.py': [Errno 2] No such file or directory.

So I guess my question is how do I run .py files from the command prompt now
that I seem to have gotten Windows to recognize and open the interpreter
when I type in "python"? Thanks for any help.

-Alex

P.S. Just as an aside, when I open up the command prompt, the initial
directory is C:\Users\Alexander, but my google exercises are in
C:\google-python-exercises and python itself is in C:\Python31. I don't know
if this makes a difference or not.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running files from command prompt

2011-07-28 Thread Alexander Quest
Awesome- thanks for that Dave! The programs all work now, except that the
google exercise programs are all from Python 2.X and I'm running 3.1, so
some of them are giving me errors. Is there a way around this or do I have
to download a 2.X version so I can run these without a problem? Thanks
again.

-Alex

On Thu, Jul 28, 2011 at 7:11 PM, Dave Angel  wrote:

> On 07/28/2011 09:58 PM, Alexander Quest wrote:
>
>> I downloaded the google's python exercise files from their website (
>> http://code.google.com/edu/**languages/google-python-class/**set-up.html<http://code.google.com/edu/languages/google-python-class/set-up.html>
>> ),
>> unzipped them, and placed them in C.
>> I then added the following to the PATH variable under system settings so
>> that I could type "python" in command prompt and have Windows start the
>> interpreter: C:\Python31;C:\Python31\Tools\**Scripts
>>
>> When I type in "python" in the command prompt, the interpreter opens, but
>> when I try to open one of the programs from the Google exercise files
>> (hello.py), I get the following error:
>> Traceback:
>>File "", line 1, in
>> NameError: name 'hello' is not defined
>>
>>
> When you're running the python interpreter, you can't just type the name of
> your script.  You need to import it
> import hello
>
> However, first it needs to be in the python's module search path.  Easiest
> way is to make
>  it your current directory.
>
> So, from a command prompt:
>
> cd C:\google-python-exercises
>
> python
>  starting Python version 
>
> >>>>import hello
>
>
>
>  Or, if I just type in "python hello.py" first in the command prompt (as
>> opposed to typing in python, hitting enter, and THEN typing in hello.py,
>> as
>> above), I get the following error:
>>
>>
>> python: can't open file 'hello.py': [Errno 2] No such file or directory.
>>
>> So I guess my question is how do I run .py files from the command prompt
>> now
>> that I seem to have gotten Windows to recognize and open the interpreter
>> when I type in "python"? Thanks for any help.
>>
>>  Similarly, before running python, change to the directory you want the
> script to run in.
> Normally, you'd do:
>
> cd c:\google-python-exercises
> python hello.py
>
>
>
>  -Alex
>>
>> P.S. Just as an aside, when I open up the command prompt, the initial
>> directory is C:\Users\Alexander, but my google exercises are in
>> C:\google-python-exercises and python itself is in C:\Python31. I don't
>> know
>> if this makes a difference or not.
>>
>>
>
> --
>
> DaveA
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running files from command prompt

2011-07-28 Thread Alexander Quest
To clarify, the particular file that was giving me trouble was the basic
"hello world" file. The original code on line 29 read as such: print
'Hello', name
When I ran "C:\google-python-exercises> python hello.py, it gave me an error
on that line (line 29), but when I changed that line to print ('Hello',
name), that is, including the parentheses, it printed out "hello world" as
it should. I'm assuming that this means that one of the differences between
Python 2.X and Python 3.X is that the print function necessitates
parentheses in the latter versions but not in the former. I am a bit
confused as to why this is, assuming I am correct in my assumption above,
because I was under the impression that code written for earlier python
versions will work for later python versions, as is the case here. Anyways,
I just wanted to add this info to clarify my last question regarding whether
or not I should install Python 2.X and uninstall Python 3.1 that I have now,
since I'm guessing that doing the google exercises will continue to give me
these errors with other programs (but this is, of course, still assuming
that the error cited above truly is caused by version incompatibility).

-Alex


On Thu, Jul 28, 2011 at 7:58 PM, Alexander Quest wrote:

> Awesome- thanks for that Dave! The programs all work now, except that the
> google exercise programs are all from Python 2.X and I'm running 3.1, so
> some of them are giving me errors. Is there a way around this or do I have
> to download a 2.X version so I can run these without a problem? Thanks
> again.
>
> -Alex
>
>
> On Thu, Jul 28, 2011 at 7:11 PM, Dave Angel  wrote:
>
>> On 07/28/2011 09:58 PM, Alexander Quest wrote:
>>
>>> I downloaded the google's python exercise files from their website (
>>> http://code.google.com/edu/**languages/google-python-class/**set-up.html<http://code.google.com/edu/languages/google-python-class/set-up.html>
>>> ),
>>> unzipped them, and placed them in C.
>>> I then added the following to the PATH variable under system settings so
>>> that I could type "python" in command prompt and have Windows start the
>>> interpreter: C:\Python31;C:\Python31\Tools\**Scripts
>>>
>>> When I type in "python" in the command prompt, the interpreter opens, but
>>> when I try to open one of the programs from the Google exercise files
>>> (hello.py), I get the following error:
>>> Traceback:
>>>File "", line 1, in
>>> NameError: name 'hello' is not defined
>>>
>>>
>> When you're running the python interpreter, you can't just type the name
>> of your script.  You need to import it
>> import hello
>>
>> However, first it needs to be in the python's module search path.  Easiest
>> way is to make
>>  it your current directory.
>>
>> So, from a command prompt:
>>
>> cd C:\google-python-exercises
>>
>> python
>>  starting Python version 
>>
>> >>>>import hello
>>
>>
>>
>>  Or, if I just type in "python hello.py" first in the command prompt (as
>>> opposed to typing in python, hitting enter, and THEN typing in hello.py,
>>> as
>>> above), I get the following error:
>>>
>>>
>>> python: can't open file 'hello.py': [Errno 2] No such file or directory.
>>>
>>> So I guess my question is how do I run .py files from the command prompt
>>> now
>>> that I seem to have gotten Windows to recognize and open the interpreter
>>> when I type in "python"? Thanks for any help.
>>>
>>>  Similarly, before running python, change to the directory you want the
>> script to run in.
>> Normally, you'd do:
>>
>> cd c:\google-python-exercises
>> python hello.py
>>
>>
>>
>>  -Alex
>>>
>>> P.S. Just as an aside, when I open up the command prompt, the initial
>>> directory is C:\Users\Alexander, but my google exercises are in
>>> C:\google-python-exercises and python itself is in C:\Python31. I don't
>>> know
>>> if this makes a difference or not.
>>>
>>>
>>
>> --
>>
>> DaveA
>>
>>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running files from command prompt

2011-07-29 Thread Alexander Quest
Alexander- thanks for the tip as to sticking with Python 3.
Steven, I greatly appreciate that breakdown. You're right about the error:
it was a syntax error on that line; I'll make sure to include the
descriptions in the future. As far as finding a new tutorial, I am going to
see if Google's class works out with Python 3.1, and if not, I'll switch
over to a different one.

-Alexander

On Thu, Jul 28, 2011 at 10:27 PM, Steven D'Aprano wrote:

> Alexander Quest wrote:
>
>> To clarify, the particular file that was giving me trouble was the basic
>> "hello world" file. The original code on line 29 read as such: print
>> 'Hello', name
>> When I ran "C:\google-python-exercises> python hello.py, it gave me an
>> error
>> on that line (line 29), but when I changed that line to print ('Hello',
>> name), that is, including the parentheses, it printed out "hello world" as
>> it should. I'm assuming that this means that one of the differences
>> between
>> Python 2.X and Python 3.X is that the print function necessitates
>> parentheses in the latter versions but not in the former.
>>
>
>
> Yes, that is correct.
>
> To be a programmer (whether professional or amateur), you need to learn to
> *pay attention to the error given*. "It gave me an error" is meaningless.
> What does the error message say?
>
> In this case, I expect it is a SyntaxError. But you need to learn to read
> the error message and understand what it is trying to tell you. Some errors
> are cryptic and don't help, but generally speaking Python is pretty good
> about giving useful error messages:
>
>
> >>> a = [1, 2, 3]
> >>> len a
>  File "", line 1
>len a
>^
> SyntaxError: invalid syntax
>
>
> Admittedly you do need to learn that Python functions require parentheses,
> but apart from that, the error tells you what is wrong: you can't follow a
> function len with another name a without something between them. This is
> illegal syntax.
>
>
>
>
>  I am a bit
>> confused as to why this is, assuming I am correct in my assumption above,
>> because I was under the impression that code written for earlier python
>> versions will work for later python versions, as is the case here.
>>
>
> Not quite. It is (mostly) true for Python 1.x and 2.x, but Python 3 has
> deliberately included some backwards incompatible changes. The biggest two
> are that strings are now Unicode rather than byte strings, and that print is
> now a function instead of a statement. So, yes, in Python 3 you have to call
> it with parentheses.
>
> The differences are still quite minor -- think of Python 2.x and Python 3.x
> being like the differences between American English and British English.
> Provided you pay attention to the error messages, and remember to add round
> brackets after print, tutorials for 2.x should still *mostly* work.
>
>
>
>  I just wanted to add this info to clarify my last question regarding
>> whether
>> or not I should install Python 2.X and uninstall Python 3.1 that I have
>> now,
>>
>
> Personally, I would consider it wiser to find a Python 3 tutorial. Python 3
> is the future, and you will need to learn it eventually.
>
>
>
>
> --
> Steven
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Indexing a list with nested tuples

2011-08-02 Thread Alexander Quest
Hi guys- I'm having a problem with a list that has nested tuples:

attributes = [("strength", 0), ("health  ", 0), ("wisdom  ", 0),
("dexterity", 0)]

I've defined the list above with 4 items, each starting with a value of 0.
The player
enters how many points he or she wants to add to a given item. The selection
menu
is 1 - strength; 2 - health; 3 - wisdom; 4- dexterity. So the "selection"
variable is actually
1 more than the index location of the intended item. So I have the following
code:

print("Added ", points, "to ", attributes[selection-1][0], "attribute.")

My intent with this is to say that I've added this many points (however
many) to the
corresponding item in the list. So if the player selects "1", then selection
= 1, but I subtract
1 from that (selection -1) to get the index value of that item in the list
(in this case 0). Then I
have [0] to indicate that I want to go to the second value within that first
item, which is the
point value. I get an error saying that list indices must be integers, not
strings. I get a similar
error even if I just put attributes[selection][0] without the minus 1.

Also, it seems that the tuple within the list cannot be modified directly,
so I can't add points to the original value of "0" that all 4 items start
with. Is there a way to keep this nested list with
tuples but be able to modify the point count for each item, or will it be
better to create a dictionary or 2 separate lists (1 for the names
"Strength, Health, Wisdom, Dexterity" and one
for their starting values "0,0,0,0")? Any suggestions/help will be greatly
appreciated!!!

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


Re: [Tutor] Indexing a list with nested tuples

2011-08-03 Thread Alexander Quest
Thanks Peter- I tried the replacement method where the entire tuple is
replaced with a new one and that worked. Changing the "attribute_index" (or
"selection" variable, as I called it) to an integer removed the int/str
errors.

-Alex

On Wed, Aug 3, 2011 at 12:12 AM, Peter Otten <__pete...@web.de> wrote:

> Alexander Quest wrote:
>
> > Hi guys- I'm having a problem with a list that has nested tuples:
> >
> > attributes = [("strength", 0), ("health  ", 0), ("wisdom  ", 0),
> > ("dexterity", 0)]
> >
> > I've defined the list above with 4 items, each starting with a value of
> 0.
> > The player
> > enters how many points he or she wants to add to a given item. The
> > selection menu
> > is 1 - strength; 2 - health; 3 - wisdom; 4- dexterity. So the "selection"
> > variable is actually
> > 1 more than the index location of the intended item. So I have the
> > following code:
> >
> > print("Added ", points, "to ", attributes[selection-1][0], "attribute.")
> >
> > My intent with this is to say that I've added this many points (however
> > many) to the
> > corresponding item in the list. So if the player selects "1", then
> > selection = 1, but I subtract
> > 1 from that (selection -1) to get the index value of that item in the
> list
> > (in this case 0). Then I
> > have [0] to indicate that I want to go to the second value within that
> > first item, which is the
> > point value. I get an error saying that list indices must be integers,
> not
> > strings. I get a similar
> > error even if I just put attributes[selection][0] without the minus 1.
> >
> > Also, it seems that the tuple within the list cannot be modified
> directly,
> > so I can't add points to the original value of "0" that all 4 items start
> > with. Is there a way to keep this nested list with
> > tuples but be able to modify the point count for each item, or will it be
> > better to create a dictionary or 2 separate lists (1 for the names
> > "Strength, Health, Wisdom, Dexterity" and one
> > for their starting values "0,0,0,0")? Any suggestions/help will be
> greatly
> > appreciated!!!
>
> [I'm assuming you are using Python 3. If not replace input() with
> raw_input()]
>
> Let's investigate what happens when you enter an attribute index:
>
> >>> attribute_index = input("Choose attribute ")
> Choose attribute 2
> >>> attribute_index
> '2'
>
> Do you note the '...' around the number?
>
> >>> attribute_index -= 1
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: unsupported operand type(s) for -=: 'str' and 'int'
>
> It's actually a string, not an integer; therefore you have to convert it to
> an integer before you can do any math with it:
>
> >>> attribute_index = int(attribute_index)
> >>> attribute_index
> 2
> >>> attribute_index -= 1
> >>> attribute_index
> 1
>
> Now let's try to change the second tuple:
>
> >>> attributes = [
> ... ("strength", 0), ("health", 0), ("wisdom", 0), ("dexterity", 0)]
> >>> attributes[attribute_index]
> ('health', 0)
> >>> attributes[attribute_index][1] += 42
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: 'tuple' object does not support item assignment
>
> The error message is pretty clear, you cannot replace items of a tuple.
> You can either to switch to nested lists
>
> [["strength", 0], ["health", 0], ...]
>
> or replace the entire tuple with a new one:
>
> >>> name, value = attributes[attribute_index]
> >>> attributes[attribute_index] = name, value + 42
> >>> attributes
> [('strength', 0), ('health', 42), ('wisdom', 0), ('dexterity', 0)]
>
> However, I think the pythonic way is to use a dictionary. If you want the
> user to input numbers you need a second dictionary to translate the numbers
> into attribute names:
>
> >>> attributes = dict(attributes)
> >>> lookup = {1: "strength", 2: "health", 3: "wisdom", 4: "dexterity"}
> >>> while True:
> ... index = input("index ")
> ... if not index: break
> ... amount = int(input("amount "))
> ... name = lookup[int(index)]
> ... attributes[name] += amount
> ...
> index 1
> amount 10
> index 2
> amount 20
> index 3
> amount 10
> index 2
> amount -100
> index
> >>> attributes
> {'dexterity': 0, 'strength': 10, 'health': -38, 'wisdom': 10}
>
> Personally I would ask for attribute names directly.
>
> ___
> 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] Indexing a list with nested tuples

2011-08-03 Thread Alexander Quest
Hi Bob- thanks for the reply again. I apologize about not "replying all"
last time- still getting in the habit of doing this.

I am using Python version 3.1. As far as tuples are concerned, I don't NEED
to use them, but I am trying to get some practice with them. This is because
I am following an instructional book that is discussing nested tuples within
lists.
The way I get the "selection" variable from the user is just by typing the
following: selection = input("Selection: ")

I'm not sure why it reads it initially as a string, but I later included the
line selection = int(selection), which solved the int/str problem.

Also, I was about to switch to dictionaries or just lists without tuples,
but another poster above stated that I could just replace the entire tuple
item within the list, which technically would not be changing the tuple, so
it worked out. The only problem I have now is trying to sort the 4
attributes based on their numerical value, not their alphabetical value. But
when I type in  attributes.sort(reverse=True), it sorts them alphabetically
because the name of the attribute is 1st in the list, and its value is 2nd.
Here it is again for reference: attributes = [("strength", 0), ("health  ",
0), ("wisdom  ", 0), ("dexterity", 0)]

Sorry if this is a bit confusing. Thanks for your help and tips so far Bob.

-Alex

On Wed, Aug 3, 2011 at 5:52 AM, bob gailer  wrote:

>  On 8/2/2011 11:39 PM, Alexander Quest wrote:
>
> Hey Bob- thanks for the reply. Here is a more complete part of that code
> section (the ellipses are parts where I've deleted code because I don't
> think it's important for this question):
>
>
> Please always reply-all so a copy goes to the list.
>
> Thanks for posting more code & traceback
>
> I forgot to mention earlier - tell us which version of Python you are using
> (this looks like version 3)
>
> You did not answer all my questions! How come? Please do so now.
>
>
>
> _
> attributes = [("strength", 0), ("health  ", 0), ("wisdom  ", 0),
> ("dexterity", 0)]
> .
> .
> .
> print(
> """
> 1 - Strength
> 2 - Health
> 3 - Wisdom
> 4 - Dexterity
>
> Any other key - Quit
> """
> )
> selection = input("Selection: ")
> if selection == "1" or selection == "2" or selection == "3" or selection ==
> "4":
> print("You have ", points, "points available.")
> how_many = input("How many would you like to add to this
> attribute?: ")
> while how_many < 0 or how_many > 30 or how_many ==
> "":   # Because max points available is 30, and
> entering less than 0 does not make sense.
> print("Invalid entry. You have ", points, "points
> available.")   # If the user enters a number
> less than 0, greater than 30, or just presses enter, it loops.
> how_many = input("How many would you like to add to
> this attribute?: ")
> print("Added ", points, "to ", attributes[selection-1][0],
> "attribute.") # Here is where I try to add the
> number of points to the value, based on what the user entered.
> points = points -
> how_many
> # I subtract the number of points added from the total points available.
> attributes[selection-1][1] +=
> how_many  # I
> add the number of points the user selected to the variable selected.
>
>
> __
>
>
> Here's the traceback I get:
>
> Traceback (most recent call last):
>   File "C:\Users\Alexander\Desktop\Python Practice\Ch05-2.py", line 54, in
> 
> print("Added ", points, "to ", attributes[selection-1][0],
> "attribute.")
> TypeError: unsupported operand type(s) for -: 'str' and 'int'
> _
>
> Thanks for any help. I understand that I can't change tuples directly, but
> is there a way to change them indirectly (like saying attribute.remove[x]
> and then saying attribute.append[x] with the new variable? But this seems to
&

Re: [Tutor] Indexing a list with nested tuples

2011-08-05 Thread Alexander Quest
My bad- meant to say [1]. Thanks.

-Alexander

On Fri, Aug 5, 2011 at 12:36 PM, Christopher King wrote:

>
>
> On Tue, Aug 2, 2011 at 10:44 PM, Alexander Quest wrote:
>>
>> have [0] to indicate that I want to go to the second value within that
>> first item, which is the
>> point value
>>
> Actually [0] is the first element. I would go with [1].
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Notepad++ question

2012-06-06 Thread Alexander Quest
Hey all; my question is regarding editing Python code in Notepad++. When I
run this piece of code in Notepad++:

def fix_start(s):
  var1 = s[0]
var2 = "*"
  var3 = s.replace(var1, var2)

  return var3


I get an indentation error, which reads:


  File "C:\google-python-exercises\google-python-exercises\basic>string1.py
line 56
var2 = "*"
^
IndentationError: unexpected indent


The thing is that in Notepad++, that code does not appear with an
indentation where var2 is. It appears like this:

def fix_start(s):
  var1 = s[0]
  var2 = "*"
  var3 = s.replace(var1, var2)

  return var3

but when I copy and paste it, it pastes with an indentation where var2 is,
which is what I think is causing the error. The code runs fine if I just
use IDLE. I am doing Google's python exercises, and they recommended I edit
the settings on Notepad++ to indent 2 spaces upon a tab, this being the
convention at Google. Does anyone know what the deal is here? Also, I am
wondering why use Notepad++ or other such programs when IDLE seems to be
fine for writing code. Thanks.

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


Re: [Tutor] Notepad++ question

2012-06-14 Thread Alexander Quest
Got it Dave- sorry about not sending it to the newsgroup as well.

My question was regarding a piece of boilerplate code:

if __name__ == '__main__':
  main()

This calls the main function, but I don't understand what the 'if'
statement is doing here. In the simple programs that I've seen this so far,
there is no variable called "_name_", and even if there was, why is it
comparing it to "_main_"? Why can't the main function just be called by
typing main()- why do we need this if statement to precede it? Thanks.

-Alex

On Thu, Jun 7, 2012 at 6:16 PM, Dave Angel  wrote:

> On 06/07/2012 02:36 PM, Alexander Quest wrote:
> > Ok, thanks guys. I also had one more quick question regarding a piece of
> > boilerplate code:
> >
>
> To get a response, you will needs to leave your question at the python
> tutor newsgroup.  We are part of a group, not offering private advice.
>
> Normally, you just do a Reply-all to one of the existing messages on the
> thread, to include the group.  But you can also typetutor@python.org
>  as a CC:
>
> I've been volunteering my time on various forums for over 25 years now,
> so I think I speak for lots of other volunteers.
>
>
> --
>
> DaveA
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Notepad++ question

2012-06-14 Thread Alexander Quest
[Resending because I messed up on last email]

My question was regarding a piece of boilerplate code:

if __name__ == '__main__':
  main()

This calls the main function, but I don't understand what the 'if'
statement is doing here. In the simple programs that I've seen this so far,
there is no variable called "_name_", and even if there was, why is it
comparing it to "_main_"? Why can't the main function just be called by
typing main()- why do we need this if statement to precede it? Thanks.

-Alex

On Thu, Jun 14, 2012 at 3:17 PM, Alexander Quest wrote:

> Got it Dave- sorry about not sending it to the newsgroup as well.
>
> My question was regarding a piece of boilerplate code:
>
>
> if __name__ == '__main__':
>   main()
>
> This calls the main function, but I don't understand what the 'if'
> statement is doing here. In the simple programs that I've seen this so far,
> there is no variable called "_name_", and even if there was, why is it
> comparing it to "_main_"? Why can't the main function just be called by
> typing main()- why do we need this if statement to precede it? Thanks.
>
> -Alex
>
> On Thu, Jun 7, 2012 at 6:16 PM, Dave Angel  wrote:
>
>> On 06/07/2012 02:36 PM, Alexander Quest wrote:
>> > Ok, thanks guys. I also had one more quick question regarding a piece of
>> > boilerplate code:
>> >
>>
>> To get a response, you will needs to leave your question at the python
>> tutor newsgroup.  We are part of a group, not offering private advice.
>>
>> Normally, you just do a Reply-all to one of the existing messages on the
>> thread, to include the group.  But you can also typetutor@python.org
>>  as a CC:
>>
>> I've been volunteering my time on various forums for over 25 years now,
>> so I think I speak for lots of other volunteers.
>>
>>
>> --
>>
>> DaveA
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Notepad++ question

2012-06-14 Thread Alexander Quest
Thanks Walter; I believe I understand the reasoning behind it, though not
all of the mechanics, but for now, your answer is more than sufficient.

-Alex

On Thu, Jun 14, 2012 at 4:10 PM, Walter Prins  wrote:

> Hi Alex,
>
> On 14 June 2012 23:18, Alexander Quest  wrote:
> > if __name__ == '__main__':
> >   main()
> >
> > This calls the main function, but I don't understand what the 'if'
> statement
> > is doing here. In the simple programs that I've seen this so far, there
> is
> > no variable called "_name_", and even if there was, why is it comparing
> it
> > to "_main_"? Why can't the main function just be called by typing main()-
> > why do we need this if statement to precede it? Thanks.
>
> In short, consider the implications of the fact that your file, apart
> from being a program that can run standalone, might also be a Python
> module that might be used in another program.  Oftentimes you want to
> write your Python code in such a way that when the module is run
> directly you want it to do something useful (such as run a main()
> function, e.g. maybe run some unit/self-tests or whatever), while when
> you import it for use in another program/module then you probably
> rather do *not* want it to run as if it is itself the "main program".
> So, in order to differentiate the 2 cases, there exists the above
> Python idiom.  So, when a module is directly run as the "main
> program", then the name of the module being run, which is reflected by
> the variable __name__, made available by the Python interpreter, will
> be equal to "__main__", while when it's imported it will be equal to
> the module name.  This allows your module to know when it's running
> whether it's running as the main program or just running because it's
> been imported by another module.
>
> Does that answer your question?
>
> Walter
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re.findall question

2012-06-26 Thread Alexander Quest
I'm a bit confused about extracting data using re.search or re.findall.

Say I have the following code: tuples =
re.findall(r'blahblah(\d+)yattayattayatta(\w+)moreblahblahblah(\w+)over',
text)

So I'm looking for that string in 'text', and I intend to extract the parts
which have parentheses around them. And it works: the variable "tuples",
which I assigned to get the return of re.findall, returns a tuple list,
each 'element' therein being a tuple of 3 elements (which is what I wanted
since I had 3 sets of parentheses).

My question is how does Python know to return just the part in the
parentheses and not to return the "blahblah" and the "yattayattayatta",
etc...? The 're.search' function returns the whole thing, and if I want
just the parentheses parts, I do tuples.group(1) or tuples.group(2) or
tuples.group(3), depending on which set of parentheses I want. Does the
re.findall command by default ignore anything outside of the parentheses
and only return the parentheses as a grouping withing one tuple (i.e., the
first element in "tuples" would be, as it is, a list comprised of 3
elements corresponding respectively to the 1st, 2nd, and 3rd parentheses)?
Thank you for reading.

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