jonathan wallis | 1 Jul 2009 02:38
Picon

Need help with python game program

My problem is simple, is their a way to make a variable equal multiple numbers? Such as X = 5 through 10, and then the program makes x  equal something random 5 through 10, or something similar.

2009/6/30 <tutor-request <at> python.org>
Send Tutor mailing list submissions to
       tutor <at> python.org

To subscribe or unsubscribe via the World Wide Web, visit
       http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
       tutor-request <at> python.org

You can reach the person managing the list at
       tutor-owner <at> python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."


Today's Topics:

  1. PYTHONPATH-corrected (Bob Rea)
  2. Re: Needing Help (Bob Rea)
  3. Re: PYTHONPATH-corrected (Steve Willoughby)
  4. Re: Needing Help (Wayne)
  5. Re: Needing Help (Bob Rea)
  6. Re: Needing Help (Marc Tompkins)


----------------------------------------------------------------------

Message: 1
Date: Tue, 30 Jun 2009 14:33:27 -0400
From: Bob Rea <petard <at> petard.us>
To: tutor <at> python.org
Subject: [Tutor] PYTHONPATH-corrected
Message-ID: <200906301433.27455.petard <at> petard.us>
Content-Type: text/plain;  charset="iso-8859-1"

In working my way through the book on python, I am working
in directories for chapers. Now I am on modules, and some
are reused in later chapters. I have set up a directory for
the modules. How do I add it to my PYTHONPATH?
I can use sys.path.append but that only lasts for the
session.

This is on a suse linux 10 box

--
Bob Rea
mailto:petard <at> petard.us
http://www.petard.us
http://www.petard.us/blog
http://www.petard.us/gallery


------------------------------

Message: 2
Date: Tue, 30 Jun 2009 14:06:01 -0400
From: Bob Rea <bob <at> telaugos.com>
To: tutor <at> python.org
Subject: Re: [Tutor] Needing Help
Message-ID: <200906301406.02104.bob <at> telaugos.com>
Content-Type: text/plain;  charset="utf-8"

On Tue June 30 2009 1:17 pm, vishwajeet singh
<dextrous85 <at> gmail.com>wrote:
> You can put your script in pastebin
> http://python.pastebin.com/
> <http://python.pastebin.com/>I don't think any one will
> mind you pasting code in mail but pastebin makes it
> easier to read

I am making my way through _Making Use of Python_ by Rashi
Gupta. I am using Python 2.4.1

I have run into problems in a script, listed at
http://python.pastebin.com/m51bc3388

If I input my own name and dob, it works:
bob <at> gandalf:~/python/MakingUse/Chapter05> python code1.py
Enter your first name:  Bob
Enter your last name:  Rea
Enter your date of birth, mm-dd-yyyy:  03-05-1943
You can chose one of the following login names:
1.   BobR
2.   BobR53
3.   RBob43
4.   BRea66

If I use the data listed in the book, it fails:
bob <at> gandalf:~/python/MakingUse/Chapter05> python code1.py
Enter your first name:  Laura
Enter your last name:  Jones
Enter your date of birth, mm-dd-yyyy:  12-24-1980
You can chose one of the following login names:
1.   LauraJ
2.   LauraJ2412
3.   JLaura80
Traceback (most recent call last):
 File "code1.py", line 67, in ?
   fourth=fname[0]+lname+ age_func()
TypeError: cannot concatenate 'str' and 'NoneType' objects

What is going on here?

Bob Rea
petard <at> petard.us
--
Bob Rea
mailto:gapetard <at> stsams.org
http://www.petard.us
http://www.petard.us/blog
http://www.petard.us/gallery

Where is Bill Stringfellow
       now that we really need him?


------------------------------

Message: 3
Date: Tue, 30 Jun 2009 12:08:13 -0700
From: Steve Willoughby <steve <at> alchemy.com>
To: Bob Rea <petard <at> petard.us>
Cc: tutor <at> python.org
Subject: Re: [Tutor] PYTHONPATH-corrected
Message-ID: <20090630190813.GA65674 <at> dragon.alchemy.com>
Content-Type: text/plain; charset=us-ascii

On Tue, Jun 30, 2009 at 02:33:27PM -0400, Bob Rea wrote:
> In working my way through the book on python, I am working
> in directories for chapers. Now I am on modules, and some
> are reused in later chapters. I have set up a directory for
> the modules. How do I add it to my PYTHONPATH?
> I can use sys.path.append but that only lasts for the
> session.

You set the variable in your system's environment, which is
platform-dependent.  For Linux, you'd typically put a line
in your ~/.profile or ~/.cshrc or ~/.login or ~/.bashrc or
whatever your shell uses per your account set up.

So, for example, in a bash/sh shell, you'd say:

export PYTHONPATH="/path/to/my/modules"

or for csh:

setenv PYTHONPATH "/path/to/my/modules"

Then starting the next time you log in, that will be set
in your environment for you.

> This is on a suse linux 10 box
>
> --
> Bob Rea
> mailto:petard <at> petard.us
> http://www.petard.us
> http://www.petard.us/blog
> http://www.petard.us/gallery
> _______________________________________________
> Tutor maillist  -  Tutor <at> python.org
> http://mail.python.org/mailman/listinfo/tutor

--
Steve Willoughby    |  Using billion-dollar satellites
steve <at> alchemy.com   |  to hunt for Tupperware.


------------------------------

Message: 4
Date: Tue, 30 Jun 2009 14:27:34 -0500
From: Wayne <srilyk <at> gmail.com>
To: Bob Rea <bob <at> telaugos.com>
Cc: tutor <at> python.org
Subject: Re: [Tutor] Needing Help
Message-ID:
       <333efb450906301227u76274a83x53ef42fdae41fead <at> mail.gmail.com>
Content-Type: text/plain; charset="windows-1252"

Oops, forgot my reply-to-all

On Tue, Jun 30, 2009 at 2:20 PM, Wayne <srilyk <at> gmail.com> wrote:

> On Tue, Jun 30, 2009 at 1:06 PM, Bob Rea <bob <at> telaugos.com> wrote:
>
>> <snip>
>> If I input my own name and dob, it works:
>> bob <at> gandalf:~/python/MakingUse/Chapter05> python code1.py
>> Enter your first name:  Bob
>> Enter your last name:  Rea
>> Enter your date of birth, mm-dd-yyyy:  03-05-1943
>> You can chose one of the following login names:
>> 1.   BobR
>> 2.   BobR53
>> 3.   RBob43
>> 4.   BRea66
>>
>> If I use the data listed in the book, it fails:
>> bob <at> gandalf:~/python/MakingUse/Chapter05> python code1.py
>> Enter your first name:  Laura
>> Enter your last name:  Jones
>> Enter your date of birth, mm-dd-yyyy:  12-24-1980
>> You can chose one of the following login names:
>> 1.   LauraJ
>> 2.   LauraJ2412
>> 3.   JLaura80
>> Traceback (most recent call last):
>>   File "code1.py", line 67, in ?
>>    fourth=fname[0]+lname+ age_func()
>> TypeError: cannot concatenate 'str' and 'NoneType' objects
>>
>> What is going on here?
>>
>
> Well the first line after "Traceback" give you the line of code and the
> filename. Which happens to be code1.py line 67.
>
> TypeError is the type of error - specifically you tried to do something
> with incompatible types. In this case, adding together a string and
> NoneType.
>
> I'll do a little digging, but my guess is you have a problem with your
> age_func() not returning any value.
>
> HTH,
> Wayne
>

I was correct:def age_func():
age=cur_year-year-1
if month<cur_month or (month==cur_month and day<cur_day):
 age=age+1
return str(age)

What happens if month is > cur_month AND it's not my birthday?

You don't return a value. But if you simply unindent the "return" value then
it should fix it:

def age_func():
age=cur_year-year-1
 if month<cur_month or (month==cur_month and day<cur_day):
age=age+1
 return str(age)

Now regardless of whether age is incremented, it will return a string.
HTH,
Wayne
--
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090630/fe4ba054/attachment-0001.htm>

------------------------------

Message: 5
Date: Tue, 30 Jun 2009 15:27:17 -0400
From: Bob Rea <bob <at> telaugos.com>
To: Wayne <srilyk <at> gmail.com>
Cc: tutor <at> python.org
Subject: Re: [Tutor] Needing Help
Message-ID: <200906301527.20516.bob <at> telaugos.com>
Content-Type: text/plain;  charset="utf-8"

On Tue June 30 2009 3:20 pm, you wrote:
> On Tue, Jun 30, 2009 at 1:06 PM, Bob Rea
<bob <at> telaugos.com> wrote:
> > <snip>
> > If I input my own name and dob, it works:
> > bob <at> gandalf:~/python/MakingUse/Chapter05> python
> > code1.py Enter your first name:  Bob
> > Enter your last name:  Rea
> > Enter your date of birth, mm-dd-yyyy:  03-05-1943
> > You can chose one of the following login names:
> > 1.   BobR
> > 2.   BobR53
> > 3.   RBob43
> > 4.   BRea66
> >
> > If I use the data listed in the book, it fails:
> > bob <at> gandalf:~/python/MakingUse/Chapter05> python
> > code1.py Enter your first name:  Laura
> > Enter your last name:  Jones
> > Enter your date of birth, mm-dd-yyyy:  12-24-1980
> > You can chose one of the following login names:
> > 1.   LauraJ
> > 2.   LauraJ2412
> > 3.   JLaura80
> > Traceback (most recent call last):
> >   File "code1.py", line 67, in ?
> >    fourth=fname[0]+lname+ age_func()
> > TypeError: cannot concatenate 'str' and 'NoneType'
> > objects
> >
> > What is going on here?
>
> Well the first line after "Traceback" give you the line
> of code and the filename. Which happens to be code1.py
> line 67.
>
> TypeError is the type of error - specifically you tried
> to do something with incompatible types. In this case,
> adding together a string and NoneType.
>
> I'll do a little digging, but my guess is you have a
> problem with your age_func() not returning any value.
>
> HTH,
> Wayne

Why doe sit work with a dob in 1943 and not with one in 1980
then, that's what really bugs me
--
Bob Rea
mailto:gapetard <at> stsams.org
http://www.petard.us
http://www.petard.us/blog
http://www.petard.us/gallery

Where is Bill Stringfellow
       now that we really need him?


------------------------------

Message: 6
Date: Tue, 30 Jun 2009 12:45:00 -0700
From: Marc Tompkins <marc.tompkins <at> gmail.com>
To: Bob Rea <bob <at> telaugos.com>
Cc: tutor <at> python.org
Subject: Re: [Tutor] Needing Help
Message-ID:
       <40af687b0906301245n3ffcc950k458e4bf629530965 <at> mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On Tue, Jun 30, 2009 at 12:27 PM, Bob Rea <bob <at> telaugos.com> wrote:

> Why doe sit work with a dob in 1943 and not with one in 1980
> then, that's what really bugs me
>

Nothing to do with the year.  Your birth month is BEFORE June; her birth
month is after.
--
www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090630/0afb4e3c/attachment.htm>

------------------------------

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor


End of Tutor Digest, Vol 64, Issue 135
**************************************



--
You will be a winner today.  Pick a fight with a four-year-old.

You should emulate your heros, but don't carry it too far.  Especially
if they are dead.

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Mark Tolonen | 1 Jul 2009 02:56
Picon

Re: Need help with python game program


"jonathan wallis" <mindboggler12 <at> gmail.com> wrote in message 
news:57b8984c0906301738w1fb0e660m6bb2123399f27a0e <at> mail.gmail.com...
> My problem is simple, is their a way to make a variable equal multiple
> numbers? Such as X = 5 through 10, and then the program makes x  equal
> something random 5 through 10, or something similar.

Use the random module.  There are various types of random number generation 
there, such as:

    x = random.randint(5,10)

-Mark

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Luke Paireepinart | 1 Jul 2009 04:01
Picon
Gravatar

Re: Need help with python game program

Please don't reply to messages like this.  If you are starting a new thread, send a new e-mail to tutor <at> python.org.  DO NOT start a thread by replying to another message.  If you do, it will bork on people's machines who use threaded e-mail readers.  also, please remove long, irrelevant quotations from your e-mails unless you reference it.  for example, the quoted text in your e-mail is at least 10x longer than the actual content.

I believe mark already answered your question.
Thanks,
-Luke

On Tue, Jun 30, 2009 at 7:38 PM, jonathan wallis <mindboggler12 <at> gmail.com> wrote:
My problem is simple, is their a way to make a variable equal multiple numbers? Such as X = 5 through 10, and then the program makes x  equal something random 5 through 10, or something similar.

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Dave Angel | 1 Jul 2009 04:29
Picon

Re: Need help with python game program

jonathan wallis wrote:

> My problem is simple, is their a way to make a variable equal multiple
> numbers? Such as X = 5 through 10, and then the program makes x  equal
> something random 5 through 10, or something similar.
> <much snipping>
>   
For a short question, why did you quote an entire mailing list digest?

Answer to your question, certainly a variable can "equal" multiple 
numbers.  Several ways:
   1) one at a time.   X=5, then later  X=7.332
   2) a list of values  X = [5.3, 8.001, 17, 2]
   3) a dictionary    X = {"tom":3, "dick":42, "harry":12}
   4) a class.  Sky's the limit here

As for random, look up the random.uniform() and random.randint() functions.

Did you have a particular programming language in mind?  I've been 
assuming Python, but you might be thinking something else.  Have you any 
experience in any other language?

DaveA

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Daniel Sato | 1 Jul 2009 04:53
Picon

Python Programming exercise

I have been going through some Python Programming exercises while following the MIT OpenCourseWare Intro to CS syllabus and am having some trouble with the first "If" exercise listed on this page:

http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises

I have been able to make the module quit after entering a password three times, but can't get it to quit right away after the correct one is entered.  I know this is really basic, please forgive me.  I have no programming experience and have just started going through these tutorials.

My code is here:

http://python.pastebin.com/m6036b52e

--
Daniel Sato
http://www.danielsato.com

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Vincent Davis | 1 Jul 2009 05:39

calculating a sort key and operator.attrgetter()

I have a class with an attribute which is a list "rank_list" this is a list of instances f another class that has attributes "quality, is_observed"
if I want to sort the list by the attribute "quality" I can just use, self.rank_list.sort(key=operator.attrgetter('quality'))
But I want to sort like this.
self.rank_list.sort(key=(operator.attrgetter('quality') * operator.attrgetter('is_observed') * self.does_observe))
Will this work or is there a better way?

Thanks
Vincent Davis

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Alan Gauld | 1 Jul 2009 09:55

Re: PYTHONPATH-corrected


"Steve Willoughby" <steve <at> alchemy.com> wrote

>> in directories for chapers. Now I am on modules, and some
>> are reused in later chapters. I have set up a directory for
>> the modules. How do I add it to my PYTHONPATH?

> So, for example, in a bash/sh shell, you'd say:
>
> export PYTHONPATH="/path/to/my/modules"

And you can add multiple directories by separating them with colons:

export 
PYTHONPATH="/path/to/my/modules:/path/to/more/modules:/path/ytoyet/more"

HTH,

--

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Siim Märtmaa | 1 Jul 2009 11:06
Picon

Re: Python Programming exercise

2009/7/1 Daniel Sato <sato.photo <at> gmail.com>:
> I have been going through some Python Programming exercises while following
> the MIT OpenCourseWare Intro to CS syllabus and am having some trouble with
> the first "If" exercise listed on this page:
>
> http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises
>
> I have been able to make the module quit after entering a password three
> times, but can't get it to quit right away after the correct one is
> entered.  I know this is really basic, please forgive me.  I have no
> programming experience and have just started going through these tutorials.
>
> My code is here:
>
> http://python.pastebin.com/m6036b52e
>
> --
> Daniel Sato
> http://www.danielsato.com
>
> _______________________________________________
> Tutor maillist  -  Tutor <at> python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>

Hello daniel, some comments
password = "qwerty"
guess = "0"  # <- no need to initialise that variable or
             # if you would need, using just "" would be enough #instead of "0"

count = 0
while count != 3:  # <- that works here, but i usually use "< 3"
	guess = raw_input("Enter your password: ")
	
        guess = str(guess)  # no need to typecast here because
raw_input alredy returns a str
                            #when you don't know the type use
type(yourvar) in python

	if guess != password  # missing ":"
		print "Access Denied"
	count = count + 1  # <- has to be indented to the same level with the
above line
                                   # also you can use count += 1 instead
	else:
		print "Password Confirmed"
		# to exit you can use the exit() function
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Emile van Sebille | 1 Jul 2009 18:19

Re: Python Programming exercise

On 6/30/2009 7:53 PM Daniel Sato said...
> I have been able to make the module quit after entering a password three 
> times, but can't get it to quit right away after the correct one is 
> entered.  I know this is really basic, please forgive me.  I have no 
> programming experience and have just started going through these tutorials.
> 
> My code is here:
> 
> http://python.pastebin.com/m6036b52e

First, paste your code into a python command window.  You'll get errors, 
but clean them up.  Second, code blocks that are indented alike execute 
as part of the same block.  Take a look where you increment count.

HTH,

Emile

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Angus Rodgers | 1 Jul 2009 17:44
Picon

When are strings interned?

Hello, world!

This is my first post to the Tutor list (although I've already
posted to comp.lang.python a couple of times).

I'm currently reading Chapter 4 of Wesley Chun's book, "Core 
Python Programming" (2nd ed.).

I find this, in Python 2.5.4, on my Win98SE system (using IDLE):

>>> n = "colourless"
>>> o = "colourless"
>>> n == o
True
>>> n is o
True
>>> p = "green ideas"
>>> q = "green ideas"
>>> p == q
True
>>> p is q
False

Why the difference?
--

-- 
Angus Rodgers
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor


Gmane