[Tutor] Doing this in reverse?

2008-09-13 Thread Alan Gilfoy
I found a script at http://code.activestate.com/recipes/65212/ that  
allows you to convert base 10 numbers to another base. I would like to  
convert non-base10 numbers to base 10. I wonder if I can do so by  
flipping the script around a bit:


# (Lovingly) ripped off from a reply to the post at
# http://code.activestate.com/recipes/65212/
# The function code is from that page
# User Interface + comments created by Alan Gilfoy, 2007-2008

def baseconvert(n, base):
"""convert positive decimal integer n to equivalent in another  
base (2-36)"""


digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# a thru z only apply in base 11 and up.
# base 11 uses only a, base 12 uses a thru b, base 13 uses a thru  
c, and so on.

# 'a' is equivalent to '10' in decimal, 'b' is equivalent to '11', etc.

try:
n = int(n)
base = int(base)
except:
return ""

if n < 0 or base < 2 or base > 36:
return ""

s = ""
while 1:
r = n % base
s = digits[r] + s
n = n / base
if n == 0:
break

return s


I'm not entirely sure how this works; I just know that it does.
It looks like the program goes through a loop that "chops away" at the  
inputted number, gradually adding to the outputted number as  
appropriate.

And what exactly does the % operator mean?



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Having trouble with a component of the random module

2008-08-31 Thread Alan Gilfoy
In this case, I was trying to pull 1 card from a 5-card pool, which  
should have worked. There was a typo in my code that I have since  
fixed, and it is working.


I have to make sure manually that I'm not telling the program to pick  
more cards than exist in the list. I can do this two ways: Make the  
list long enough, and tell the selector function to pick a small  
enough amount from that pool.


The following is not related to my code per se, but rather to an  
aspect of MTG booster packs that I have successfully replicated:


In most cases, every common appears at the same frequency as every  
other common, so I normally put each name on the commons list exactly  
once. (and so on for other lists/other rarity levels) Thus, each card  
will be displayed only once per pack, unless I repeat the same name in  
the source list multiple times. (The program sees those repetitions as  
different entities.)


Quoting OmerT <[EMAIL PROTECTED]>:


Bob, Kent,

I understood all that from the context and yet do not feel familiar
enough with the information nor had the time to lookup.

The point I'm making is that Alan has a logical problem with his
generator regarding the question of repetitive cards in a booster:
On one hand, the script does not allow to repeat cards. On the other-
he tries to display 8 cards out of a 5 card pool, which is impossible
without repeating cards.

That's all..

On Sun, Aug 31, 2008 at 4:00 PM, bob gailer <[EMAIL PROTECTED]> wrote:

OmerT wrote:


I'm unfamiliar with the random.sample function, yet I can't help but
wonder:
Do you ever got the same card twice?



Quoting the manual:

sample( population, k)
Return a k length list of unique elements chosen from the population
sequence. Used for random sampling without replacement.

--
Bob Gailer
Chapel Hill NC 919-636-4239

When we take the time to be aware of our feelings and needs we have more
satisfying interatctions with others.

Nonviolent Communication provides tools for this awareness.

As a coach and trainer I can assist you in learning this process.

What is YOUR biggest relationship challenge?








___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Having trouble with a component of the random module

2008-08-31 Thread Alan Gilfoy

I shouldn't get the same card twice in the same batch, no.
Of course, in separate trials, I may get the same card again.

Basically, I type the names of relevant cards in a list, and proceed  
to "tell" my program which list to read, and how many cards to pick  
from that list.


Normally, I put each card name in the list only once. But if I type  
the same card name twice, the program sees the repetitions as  
different entities, and may display both of 'em if the randomness  
works out that way.


random.sample I think runs dependent trials (the first pick affects  
the odds of the second pick, etc.) - kind of like pulling tickets from  
a hat.

This is as opposed to independent random trials a la dice rolling.

Someone correct me if I'm wrong on this - for this application, I want  
dependent trials.


Quoting OmerT <[EMAIL PROTECTED]>:


I'm unfamiliar with the random.sample function, yet I can't help but wonder:
Do you ever got the same card twice?
Is that intentional ?



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] It works! (my booster pack generator)

2008-08-28 Thread Alan Gilfoy
Yeah, I did a lot of python work for my high school's Senior Project  
(so, spring 2007); hadn't coded much since then, was rusty on the  
"pritn stattements for debugging" part.


And I was trying to look at the code mentioned in the traceback, but  
due to my own mistake, I hadn't detected the problems with the lines  
that the traceback was pointing to.


Quoting Kent Johnson <[EMAIL PROTECTED]>:

On Thu, Aug 28, 2008 at 11:49 AM, Alan Gilfoy   
<[EMAIL PROTECTED]> wrote:

The booster pack generator (my use of random.sample as described in a
previous message) is working as intended now.

There was a rather stupid typo in my code that was causing it to go wonky.

Y'all still helped, though. A few people replied, but a bit of advice from:
W W <[EMAIL PROTECTED]> pointed me in the right direction.


As well as the lesson about using print statements to debug, I hope
you have learned to believe the interpreters error messages. It
usually knows what it is talking about :-)

Kent





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] It works! (my booster pack generator)

2008-08-28 Thread Alan Gilfoy
The booster pack generator (my use of random.sample as described in a  
previous message) is working as intended now.


There was a rather stupid typo in my code that was causing it to go wonky.

Y'all still helped, though. A few people replied, but a bit of advice  
from: W W <[EMAIL PROTECTED]> pointed me in the right direction.


for card in random.sample(SpecialA, SA):
 print card

he suggested changing to:

print "Special: ", SpecialA, "\nSA: ", SA
for card in random.sample(SpecialA, SA):
 print card

Basically, what my program was doing:

1. Take a textfile, import it/convert it to a list via  
open("filename.txt).readlines() This list was defined as the variable  
"SpecialA_list".

2. Feed SpecialA_list, along with some other variables, into my function
3. "SpecialA" variable is set equal to a list consisting of items from  
"SpecialA_list"

4. Then sample from "SpecialA" & print the results.

Thanks to the print statement that WW suggested, I figured out that  
the "SpecialA" variable was coming out as an empty list. [error in  
Step 3 of the above process list, so Step 4 had nothing to sample  
*from*, thus triggering the traceback].
I found the typo that was causing this, and fixed it. So, the SpecialA  
variable was set equal to the expected list, so random.sample could  
sample properly.


Thank you. :)


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Having trouble with a component of the random module

2008-08-28 Thread Alan Gilfoy

Quoting W W <[EMAIL PROTECTED]>:



The number of items I want from a list is smaller than the population
(number of items) in the list, so it should work.

In this specific case, I'm asking for one item from a five-item list.


Are you sure?

change this:
 for card in random.sample(SpecialA, SA):

to this:

print "Special: ", SpecialA, "\nSA: ", SA
for card in random.sample(SpecialA, SA):

then give us the output. I'll bet that your SA value is never reset to 1.

There is a part of my front-end code that declares the SA variable as  
equal to 1 for the card set in question, and feeds that into the  
processing function.

Changing the block of code, as you suggest, still gets me a traceback:

Special:  []
SA:  1

Traceback (most recent call last):
  File "C:\Documents and Settings\Alan\My  
Documents\_Alan_Programming\trunk\BoosterPackMaker\BoosterPackMaker.py", line  
104, in 
SixList_PackChooser(C_list, UC_list, R_list, BL_list,  
SpecialA_list, SpecialB_list, C, UC, R, BL, SA, SB)
  File "C:\Documents and Settings\Alan\My  
Documents\_Alan_Programming\trunk\BoosterPackMaker\PackGenerator.py",  
line 367, in SixList_PackChooser

for card in random.sample(SpecialA, SA):
  File "C:\Python25\lib\random.py", line 303, in sample
raise ValueError, "sample larger than population"
ValueError: sample larger than population

So, I'm asking my program to pick 1 item from a 0-item list. How there  
are zero items in the list, I don't know. That must be the problem.




You've obviously researched enough to know this:

mylist = [1, 2, 3, 4, 5]
from random import sample
sample(mylist, 3)

[2, 1, 4]

sample(mylist, 10)

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.5/random.py", line 303, in sample
raise ValueError, "sample larger than population"
ValueError: sample larger than population

Okay, asking for 3 items from a 5-item list works, and asking for 10  
items from a 5-item list doesn't work. It should be trying to pick 1  
item from the 5-item SpecialA list, but somehow the SpecialA list has  
zero items in it.


So give the print statement before your for loop a try and see what you come
up with.


Okay, I cut'n'pasted it before the traceback message.


HTH,
Wayne


Thank you.
---
SixList_PackChooser(C_list, UC_list, R_list, BL_list, SpecialA_list,  
SpecialB_list, C, UC, R, BL, SA, SB)


^ The error occurs in this function.
The program picks C number of items from C_list and displays them just fine.
The program picks UC number of items from UC_list and displays them just fine.
The program picks R number of items from R_list and displays them just fine.
The program picks BL number of items from BL_list and displays them just fine.

Looking at my user-interface/front-end, it looks like the function's  
being fed proper values.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Having trouble with a component of the random module

2008-08-27 Thread Alan Gilfoy
One of my for-fun Python products is a booster-pack generator for  
trading card games (specifically, Magic the Gathering, the one game of  
the genre that I play heavily)
Somewhat like baseball cards, the cards in a pack are a  
somewhat-random selection of cards from the set in question, broken  
down by rarity levels.


There are anywhere from two to six rarity levels/groupings per set;  
this depends on the set in question.
I have a function for 2-list sets, another for 3, another for 4,  
another for 5, and another for 6.


By lists I mean the following: If a set has, say, 2 rarity levels in  
it, I make 2 lists, one per rarity level. Each list is a text file  
that has the names of each card of that rarity level in that set, one  
per line. (There are no blank lines.)


My function is set up kind of like this: Import the lists - via  
open("filename.txt").readlines()
This action creates a List, with strings as the list elements, one  
string/element for each line of the imported text file


I use random.sample to make the selection, random.sample(population, k)

"
for card in random.sample(Common, C):
print card
"

Common is the 1st list, with the names of the commons in the set as  
the items in that list. The C variable is an integer, the amount of  
commons in a booster pack of that set. Say, if there are 11 commons in  
a booster pack of that set, I feed 11 as the value for C, and the code  
snippet above proceeds to print 11 randomly-selected names from the  
Commons list


My program does the same thing for the second list (Uncommon), the  
third list (Rare), the fourth list (BasicLand), the fifth list  
(SpecialA), and the sixth list (SpecialB).


For card sets that use two, three, or four lists, the results are  
displayed just fine.


For card sets that use five or six lists, the results for the first  
four are displayed just fine, and then I get this:


The code that processes the 1st, 2nd, 3rd and 4th lists is very  
similar to the code that processes the 5th and 6th, so I don't see  
what's happening.


Traceback (most recent call last):
  File "C:\Documents and Settings\Alan\My  
Documents\_Alan_Programming\trunk\BoosterPackMaker\BoosterPackMaker.py", line  
104, in 
SixList_PackChooser(C_list, UC_list, R_list, BL_list,  
SpecialA_list, SpecialB_list, C, UC, R, BL, SA, SB)
  File "C:\Documents and Settings\Alan\My  
Documents\_Alan_Programming\trunk\BoosterPackMaker\PackGenerator.py",  
line 367, in SixList_PackChooser

for card in random.sample(SpecialA, SA):
  File "C:\Python25\lib\random.py", line 303, in sample
raise ValueError, "sample larger than population"
ValueError: sample larger than population

Looking at the mentioned line in random.py, I don't see how my program  
is triggering the ValueError.


The number of items I want from a list is smaller than the population  
(number of items) in the list, so it should work.


In this specific case, I'm asking for one item from a five-item list.

Overall, more code is involved that I think can be represented in a  
few cut'n'paste snippets; how would I go about providing the files in  
question for interested parties to look at?
(There's the Python file for my front-end UI code, the Python module  
file for my processing function, and the relevant text files)


Apologizing for message length; I'm just trying to clearly explain the  
problem that I want assistance on.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] file(), open()

2007-05-26 Thread Alan Gilfoy
I want to work with simple ASCII text files, here..

I know you get them loaded into Python with file() or open()

The arguments for file() and open() are: (filename, mode, buffering).
How do you refer to the filename? Do you put it in quotes? Do you put  
in the file's full directory path? Or do file() and open() refer to  
the same directory as the one the script is in?
Are the mode and buffering things always necessary arguments?

For now, I just want to read/manipulate files, not write to them.

What do file() and open() create? Do you need to set them equal to  
some variable? Is one better than the other?

I want to create a list, where each list item is a line in the file.  
(The, my program would do stuff with the list.)





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Starting with wxPython

2007-05-22 Thread Alan Gilfoy
I'm not quite there yet, but I want to get into using GUIs in Python  
soon, and I plan to use wxPython to do so. What, do you think, is the  
best tutorial out there for wxPython?

Also, what happens when you code for some GUI feature and run the  
script in your IDE, such as IDLE? Will the graphical features properly  
appear?





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I want some help with arrays...

2007-05-17 Thread Alan Gilfoy
Why Python starts counting at [0] instead of at [1] is a whole other issue. :D

array = [["0.0", "0.1"], ["1.0", "1.1"]]

array[0[1]] seems right, although it isn't, because the index (0) and  
the subindex(1) are nested in 'array[0[1]]' much like the list and  
sublist that I'm "calling" from with the indexes.

array[0][1] works instead? Gotcha.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I want some help with arrays...

2007-05-17 Thread Alan Gilfoy
Ah, a simple concept. Good. :)
I'm already familiar in working with lists.

Here's a case of a simple two-dimensional array (the simplest  
possible), ran in the IDLE interpreter:

>>> array = [["1.1", "1.2"], ["2.1", "2.2"]]
>>> array[1[2]]

Traceback (most recent call last):
   File "", line 1, in 
 array[1[2]]
TypeError: 'int' object is unsubscriptable
>>> array[1]
['2.1', '2.2']
>>> second_half = array[1]
>>> second_half[1]
'2.2'

When I nest the slices ( array[1[2]] ) I get that error message. When  
I define one variable as an index value of the array, and then  
index-value that, it works fine.

What's the deal?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] I want some help with arrays...

2007-05-17 Thread Alan Gilfoy
What is the best (only?) way to set up an array in Python.
I've heard they can be quite good for certain types of data you need  
to organize...
What IS the best reason(s) to be using an array?
Apologies, if Python has something similar by a different name.





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Checking/Debugging tools

2007-05-12 Thread Alan Gilfoy
Quoting Kent Johnson <[EMAIL PROTECTED]>:

> Alan Gilfoy wrote:
>> My programs often have long, detailed loops in them, and would like  
>>to, as I'm executing the loop, view what part of the loop Python  
>>   is  currently processing.
>>
>> Thus, if my program gets stuck in one part of the loop, I would see that.
>> Thus, if one part of my loop is never triggered, I would see that.
>>
>> I could use, within my loop, print 'I am here, at part X of the
>> loop', but I believe that would be far to clunky to put in every
>> section of  my program's loop(s).
>
> I'm not really sure what you expect this view to look like. I don't
> know of any tool that will let you dynamically watch a program as it
> executes. Some alternatives:
>
> - A debugger lets you step through the code and see how it behaves.
> winpdb is a pretty nice GUI-based Python debugger and some Python
> development tools have built-in debuggers.
> http://www.digitalpeers.com/pythondebugger/

I got winpdb downloaded, but when I tried to run the script, it said I
needed wxpython. I tried to go to the wxpytohn website, and it's
evidently down right now.
>
> - If the program is stuck in a loop, pressing control-C will abort the
> loop and give a stack trace showing you where it was.
>
> - The Python profiler will tell you (after the fact) how much time you
> spend in each function. To see what part of a loop the program is in
> you would have to break the loop up into functions.
> http://docs.python.org/lib/profile.html

My loop isn't broken into functions, just a truckload of if statements
>
> - A code coverage tool will tell you (after the fact) which lines of
> the program were executed and which were not. (google python code
> coverage)
>
Running that Google Search, I seem to be running into a lot of what
is, to me, technical gobbeldygook. Can y'all help me choose a good
code-coverage tool, and a good tutorial?

> Kent

Alan


- End forwarded message -


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Repeating an action

2007-05-12 Thread Alan Gilfoy
How do you 'tell' Python to repeat a certain action X amount of times,  
and then stop.

I could use a 'counter' (see below), but that seems kind of clunky.



counter = 0
example = True
while example:

 print "Do something"
 counter += 1

 if counter == 100:
 example = False




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Loop-checking question

2007-05-12 Thread Alan Gilfoy
My programs often have long, detailed loops in them, and would like  
to, as I'm executing the loop, view what part of the loop Python is  
currently processing.

Thus, if my program gets stuck in one part of the loop, I would see that.
Thus, if one part of my loop is never triggered, I would see that.

I could use, within my loop, print 'I am here, at part X of the loop',  
but I believe that would be far to clunky to put in every section of  
my program's loop(s).
-- 
"Computers were the first God-Satan collaboration project."
"Blind faith in bad leadership is not patriotism."

"One of the most horrible features of war is that all the war-propaganda, all
the screaming and lies and hatred, comes invariably from people who are not
fighting."-George Orwell, _Homage to Catalonia





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Another string-manipulation question

2007-05-09 Thread Alan Gilfoy
Given a string, how would I?:

1. Make sure only the first letter string_name[0], is capitalized.
This would involve using string_name.lower() to lowercase everything  
else, but how do I use .upper(), or some other method, to capitalize  
only the first character?

2. Make sure that there are no symbols (non-letter, non-number) in the  
string, and, if one is found, remove it.

Pseudocode time, as to a potential approach-

for each character in the string:
 if character not in  
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
 remove it
-- 
"Computers were the first God-Satan collaboration project."
"Blind faith in bad leadership is not patriotism."

"One of the most horrible features of war is that all the war-propaganda, all
the screaming and lies and hatred, comes invariably from people who are not
fighting."-George Orwell, _Homage to Catalonia





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Assembling multiple strings into one

2007-05-09 Thread Alan Gilfoy
I have a program producing a list of multiple strings.
The amount of strings in the list varies.
I want to assemble a string that is:

list item 0 + space + list item 1 + space,
and so on, going through every string in the list.
-- 
"Computers were the first God-Satan collaboration project."
"Blind faith in bad leadership is not patriotism."

"One of the most horrible features of war is that all the war-propaganda, all
the screaming and lies and hatred, comes invariably from people who are not
fighting."-George Orwell, _Homage to Catalonia





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Working with error messages

2007-05-06 Thread Alan Gilfoy
I have a number-to-Roman numeral program that churns out ValueError  
messages with a few improper input cases:

1. not an integer
2. larger than 3999
3. smaller than 0

When I run the program via IDLE, and I give one of these improper  
inputs, the interpreter closes down the program and then displays the  
appropriate ValueError message.

I would like the appropriate ValueError message to be displayed before  
the program shuts down, or at least a generic ValueError message.

Is looking at my specific pieces of code necessary to help with this?

I want this because if the program's being run as an application, the  
application window closes down as soon as the program closes, and the  
user doesn't get to see the message. [When I doubleclick on the .py  
file in Windows Explorer, it runs as a .exe, for example.]
-- 
"Computers were the first God-Satan collaboration project."
"Blind faith in bad leadership is not patriotism."

"One of the most horrible features of war is that all the war-propaganda, all
the screaming and lies and hatred, comes invariably from people who are not
fighting."-George Orwell, _Homage to Catalonia





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] The IDLE subprocess

2007-04-29 Thread Alan Gilfoy
Often, when I am developing code, I get an error message saying that  
"IDLE's subprocess can't make connection"

Sometimes this happends when I have IDLE open, and am about to hit F5  
to run a program-in-process.
Sometimes it happens when I opne up IDLE the first time.

It's often fixed on restart of the program, not even restart of the computer.

A big deal or not?
And what does the subprocess do, anyway?
-- 
"Computers were the first God-Satan collaboration project."
"Blind faith in bad leadership is not patriotism."

"One of the most horrible features of war is that all the war-propaganda, all
the screaming and lies and hatred, comes invariably from people who are not
fighting."-George Orwell, _Homage to Catalonia





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A string-manipulation question

2007-03-29 Thread Alan Gilfoy
Awesome! (I was hoping it would be a 1-line solution. :)
Thanks to batteries beign included, I don't necessarily need to worry  
about why and how .spilt() works. :)

Quoting Bob Gailer <[EMAIL PROTECTED]>:

> Alan Gilfoy wrote:
>> Hi. I want to learn how to "break down" a string into its component  
>>   words, and then process each word somehow.
>>
>> Is there a way in Python to separate a string into its component words.
>>
>> Like this:
>> "I would like to convert an English string (sentence) into Pig Latin."
>>
>> The Pig Latin conversion I think I can handle already (if not, I'll  
>>   cross that bridge when I come to it.) However, what I want help  
>> on  is  how to make each word of the string a separate value, or at  
>>  least make it able to index the string by word, as opposed to the   
>> standard method  of strings being indexed by character.
>>
>
> "I would like to convert an English string (sentence) into Pig   
> Latin.".split()
> yields:
> ["I", "would", "like", "to", "convert", "an", "English", "string",
> "(sentence)", "into", "Pig Latin."]
>
>
> -- 
> Bob Gailer
> 510-978-4454



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] A string-manipulation question

2007-03-29 Thread Alan Gilfoy
Hi. I want to learn how to "break down" a string into its component  
words, and then process each word somehow.

Is there a way in Python to separate a string into its component words.

Like this:
"I would like to convert an English string (sentence) into Pig Latin."

The Pig Latin conversion I think I can handle already (if not, I'll  
cross that bridge when I come to it.) However, what I want help on is  
how to make each word of the string a separate value, or at least make  
it able to index the string by word, as opposed to the standard method  
of strings being indexed by character.
-- 
"Computers were the first God-Satan collaboration project."
"Blind faith in bad leadership is not patriotism."

"One of the most horrible features of war is that all the war-propaganda, all
the screaming and lies and hatred, comes invariably from people who are not
fighting."-George Orwell, _Homage to Catalonia





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] String-manipulation question

2007-03-11 Thread Alan Gilfoy
#start of triple-letter processing block
if len(roman_input) > 2 and roman_input[0] + roman_input[1] +  
roman_input[2] == "MMM":
 digital_result += 3000
 roman_input = #something to remove those "M"'s from the string
 continue

-- 
My question here boils down to, "Is there a way to remove certain  
characters from a string?"

Here, I'm trying to set the variable roman_input (the string's name)  
equal to the string minus those characters, the 3 Ms. (being the first  
three characters  of the string, they would be roman_input[0],  
roman_input[1], and roman_input[2].)

Setting the (new) string equal to the (old) string minus those three  
characters will enable calculations on the string to keep running,  
because I have this piece of code, and similar pieces nested in a loop.





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Roman Numeral - To - Digital Converter

2007-03-11 Thread Alan Gilfoy
Roman Numeral - To - Digital Converter

So far, I'm coming along OK with the code that runs the conversions,  
assuming the user gives me a proper input. (A string of capital  
letters I, V, X, L, C, D, M)

However, not every input someone gives the function is goign to be  
properly formatted like that.

So:

1. How do I have my function "screen" an input string for characters  
that aren't I,V,X,L,C,D or M?
2. If any of the characters in the string are merely lowercase  
i,v,x,l,c,d, or m, how would I tell Python to capitalize those and go  
on with working the function?
-- 
"Blind faith in bad leadership is not patriotism."

"One of the most horrible features of war is that all the war-propaganda, all
the screaming and lies and hatred, comes invariably from people who are not
fighting."-George Orwell, _Homage to Catalonia





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Digital-to- Roman (pseudocode)

2007-03-08 Thread Alan Gilfoy
Quoting Bob Gailer <[EMAIL PROTECTED]>:

>>
>> Digital to Roman pseudocode:
>>
>> 1. if digital_input is greater than 1000:
>> subtract 1000 from it and add "M" to string roman_result
>> # How do you do that, add one character to the end of an existing string?
>>
> Start with an empty string:
>
> roman_result = ""
>
> To add a character at the end:
>
> roman_result += "M" # Python shorthand for roman_result = roman_result + "M"
>
roman_result + "M" would also work, right/ I'm just trying to save  
time on typing in the code, right
>
>> # also, how do I modify the digital_input variable (it's an integer)
> digital_input -= 1000

is that somewhat like digital_result = digital result - int(1000)?

>> several times through the conversion process?
>>
> You will be processing the input in a loop (while or for).

running = True and
while running

is how I've tended to set

(my pseudocode)

> As you gain familiarity with Python you will develop ways to separate
> data from logic. I might say more about this later, but right now I'm
> about to drive north a bit.
>
I have already gained some familiary with separating data manipulation  
(data and logic, as a whole) from the code for the user interface,  
that's something that oen of my advisors has been big on.



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Digital-to- Roman (pseudocode)

2007-03-08 Thread Alan Gilfoy
Quoting Bob Gailer <[EMAIL PROTECTED]>:

> Make sure each step
> is one simple operation. Walk thru the steps to verify that you have
> them correct.

Bob, your email did inspire me on how I would express the process in  
computer-processing terms.

Here's how I'd break down the steps, using "pseudocode".
I might also be askign how to convert each bit of pseudocode into actual code.

(For both, I'm going to program in "exceptions" for numbers less than  
0 or greater than 3999, which is MMMCMXCIX in Roman numerals. That is  
the highest number that can be expressed in Roman numerals using  
strings of no morer than 3 of the same letter (standard rule for Roman  
numerals), and using only the symbols I, V, X, L, C, D and M.)
I know there are ways to express a Roman numeral for 5,000 and higher,  
but I'm goign to pgram those in later.

Digital to Roman pseudocode:

1. if digital_input is greater than 1000:
subtract 1000 from it and add "M" to string roman_result
# How do you do that, add one character to the end of an existing string?
# also, how do I modify the digital_input variable (it's an integer)  
several times through the conversion process?

if digital_input is less than 1000:
is it greater than 900? If so, subtract 900 from digital_input and add  
"CM" to string roman_reuslt

is it less than 900?
If it's less than 900, but greater than 500, subtract 500, and add "D"  
to the string.

If it's less than 900, and less than 500, is it greater than 400?
If so, subtract 400 from input and add "CD" to the string.
If it isn't greater than 400, but greater than 100, subtract 100 from  
the input and add "C" to the result string.

is it less than 100?
If it is, but it's greater than 90, subtract 90 and add "XC" to the string.
if it is less than 90, but greater than 50, subtract 50, and add "L"  
to the string.

(and so on, down from 50 to 1)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Roman to digital (pseudocode)

2007-03-08 Thread Alan Gilfoy
This, I heard, is more difficult than digital-to-Roman, since you have  
to "read" the subtractive cases, with a smaller numeral placed before  
a larger numeral, without simply adding all the numerals' values up

I'm going to use a raw_input prompt to ask the user which Roman  
numeral he/she wants to convert. How do I "screen" for inputs that  
have characters besides "I", "V", "X", "L", "C", "D", or "M"?

Second Python question:

I know there's a way to "find out" the name of the first item in a list
(ListName[0]), but is there a way to find out the first character of a string?

Also, is there a way to "ask" Python what characters are before and  
after the character in the string that you're asking about?

For example, usign the sample string "MCMXVII" (1917):

How would you ask Python:
"What's the 3rd character in this string?" (returns "M")
"What's before that character?" (returns "C")

Pseudocode time:

If character Y is "M":
and the character before character Y is "C",
add 900 to digital_result
and remove that "C" and that "M" from the string.
#How would you do *that*?

and the character before character Y is another "M", or if character Y  
is the first character in the string,
add 1000 to digital_result
and remove that "M" from the string.




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] my program problem!

2006-11-26 Thread Alan Gilfoy
Hey! Glad to hear that using raw_input() at the end is the actual  
solution, not just a jury-rigged workaround. :)
So sleep(10) would simply be a shorter snippet of code for that purpose?

The \n thing might be handy if I was short on space, but I don't mind  
separate 'print' lines, I find that my code seems easier to read that  
way. (at least to me)
I understand that having highly readable code is a ratehr desirable  
quality...:)
-- 
"Blind faith in bad leadership is not patriotism."

"One of the most horrible features of war is that all the war-propaganda, all
the screaming and lies and hatred, comes invariably from people who are not
fighting."-George Orwell, _Homage to Catalonia_





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] program spontaneously closes...

2006-11-25 Thread Alan Gilfoy
Hello All.
I've been concocting some simple programs based on an online tutorial,  
and I, of course, save a program as a .py file.

When running my program via IDLE, once I've cleaned out the bugs, it  
works as I expect it to.

But I noticed something else:

Clicking directly on the file's icon has the program open in the  
Windows command line program as a .exe file.

The command line program seems to work through my program OK, but the  
command line program shuts down immediately after the program has run  
through, as opposed to staying open in order to display the result. [I  
noticed that if the command line program is not waiting for a resposne  
to a prompt, it stays open.]

Relevant code block:

#rectangle stuff
length = int(raw_input("Please enter the length of your rectangle, as  
a whole number : ")) #lets user input a number
width = int(raw_input("Please enter the width of your rectangle, as a  
whole number : ")) #lets user input a number
area = length * width
print " "
print " "
print " "
print "the length is"
print length
print " "
print "the width is"
print width
print " "
print "So the area is"
print area
print " "

end = (raw_input("Type something, and press Enter, and the program  
should shut down."))
#workaround




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] My number-guessing program

2006-10-02 Thread Alan Gilfoy
Yeah, that's what the wiki-based tutorial mentioned.
In fact, Lee Harr (my community adviser) also suggested using a random number.

Quoting Luke Paireepinart <[EMAIL PROTECTED]>:

> Alan Gilfoy wrote:
(the start of the code for my number-guessing thingamajig)
>>
>> else:
>>print "The number-guessing loop is over."
>>
(some more code)

My line: number = 3
Would I use 'number = random.randint()' in place of that, to have the 
relevant number be random?


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] senior project

2006-10-01 Thread Alan Gilfoy
I am a senior at the School Without Walls in Rochester, NY. As such, I 
get to do a senior project. I decided to work on computer programming, 
since that is sometihng that I certianly think can hold my interest 
throughout the school year.

(despite my compute rinteresat, I've never done anything much more 
advanced than HTML, for some reason)

One of my community advisers suggested I start with Python. (he also 
suggested this mailing list)

So far, I've liked the 
http://swaroopch.info/text/Byte_of_Python:Main_Page tutorial, for 
basics.





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] One of my 'baby step' programs

2006-10-01 Thread Alan Gilfoy
-code block-

number = 3
running = True

while running:
   guess = int(raw_input("Please enter a number : ")) #lets user guess a number

   if guess == number:
   print "Yay, you got the right number, good for you. But you 
don't get any prizes. Do I look like a walking ATM to you?"
   running = False #this causes the guess-again loop to start.
   elif guess < number:
   print "No, my number is higher than that"
   else:
   print "No, my number is lower than that."

else:
   print "The number-guessing loop is over."

print "Done."

-end code block-

Darnit, it's very simple, but I still like the fac tthat is does 
something to user input.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor