Rafael Knuth | 24 May 2013 13:01
Picon

Issue w/ program: Flip a coin and count heads and tails

Hello,

I am writing a program in Python 3.3.0 which flips a coin 10 x times and then counts the number of heads and tails. It obviously does something else than I intended, and I am wondering what I did wrong:

import random

print ("""

This program flips a coin 10 times.

It then counts the number of heads and tails.

""")

flips = 0

heads = 0

tails = 0

while flips < 10:

    flips = flips + 1

    if random.randint(1,2) == 1:

        heads = heads + 1

        print("We've got " + str(heads) + " heads here."

    if random.randint(1,2) == 2:

        tails = tails + 1

        print("We've got " + str(tails) + " tails here.")

This is what I get as output:

This program flips a coin 10 times.

It then counts the number of heads and tails.

We've got 1 tails here.

We've got 1 heads here.

We've got 2 tails here.

We've got 2 heads here.

We've got 3 tails here.

We've got 3 heads here.

We've got 4 tails here.

We've got 5 tails here.

We've got 4 heads here.

We've got 6 tails here.

We've got 7 tails here.

We've got 5 heads here.

Can anyone help?

Thank you so much!

All the best,

 

Rafael
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Jim Mooney | 24 May 2013 04:39
Picon
Gravatar

making a random list, then naming it

I keep needing random lists of integers for trying things, so I wrote
a small prog I'll import as randlist. The user enters the length and
max val, but then I want the actual data name of the random list
created, to be   list_(length entered)_(max value entered)  as an easy
mnemonic.

I got as far as making the list, but my brain is fozzled on changing
the numbers entered into part of a data name. Feels like I have to go
outside the program to do it:

import random
random.seed

listparameters = raw_input('\nEnter two numbers, space separated, for
the length and largest value of a zero-starting random list, which can
be used as variable name:  randlist.list_len_max , where len is the
list length you entered, and max is the largest value\n')

listargs = listparameters.split()
rlist = []
length = int(listargs[0])
maxval = int(listargs[1])
for r in range(0,length):
    rlist.append(random.randint(0,maxval))

listlenmax = 'list' + '_' + str(length) + '_' + str(maxval)   # just
the name of a string, not the data name I want

#just printing the below as a test - they'll be removed when the import works

print
print rlist
print
print listlenmax

--

-- 
Jim Mooney
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Citizen Kant | 23 May 2013 21:57
Picon
Gravatar

Difference between types

I guess I'm understanding that, in Python, if something belongs to a type, must also be a value.

I guess I'm understanding that the reason why 9 is considered a value, is since it's a normal form, an element of the system that cannot be rewritten and reduced any further.

I also guess I'm understanding that the same goes somehow for the letter A for example, since it cannot be rewritten or reduced any further, so it's a value too.

type('A')
<type 'str'>

The question is, in order to understand: does this apostrophes thing has a more profound reason to be attached to the letters or it's just a conventional way to set a difference between letters and numbers? Do I must observe this apostrophes thing like the symbol of the type itself inside which one can put any character, setting it as type str?
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Andrew Triplett | 23 May 2013 19:31
Picon
Favicon

Presenting text in different ways by using quotes!

I am asked to present text in different ways by using quotes in strings. for example:

print("Program "Game Over" 2.0")

print("same", "message", "as before")

print("just",
        "a bit",
        "bigger")

print("Here", end=" ")
print("it is...")

print(

          """





    """
)

I can't however seem to input the text GAME OVER in giant text as it says in the book. Any help for this would be appreciated.

-Mitch Stevens

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Robert R | 23 May 2013 18:58
Picon
Favicon

installing old versions of python on Mac OS X

Hi,

I have Mac OS X 10.7.5 and would like to install < = python 2.3 on it.

I need it as one of the s/w I would like to try is based on an older version and does not seem to work with the new ones.
I am not sure if the s/w is wrong (doesn't seem like it) or is it really the python version that is messing it up.

I tried 2.4 (using mac ports) but thats not it so I want to go further back but I cannot do it with fink or mac ports.

Any other suggestions ? I tried to install using a tar file but that gave errors.
I am sorry for the dumb question. Thank you for the help
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Jim Mooney | 22 May 2013 21:47
Picon
Gravatar

keyboard interrupt

I made a simple ear frequency-tester, but I don't want it to go on
forever, so I tried stopping it when I  pressed a key, as below, but
that doesn't work. I did check out keyboard interrupts but they seem
unnecessarily complex just to stop a program. I'm not passing keys. Is
there something simple I'm missing?

import winsound

try:
    for freq in range(100,32000,100):
        winsound.Beep(freq, 1000)
except KeyboardInterrupt:
    pass

--

-- 
Jim Mooney
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Citizen Kant | 22 May 2013 20:14
Picon
Gravatar

Available characters

Does anybody know if there's a Python method that gives or stores the complete list of ascii characters or unicode characters? The list of every single character available would be perfect.

Thanks.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Jim Mooney | 22 May 2013 18:27
Picon
Gravatar

To error statement or not to error statement

>> "I find it amusing when novice programmers believe their main job is
>> preventing programs from crashing. ... More experienced programmers realize
>> that correct code is great, code that crashes could use improvement, but
>> incorrect code that doesn't crash is a horrible nightmare."

Then am I right to assume that rather than put in error statements I
barely understand at this point, the best thing would be to work the
hell out of the program in hope of seeing an error?  Does Python have
something that would do this automatically since I can't see running a
program a hundred times by hand?

Mainly, I'm just learning all this stuff for future reference. I
really doubt I'll need to use nose to find errors in twenty-line
programs. Print, assert, and staring at it for a long time should be
enough for now - and the Wing debugger now and then.

From the varied replies so far, it sounds to me that debugging is more
of an art than a science. So far the books I've looked at just mention
the basics but don't get into the philosophy of when and how.

Jim
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Stuart Tozer | 22 May 2013 09:49
Picon

trying to split or rpartition the contents of a list

Hi everyone. 

I'm stuck on a problem while developing a small tool for Maya. Basically, I have a folder with some filenames in it which have been returned by os.listdir(). These filenames look something like...


apple_d.jpg
apple_si.jpg
apple_sg.jpg
box_d.jpg
box_si.jpg
pumpkin_d.jpg


Right now, I'm using os.listdir to get the filenames, and then each filename becomes a selectable option in an option menu in maya. The code is...

objects = os.listdir(dir) for object in objects: cmds.menuItem(label = object, parent = "objectMenu")


My problem is that I don't want duplicates of the same object in the menu and I also want to truncate everything past and including the underscore. So for eg, if my filenames are the same as the example above, I want to change the list so I have this instead...

apple
box
pumpkin


I have tried splitting the filenames, but so far haven't had any luck.

for object in objects: sorted(set(object.split('_', 1)[0])) cmds.menuItem(label = object, parent = "objectMenu")


Also, I tried .rpartition but also no luck. I'm very new to python and this has me stumped. 
Any suggestions would be most welcome! ;)

Cheers,
S Tozer



_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Jim Mooney | 22 May 2013 08:20
Picon
Gravatar

Re: try..except - what about that ton of **Error statements?

> Keep the try block small. For example if it's for a call to
> open(filename, "r") the only possible errors (assuming correct syntax)
> are NameError for using an undefined variable and IOError for
> specifying a file which doesnt exist.

Thanks. Since I'm new at this the error lists I saw just had the bald
names, which didn't tell me much. But I found a concise and basic
explanation of each error at
http://python.about.com/od/pythonstandardlibrary/a/lib_exceptions.htm

Although it's still a bit circular at my level. I won't be sure what
errors to raise until I see enough errors, but that will come with
experience, I guess. Now I just have to figure how to print the list
out without the ads ;')

Jim
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Jim Mooney | 22 May 2013 07:46
Picon
Gravatar

try..except - what about that ton of **Error statements?

I'm looking at Try..Except

Try:
     <some statements>
Except SomethingError as err:
    <other statements>

The list of error statements is huge. How do I know which error
statement to put in place of SomethingError (or multiple errors for
that matter)? Or is it best to just leave SomethingError blank until I
know more? ( I have an asymptotic learning curve, so I go off on a lot
of confused tangents, but I find it makes things go a lot faster after
a certain point.)

--

-- 
Jim Mooney

"When I got to high school I realized my name would always present
problems." --Dick Hertz
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Gmane