Chris Kavanagh | 1 Nov 2011 02:02
Picon
Favicon

Help

This code is from the book 'Invent your own computer games with Python' 
2nd edition. Great book so far. . .

My question comes on the 2nd game (dragon game). Just a simple little 
program that lets you choose to enter 'cave one' or 'cave two' by using 
the random module, radnom.randint. If you choose the 'wrong' cave, you 
get a message saying 'sorry you've been eaten'. Choose the correct cave 
and you get a message that 'you get the treasure. Pretty simple. .

However, I'm confused on Line 30 {if chosenCave== str(friendlyCave)}. 
Here's the description of this line the author gives:

"Here we check if the integer of the cave we chose ('1' or '2') is equal 
to the cave
randomly selected to have the friendly dragon"

My question is, we saved the integer of the cave we chose in the 
variable {cave}in line 15, not {chosenCave}. So, what the heck am I 
missing?? How is the {chosenCave} variable now holding the choice I made 
in the {cave} variable??

Keep in mind I'm a beginner, but geez, I should understand this easily! 
I'm very frustrated right now, I should easily know this. But, I don't, 
lol. Thanks in advance for any help!! Code below:

dragon.py
This code can be downloaded from http://inventwithpython.com/dragon.py
If you get errors after typing this code in, compare it to the book's 
code with the online
diff tool at http://inventwithpython.com/diff or email the author at
(Continue reading)

Steven D'Aprano | 1 Nov 2011 02:33

Re: Simple Question On A Method (in subclass)

Marc Tompkins wrote:
> It can be a little hard to wrap your head around how Python handles
> variables/objects; in other languages you create a variable and assign a
> value to it, while in Python you create an object and assign a name to it -
> the name can change while the object remains unchanged.  Here's a very
> simplified demo of what Dave is talking about:
[...]
> It's extremely logical, but almost entirely backward from the way most other
> languages do things.  Possibly it's because Guido is Dutch.

Fortunately, that is untrue.

I'm not sure where the myth that "Python is different from other 
languages" comes from. I suppose it is true only so far as *every* 
language is different from any other language (otherwise they would be 
the same language!). But Python is not so different from other common 
languages.

In particular, I don't know of any language where assignment means 
aliasing. Take the example Marc gave earlier:

t = 'this'
s = 'that'
group = [t, s]
print group  # => "['this', 'that']
s = 'the other'
print group  # => "['this', 'that']

I don't know of any language where the second item of group would now 
equal 'the other'. Pascal certainly isn't one:
(Continue reading)

Steven D'Aprano | 1 Nov 2011 02:55

Re: Help

Chris Kavanagh wrote:

> However, I'm confused on Line 30 {if chosenCave== str(friendlyCave)}. 
> Here's the description of this line the author gives:
> 
> "Here we check if the integer of the cave we chose ('1' or '2') is equal 
> to the cave
> randomly selected to have the friendly dragon"
> 
> My question is, we saved the integer of the cave we chose in the 
> variable {cave}in line 15, not {chosenCave}. So, what the heck am I 
> missing?? How is the {chosenCave} variable now holding the choice I made 
> in the {cave} variable??

Unfortunately, the indentation of your code is completely mangled for 
me, which makes it difficult to be sure which parts of the code are 
inside functions and which are not. So I will be forced to guess.

I can tell that line 15 is inside the function chooseCave(), and so the 
variable "cave" is a local variable. Local variables only exist inside 
the function that creates them. In this case, the chooseCave() function 
returns the value of "cave" to the caller.

That is, at the end of your code, you call the functions you earlier 
created:

caveNumber = chooseCave()
checkCave(caveNumber)

These two lines cause the following to happen:
(Continue reading)

Joel Montes de Oca | 1 Nov 2011 03:45
Picon
Gravatar

Re: Paper Rock Scissors game - User's choice not returned properly

On 10/31/2011 07:10 PM, Steven D'Aprano wrote:
> Alan Gauld wrote:
>> On 31/10/11 20:22, Peter Otten wrote:
>>> Alan Gauld wrote:
>>>
>>>> if choice.lower() not in ('prs'): # NB use a single string
>>>
>>> That's not a good idea. If a user accidentally enters PR (for 
>>> example) your
>>> version will mistake that for a valid choice.
>>
>> Good point, although  you could test the first character only...
>>
>> if choice[0].lower() not in ('prs'): # NB use a single string
>
> Why would you do that? If the user is supposed to enter a single 
> letter, why would you accept (for example) "screw you hippy, I hate 
> this game!" as the valid response "s"?
>
>
>
I agree, I am not sure that I see the benefit of using a single string. 
If anything, it makes it a bit harder to read by someone other than the 
person who wrote it.

    if choice.lower() in ('p', 'r','s'):

I think the code above is a lot clearer.

--

-- 
(Continue reading)

Chris Kavanagh | 1 Nov 2011 04:07
Picon
Favicon

Re: Help

Yes Steven, that solved my question(s). It also cleared up what was to 
be my next question! Thanks so much. You might not realize how grateful 
I am to be able to have you & others on the list answer my questions. 
Just trust me when I say, I am grateful. And I apologize for the code 
being mangled on your end. I'm not sure why this happened, but you were 
correct in your assumptions. . .

One thing I'm curious about. If the code is read by the Interpreter or 
Compiler from the top down, why in this case, does it not get confused 
(cause an error) when it hits line 30, if it doesn't yet know that 
{caveNumber=chooseCave()} and checkCave(caveNumber)}?? It seems to me in 
my limited experience, those last two lines should've come before line 
30. It seems as though it would've made more sense to somehow put them 
before. In other languages, ala C++, don't global variables have to be 
declared at the 'top' of the code??

On 10/31/2011 9:55 PM, Steven D'Aprano wrote:
> Chris Kavanagh wrote:
>
>> However, I'm confused on Line 30 {if chosenCave== str(friendlyCave)}.
>> Here's the description of this line the author gives:
>>
>> "Here we check if the integer of the cave we chose ('1' or '2') is
>> equal to the cave
>> randomly selected to have the friendly dragon"
>>
>> My question is, we saved the integer of the cave we chose in the
>> variable {cave}in line 15, not {chosenCave}. So, what the heck am I
>> missing?? How is the {chosenCave} variable now holding the choice I
>> made in the {cave} variable??
(Continue reading)

Chris Kavanagh | 1 Nov 2011 05:10
Picon
Favicon

Re: Help

I'm going to thank Steven once again, and answer my own question in the 
2nd paragraph directly below (Steven hasn't had a chance to respond yet).

I just learned that the Function definitions are not read by the 
interpreter UNTIL they are called. I was reading them and assuming they 
were executed from the top down. Obviously I was getting somewhat 
confused because of this. This 'dragon' program makes much more sense to 
me now that I understand that. . .Thanks again Steven & everyone!!!
Happy Halloween!!!

On 10/31/2011 11:07 PM, Chris Kavanagh wrote:
> Yes Steven, that solved my question(s). It also cleared up what was to
> be my next question! Thanks so much. You might not realize how grateful
> I am to be able to have you & others on the list answer my questions.
> Just trust me when I say, I am grateful. And I apologize for the code
> being mangled on your end. I'm not sure why this happened, but you were
> correct in your assumptions. . .
>
> One thing I'm curious about. If the code is read by the Interpreter or
> Compiler from the top down, why in this case, does it not get confused
> (cause an error) when it hits line 30, if it doesn't yet know that
> {caveNumber=chooseCave()} and checkCave(caveNumber)}?? It seems to me in
> my limited experience, those last two lines should've come before line
> 30. It seems as though it would've made more sense to somehow put them
> before. In other languages, ala C++, don't global variables have to be
> declared at the 'top' of the code??
>
> On 10/31/2011 9:55 PM, Steven D'Aprano wrote:
>> Chris Kavanagh wrote:
>>
(Continue reading)

Rinu Boney | 1 Nov 2011 06:31
Picon

Pickle Class Instances

This Is My Program :
class book:
    def __init__(self,bno=100,bname='Book'):
        self.book_number=bno
        self.book_name=bname
    def enter_book(self):
        self.book_number=input("Enter Book No : ")
        self.book_name=input("Enter Book Name : ")
    def display_book(self):
        print('Book No : ',self.book_number)
        print('Book Name : ',self.book_name)

b1 = book()
b=book()

def add_book(b):
    fo=open('books.dat','wb')
    pickle.dump(b,fo)
    fo.close()

def display_books():
    global b1
    fi=open('books.dat','rb')
    b1=pickle.load(fi)
    b1.display_book()
    fi.close()

The Display After Unpickling Shows only the last Class Instance.
How To Display All The Data In The File ?

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Rinu Boney | 1 Nov 2011 06:38
Picon

Pickling Class Instances

I have a class 'book' and function for input and display of the class members

then there are 2 functions for pickling and unpickling class instances from and to a file.
after unpickling , then displaying it shows only the last class instance stored in the file!
what can be the possible problems? 
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Andreas Perstinger | 1 Nov 2011 07:53
Picon

Re: Pickle Class Instances

On 2011-11-01 06:31, Rinu Boney wrote:
> def add_book(b):
>      fo=open('books.dat','wb')
>      pickle.dump(b,fo)
>      fo.close()
>
> The Display After Unpickling Shows only the last Class Instance.
> How To Display All The Data In The File ?

You haven't shown us the complete program (how to you call "add_book" 
and "display_books"?) but I guess that you call "add_book" with one 
instance (if not, you can ignore the rest). In that case there is no 
more data in the file.

In your "add_book" function you open the file "books.at" and just save 
one instance. The next time you call the function with another instance 
you overwrite the former, because opening the same file with the 
parameter "w" (or in your case "wb") deletes any already existing content.

What you probably want is to put all the instances into a list and save 
this list. When you later read the file, you'll get the list back and 
you can iterate over it for displaying.

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

Peter Otten | 1 Nov 2011 10:46
Picon

Re: Paper Rock Scissors game - User's choice not returned properly

Alan Gauld wrote:

> On 31/10/11 20:22, Peter Otten wrote:
>> Alan Gauld wrote:
>>
>>> if choice.lower() not in ('prs'): # NB use a single string
>>
>> That's not a good idea. If a user accidentally enters PR (for example)
>> your version will mistake that for a valid choice.
> 
> Good point, although  you could test the first character only...
> 
> if choice[0].lower() not in ('prs'): # NB use a single string

What Steven says, plus you may run into an IndexError if choice is the empty 
string. If you absolutely want to test against a single string you have to 
check the length first.

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


Gmane