My experience with Python

I’ve had a nice relationship with your way of teaching how to code. First I really thought that your book seems to have only ‘easy stuff’. But you know, I’m really a noob and I’m trying to learn hard. So I choosed to keep things as simple as possible, following your book chapter by chapter and thinking about it constantly. And, of course, print" " and print" “and print” ".
I finished the book but I really need to go back and try to grasp some more concepts specially about class and objects. Actually I always do that in order to understand what is going on.

So last sunday I was soo bored and was feeling myself in ‘code mode’. Inspired by Exercise 27, Memorizing Logic I started to draw a little game. So I opened my gedit and put some Python3 stuff there. I’m using a lot of functions, functions inside another functions, and I tried some crazy stuff that I had sure I was doing it wrong. But I was expecting just some fun.

But when the time came, I found myself stuck to a weird problem. I think I understood well the Logic behind it. But my if statements did not work as I expected.

So I started to print things out, checking if the type of things match, trying to receive specifs values, like booleans, ints, checking to see if the inputs was what I really want they to be. I thought there’s must be something wrong my logical thinking and I tried everthing that comes up to my mind.

The problem that I’ve using Python 3 in this game is probably something related to type of things. For example, If I want that a user gives me a boolean value as string, I need to convert it using bool(), and then, bool() is always return True if, for example, bool(x), where x must be True, so if my string is ‘False’, looks like the string itself is something True, and until now for me is like bool() is not working fine.

Then I saved my file and I created a new one. Basically, I thought: Let’s see If I can make my little game run with Python 2.

Surprise! Now a lot of ‘features’ is working just fine.

Now I will try to finish my game using Python 2. After that, I need to figure out what I need to do to make my game work in Python 3

P.S: English is not my first language. Sorry for any mistake.

2 Likes

Would you be willing to post/link your code? This way it would be easier to provide guidance.

-CaH

1 Like

Yeah, that is definitely weird. Just to make sure I tried:

bool("False")

And it returns true in Python 2 or 3. Without looking at your code, I think you may be doing two things you might want to change up:

  1. You might not be building your code incrementally, and instead writing a lot of code straight out of your head and then trying to make it run. I build my code in little pieces making sure each line I write works before doing more. So, if I wrote an if-statement that relied on bool(“False”) I’d find that before I wrote any more code.

  2. If you want to check for the truth or falseness of “True” or “False”, just use ==. If x == “True” does what you want better. The thing you may be doing is, now that you’ve learned some OOP, you’re trying to use it everywhere and doing type conversions all over. It’s best to use simpler things first and then add OOP when you need it.

But, if you can put your code into https://gist.github.com/ I’ll take a look.

1 Like

Thank you guys.

I’ve been editing my game since I opened this topic and yesterday I did a lot of changes in my script.

Your guess is correct, Zed. I’m coding like you described.

Still I had a few things I need to fix it, and I’m trying to put my best on it
300 lines of code so far, which means nothing, but it is the first time I build something on my own and almost from scratch.

I’ll share with you guys as soon as possible.

1 Like

I suggest you get into the automated testing then. It’s WAAAAYYYY easier to have a test_mystuff.py script that runs all your tests to make sure your code works. You can use nosetests or pytest to do this, but I’m finding that pytest is better and nosetest is deprecated so try out pytest.

What you want to do is get into a cycle of:

  1. Write some code.
  2. Write some tests.
  3. Review your code and tests.
  4. Run your tests.

That’s how I work, and it also doesn’t matter too much which order you do #1 and #2. You could write some tests if that makes sense, and then write the code to make the tests work. Or, write a little bit of code, and then write tests to test it.

2 Likes

Hi Zed,
Thanks for your suggestion - can you provide some example on how to use pytest to run automatic tests on the code?

Thanks

Start with the docs:

https://docs.pytest.org/en/latest/

There’s lots of examples, then start small. Do some toy examples to figure it out.