Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: Let's Python! (2.7.2) Slowly headed towards Amazons and Barbarians.  (Read 2785 times)

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile

So, decided to get back to programming so I have something other than my internship on my hands to deal with, and since I'd prefer a career in some sort of applied computer science. Still, I never found following tutorials to be very intuitive to learning something. So I had a little thought...

Why not work on whatever it is I feel I need more practice in, and post what I did over here? I mean, when has depending on the charity of strangers online ever been a bad thing? I can screw up, people can point what I did wrong, and I can learn and move on.

Slight heads up though, I'm not exactly known for sticking to anything, so this might die out tomorrow, or the day after, or something, but at least I'm doing something.

Anyway, here's the first bit of python scripting I've done:
Spoiler (click to show/hide)

Anyway, that's what I have for now, since I'm still learning the ropes of python. I'd still want to tackle input and output for a while, but I'll also be going over control statements quickly, then move onto classes and such.

Got a comment? Want to ask a question? Go ahead post. I might not be able to answer it, but I'm sure someone else here can.
« Last Edit: September 19, 2011, 01:35:36 pm by chaoticag »
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #1 on: September 18, 2011, 07:58:07 am »

Try making a class called 'Player' that takes an argument for their name.

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #2 on: September 18, 2011, 08:01:52 am »

Yeah, that's pretty easy and OOP is always good to know.
Logged

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #3 on: September 18, 2011, 08:13:35 am »

Sure, I'll get to work right away.
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #4 on: September 18, 2011, 08:16:54 am »

As a bonus, define the speech as a method.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #5 on: September 18, 2011, 08:19:13 am »

Make it return a string, rather than print. It is nice to have model view separation.

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #6 on: September 18, 2011, 08:32:19 am »

Aaaand hurray! Run into some uncompilable code. Looks like the problem seems to be with me defining a function within the class Player. Here's the code so far, I'll look at the online help, while I figure out what went wrong.

Spoiler (click to show/hide)

It's kinda comment-less this time, so bear with me...
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #7 on: September 18, 2011, 08:33:33 am »

Define Player above main, not in it.

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #8 on: September 18, 2011, 08:36:37 am »

Aaand ninja.

Also, you never really use the methods.
« Last Edit: September 18, 2011, 08:39:11 am by Darvi »
Logged

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #9 on: September 18, 2011, 08:40:22 am »

...

Well, it's been over a year since I dealt with programming. Still, that was pretty stupid (and educational). Still, it complains when I execute it.

Here is the updated code:
Spoiler (click to show/hide)

I get this error here when running the script:
Code: [Select]
  File "Player.py", line 26
    def namePlayer()
                   ^
SyntaxError: invalid syntax
Press any key to continue . . .

Oh, and it's been long enough that I've errr, forgotten what methods are.
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #10 on: September 18, 2011, 08:50:53 am »

Meh, well, I never even knew what methods were until a couple years ago.

Anyways, I can't get python to run because of missing dll's, so I can't check right now.
Logged

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #11 on: September 18, 2011, 09:00:08 am »

I found the problem. Actually, let's play a game. See if you can spot the difference between there two statements

Code: [Select]
def namePlayer()
and

Code: [Select]
def namePlayer():
And just a hint, but I put in the first one, so guess which one I was supposed to use?

Anyway, now that that bug is sorted... well I think I'm running into another bug. (Well, other than the typo in main that I corrected asking for the wrong function). Looks like python automatically makes everything private within it's classes, including functions, so I can't call them from main.

Edit: hold on, I forgot something. The class is defined, but not actually used. Derp.
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #12 on: September 18, 2011, 09:05:13 am »

Yeah, python as such has no private instances.
Logged

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #13 on: September 18, 2011, 09:16:22 am »

Okay forgot something (I sense a recurring theme here) but it looks like, the thing about classes, is you want them to be able to initialize them. In order to do that, you need a function that initializes said class. Once you have that, you can make an instance of said class... I think. Does this sound right?
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #14 on: September 18, 2011, 09:34:25 am »

Uh, right. Lemme think.

There's the __init__ method (two underscores each), that always executes once you create an object based on said class.

So in your case if would look something like that:
Code: [Select]
def __init__(self):
          self.name='somebody'
And then in the main loop you initialize the class:
Code: [Select]
Me = Player()Then you can assign it a name
Code: [Select]
Me.name = raw_input()
And then use it with
Code: [Select]
Me.displayName()
Of course I haven't actually done anything of the sort in a while and I can't check if it works right now, but that's about the idea.
Logged
Pages: [1] 2