Devember - Relearning to program using Python

Day 7

Tested time and calendar modules today, and created some of my own. The output on time module was odd, and I didn’t manage to get it to look sane. On Tutorials point there is a example of how to clean the time output, but that just looked really complicated way of doing it. Or then I just assume it must be easier.

Anyway, tomorrow I try to import my own modules from different file. Let’s see how that goes.

2 Likes

Day 8

Looks like a can of Monster really is the fuel of the champions! I managed to make and import couple simple modules from other file, but that little more complex Animal class in moduletest.py does not work. Or then I’m doing someting wrong when trying to invoke it in my test1.py script.

image

Investigation shall continue tomorrow. My guess is this is yet again some stupid little mistake or typo somewhere…

1 Like

Is it the tabbing? It seems to step out. I realise I have no idea how python lays out functions, it’s all Greek to me.
Suffice to say it cannot find the toString function, or one that matches what you called.

1 Like

Note that Python has the __str__ method for returning strings (of your choosing) for objects, so instead of print(cat.toString()) you can just write print(cat).

Ah, yes, it’s the indentation. All of your methods for Animal are defined inside __init__.

Hope I’m not giving too much away. Just trying to be helpful.

2 Likes

Day 9

Thanks for the help, I really appreciate it! :slight_smile: I looked into your suggestions but I didn’t manage to figure out what’s wrong.

I tried to tinker with indentation but I just got more errors. :smiley: I try again tomorrow if that solves the problem. The Animal class is taken (with small modifications) from the Python youtube tutorial by Derek Banas. He has the code available on his website so I’ll look if there’s something I missed from the video.

I did create simple Person class in moduletest.py and was able to use that in my main test1.py script without any problems. That class does not have any methods, so like @khaudio said my methods in Animal class are broken.

So at least I have something to look into tomorrow. :slight_smile:

3 Likes

Ok, i’ll give you one example, and you can sort the rest; it’s your devember, after all. You need to indent each method under the class, not nested under other methods.

Wrong:

class Animal:
    def __init__(self):
        print('Initialized')

        def jump(self):
            print('Jumping')

Right:

class Animal:
    def __init__(self):
        print('Initialized')

    def jump(self):
        print('Jumping')

After getting familiar with python’s flavor of classes, you can cut corners using attrs

1 Like

Day 10

Thanks @khaudio, now the script works. Yesterday I tried to move indentation in methods to wrong direction. Don’t ask me why I didn’t try the other way around, I’m asking that from myself too… :man_facepalming:

I’ll read up some more about classes, methods and object oriented programming in general. I feel there is a lot of basic stuff that I need to make clear to myself before I can move forward.

Also as a note to myself, some things I still need to take a look are:

  • How to fetch data form files (File I/O)
  • How the user input works.

I’m sure there’s much more than that, but I feel those are the two things I need to learn before I can start making that simple game.

3 Likes

Day 11

Well today was a busy day. I didn’t even look at my code today, and managed to read some python stuff maybe 10 minutes. If I don’t fall asleep instantly I maybe read some documentation as a bedtime story for my self. :grin:

Hopefully tomorrow is a bit more productive day for me. Good night for now :sleeping:

2 Likes

Day 12-13

Yesterday I got nothing done, but today was bit better day. I tested how reading and writing from files work and created two new .txt files to repo. test.txt was used for writing stuff to a file from python script and then reading it. readtest.txt was other test file for reading stuff. That piece of text is from Python Wikipedia page. :grin:

Not the most interesting update. Tomorrow I’ll take a look at how python reads input from user. I managed to read documentation a little bit and it looks like raw_input is deprecated from Python 3, and that could explain why I had problems with I/O in my earlier tests.

3 Likes

I do not like the Python REPL. Haskell and Lisp have good ones.

If you are on a Linux based distribution you can always chmod +x the thing and ./app or what have you.

app

#!/usr/bin/env python3

for i in range(100000000000000000000000000000000):
    print(i * 1000)

Then you can just ./app or add $WORK_DIR to your $PATH and then run app

2 Likes

I use it mainly to test out some random one line scripts to see how they work. Anything more complex and I rather write the code to separate file and use Pycharms integrated debugger to see what happens.

Are you talking about the original Lisp? Like the one from 50’s? If I ever get anywhere with programming I’m definitely going to try some old programming languages just for fun. :smiley:

Day 14

Read a bunch of documentation and tutorials about functions in Python today, and now they start to make more sense to me. This far I’ve only used functions with the mindset of “well that’s how it is…”, without further thinking how methods and functions actually work.

Tomorrow I try to look into user I/O and hopefully I can then start to make that game. I have absolutely no idea where to start, but maybe I’ll just start to code the basic stuff and built from there bit by bit. That might force me to rewrite stuff but hey, it’s a learning experience! :smiley:

2 Likes

That’s good, I think that’s what it’s generally designed to do. I learned Haskell that way. Write a function in a file, save, import in the REPL, play with function, rinse repeat.

Lol yeah I think they call it Modern Lisp or something now. The book that reads like a comic book is really good. Hits the ground running and gets you started.

Also this:


Regarding your IDE of choice, I love JetBrains, but don’t be afraid to test some stuff out on CLI too (like creating your own env or installing packages with pip). It helps to ingrain the knowledge rather than relying on the IDE to do it for you.

That said, I use RubyMine and IntelliJ every day :wink:

1 Like

I used to run my scripts on CLI at the begining, like: python3 foo.py, and wrote the code using VIM. Main reason why I use IDE for now is to be able to concentrate to programing, and let the IDE do all the nasty stuff behind the scenes. :smile:

I will give VIM another try later, but to keep things simple (and keep myself sane :smiley: ) I will use IDEs for now.

Day 15

Yet again I spent most of my time reading documentation and trying out some random small things. I also created new game.py and gmodules.py for the simple CLI game. Then I hit the wall and realized I have no idea where to start, meaning I haven’t planed at all how the game would progress and so on. So basically I was about to build a house, but I didn’t even know what kind of house I want to build. :smile:

So for tomorrow I will plan out the game, try to think out the basic functionality, etc.

3 Likes

This is very smart, and I believe the way you’re supposed to do it.

Learn clang or gcc and create objects and link files. Then start using Visual Studio or CLion or Eclipse to write your code.

Good on you for being on top of your game.

1 Like

Ooh making a text adventure game?

1 Like

Well sort of. I’m not actually sure what this will be. Actually, let me translate the main idea for the game I worked on the programming course I took while at the Uni:

In the game, you are able to create different player characters. Characters can have a profession, sell goods and services and engage in battle. Character starts from level 1 and after acquiring enough experience character will level up. To earn money, you must win a battle or sell goods. You may acquire a profession with money. Each character can have two professions. When you practice your professions, your skill level will increase and thus the quality the goods or services you provide will also improve. Once your character reached new level, the health and strength will also increase.

So that was the game idea in a nutshell. At first I was planning to follow that idea, but maybe I try to add my own twist to it.

It would also be fun to try to make pure text adventure game, but I’m not sure if I can keep up with multiple endings and so on. :smiley:

Screw it, I’ll stop procrastinating now. Less talking and more coding!

2 Likes

Day 16

Started coding the game, but didin’t get far and got buried under the not-so-helpful documentation. Tried to find out how to avoid extra space if I want to print variables: print('Hello', name, ", how are you doing?")

If I do it that way the output will be Hello Mike , how are you doing?, and I would like to avoid that extra space after variable. Tried the .format() but didn’t get it to work. I probably didin’t even use it right. Have to do some more research.

Other thing I briefly tired was the random number generator, but didn’t get that to work either. I only used maybe 10 minutes for trying to find solution for that problem so, meh, now I have something to do for tomorrow. :smiley:

Stackoverflow is your friend!

1 Like

Day 17

Thanks! I tired to figure out that problem on my lunch break at work today, but it didn’t work. I have a feeling the problem is my string is a variable, and that variable is the one that by default adds white space after it. I have to do more research tomorrow.

I did get the random generator to work, so some progress at least. Well, three lines of code… :expressionless:

2 Likes