Jump to content

Photo

Learning Python


  • Please log in to reply
26 replies to this topic

#16 Anthus

Anthus

    the wild-eyed boy from freecloud

  • Forum Moderators
  • Gender:Male
  • Location:Please turn on high-accuracy mode

Posted 20 July 2018 - 07:25 PM

I bookmarked all the links you guys posted, and the code design stuff sounds pretty useful, for later on at least. :)

 

This is the book series I've been using. I read chapter 1 in the one I'm linking directly, but I am on chapter 4 of "Invent your own games with Python". These seem fairly short, and I will probably be able to finish this book in a month or so. I plan on doing the other ones too, and finding any and all sources of information that I can.

 

Here is the first thing I wrote (after hello world, that is).

# This is a simple guess the number game.
import random

guessesTaken = 0

print('#############################')
print('# Hello! What is your name? #')
print('#############################')
myName = input()

number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20')

for guessesTaken in range(6):
    print('Take a guess!') # Four spaces here
    guess = input()
    guess = int(guess)

    if guess < number:
        print('Your guess is too low :(') # eight spaces here

    if guess > number:
        print('Your guess is too high :/')

    if guess == number:
        break

if guess == number:
    guessesTaken = str(guessesTaken + 1)
    print('Good job, ' + myName + '! You guessed the number in ' + guessesTaken + ' guesses!')
    print()
    print('Thanks for playing!')

if guess != number:
    number = str(number)
    print('Nope. The number I was thinking of was ' + number +'.')
    print()
    print('Thanks for playing!')

The next chapter talks about defining your own functions, and making it so the program knows if you gave a valid answer. For example, this 'game' only accepts numbers as an answer. When you type in the number 5, it is a string, not an integer, so the int() function makes that input into an integer, but only if it is an actual number. But you guys know this stuff anyway :P. Currently, if you enter anything that isn't a whole number, you'll get an error. But I can make a function that tells you that is invalid, and asks for a number.

 

I also made this before learning about line breaks within strings, so I used three print() functions there at the top. Small steps!


  • peteandwally likes this

#17 peteandwally

peteandwally

    chiubicabachiukicaca

  • Members
  • Gender:Unspecified

Posted 20 July 2018 - 07:59 PM

I bookmarked all the links you guys posted, and the code design stuff sounds pretty useful, for later on at least. :)

 

This is the book series I've been using. I read chapter 1 in the one I'm linking directly, but I am on chapter 4 of "Invent your own games with Python". These seem fairly short, and I will probably be able to finish this book in a month or so. I plan on doing the other ones too, and finding any and all sources of information that I can.

 

Here is the first thing I wrote (after hello world, that is).

# This is a simple guess the number game.
import random

guessesTaken = 0

print('#############################')
print('# Hello! What is your name? #')
print('#############################')
myName = input()

number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20')

for guessesTaken in range(6):
    print('Take a guess!') # Four spaces here
    guess = input()
    guess = int(guess)

    if guess < number:
        print('Your guess is too low :(') # eight spaces here

    if guess > number:
        print('Your guess is too high :/')

    if guess == number:
        break

if guess == number:
    guessesTaken = str(guessesTaken + 1)
    print('Good job, ' + myName + '! You guessed the number in ' + guessesTaken + ' guesses!')
    print()
    print('Thanks for playing!')

if guess != number:
    number = str(number)
    print('Nope. The number I was thinking of was ' + number +'.')
    print()
    print('Thanks for playing!')

The next chapter talks about defining your own functions, and making it so the program knows if you gave a valid answer. For example, this 'game' only accepts numbers as an answer. When you type in the number 5, it is a string, not an integer, so the int() function makes that input into an integer, but only if it is an actual number. But you guys know this stuff anyway :P. Currently, if you enter anything that isn't a whole number, you'll get an error. But I can make a function that tells you that is invalid, and asks for a number.

 

I also made this before learning about line breaks within strings, so I used three print() functions there at the top. Small steps!

 

Also for 'if' statements, you can add on 'elif' and 'else' conditions. Tht way if the first thing isn't 'true' then the statement moves on until it check all 'elif' conditions (else if's), and the catchall if nothing else is true is the 'else'.

There is another kind of conditional statement called a 'switch' that checks some input against cases that would be useful for checking whether guess==number

Other things to consider are playing a sound for right and wrong answers or writing a txt file of the console output. Baby steps!

I will also add that the different types of variables like string and int are related to how many bits are used to store the value on your memory. Even string values and char values are stored as ones and zeroes, but the numerical values in interpreted for our ability to read and understand it. To see what I mean, open a new txt file and write your name and save it. The file size is some number of bytes. Then, type in 010101 or something and check that file size after saving. Compression algorithms take advantage of patterns in those bits to make files smaller... but I am digressing... baby steps!


  • Anthus likes this

#18 Saffith

Saffith

    IPv7 user

  • ZC Developers
  • Gender:Male

Posted 20 July 2018 - 09:48 PM

I also made this before learning about line breaks within strings, so I used three print() functions there at the top. Small steps!

Eh, I'd leave that as it is, anyway. It's clearer with three calls.

#19 Jamian

Jamian

    ZC enthusiast

  • Members
  • Gender:Unspecified

Posted 21 July 2018 - 08:37 AM

As a slighty harder exercise, you could have the computer guess the number the player is thinking of (the player answers "higher" or "lower" on the computer's guesses, and we're assuming he doesn't cheat). And most importantly, the computer finds the correct number in the minimum amount of guesses. For example, it should be able to find a number between 1 and 1 million in 20 guesses. I know it sounds like it's a lot, but the computer cannot fail if it plays well.


Edited by Jamian, 21 July 2018 - 08:46 AM.


#20 Saffith

Saffith

    IPv7 user

  • ZC Developers
  • Gender:Male

Posted 21 July 2018 - 10:18 AM

Oh, I have a game to recommend, too: else Heart.Break() (Steam, GOG, Humble)

It's a game in which you reprogram things using a language fairly similar to Python or Ruby. You don't have to do anything too difficult to finish the game, but there's quite a lot you can do. There are arcade cabinets with entire games that you can modify or replace. You can hack doors to lead to testing areas once you figure out how to get a list of maps (though I think the most interesting ones were removed in an update...).
  • Anthus likes this

#21 Anthus

Anthus

    the wild-eyed boy from freecloud

  • Forum Moderators
  • Gender:Male
  • Location:Please turn on high-accuracy mode

Posted 21 July 2018 - 12:08 PM

Whoa, that game looks like a fever dream. I added it to my wishlist. I will probably pick it up at some point, especially if it goes on sale.

 

I did make a version where the number was between one and one million, and you get 100 guesses. I guessed it in 16 tries. It's not as crazy as you'd think. Just enter 500,000 and go from there, and narrow it down.



#22 Jamian

Jamian

    ZC enthusiast

  • Members
  • Gender:Unspecified

Posted 22 July 2018 - 11:30 AM

That's right. The (slightly) harder part for this exercise though, is to have the computer make the guesses. You have to remember previous guesses and implement something to make up for the loss of precision you get when you have to divide an odd number by 2.


  • Anthus and peteandwally like this

#23 peteandwally

peteandwally

    chiubicabachiukicaca

  • Members
  • Gender:Unspecified

Posted 24 July 2018 - 09:53 AM

That's right. The (slightly) harder part for this exercise though, is to have the computer make the guesses. You have to remember previous guesses and implement something to make up for the loss of precision you get when you have to divide an odd number by 2.

Now you're getting into gradient descent algorithms and the basis for fast marching cubes and all that junk. Nice way to ease into those concepts.


  • Anthus likes this

#24 Saffith

Saffith

    IPv7 user

  • ZC Developers
  • Gender:Male

Posted 26 July 2018 - 12:09 PM

You might also be interested in The Daily WTF. It's a good place to see a lot of bad code (generally with explanations of why it's bad) and read stories about the sorts of places you definitely don't want to work. You may not fully understand much of the code at this point, but you can still learn some stuff not to do.
  • Anthus and ZoriaRPG like this

#25 Saffith

Saffith

    IPv7 user

  • ZC Developers
  • Gender:Male

Posted 07 August 2018 - 11:14 AM

Oh, here's another thing I should have thought of sooner: MIT OpenCourseWare. MIT makes materials from a lot of their courses available for free. Some of them even have videos of all the lectures.
 
Here's one that teaches basic programming concepts using Python. Probably covers a lot of stuff you've already learned, but it's more thorough than any web tutorial would be. Introduction to Algorithms looks like a good follow-up, dealing with a lot of basic structures, algorithms, and analysis.
  • Anthus and peteandwally like this

#26 Anthus

Anthus

    the wild-eyed boy from freecloud

  • Forum Moderators
  • Gender:Male
  • Location:Please turn on high-accuracy mode

Posted 15 August 2018 - 09:31 PM

Thanks again for all the resources, guys. :)

 

I took a small break, cause I got a bit discouraged, but I'm back at it. I learned it is much better for me to do this in small blocks, rather than by trying to cram for hours on end. I got a bit ahead of myself, and tried to do my own thing, but obviously, I wasn't able to quite yet. I'm going to try to stick to one course at a time. I don't want to over-expose myself to information, get overwhelmed, and burned out. I've also been thinking a lot about programming as more of a way of solving a series of problems. I think that "knowing code" and "knowing how to code well" are two different things. I am looking forward to this also helping with general problem solving skills, as this seems a good way to build those.

 

One thing I did do though, was look over the Ocarina of Time Randomizer's source code, as it is made with Python. I was impressed with how much of the stuff I did understand, or at least, could identify. Obviously, I'm a ways off from doing anything like that from scratch, but it was nice to be able to comprehend some of it. The code itself seems fairly simple. It's literally just loads of lists of lists of lists, and has some user defined functions, for identifying, and shuffling "areas" and "items" and stuff.  There's also a lot of logic for stuff like placing items 'if Link has this, this, this" etc. The hard part was seeing the file for the part that actually interacts with the ROM, as that has a lot of that new fangled 0x4855757 N64 ASM stuff. Still mostly just lists though. The file-managing, and compression stuff was still waaay over my head though. 

 

 

EDIT: I am still proud of myself for this modest amount of progress. I went from wondering how on earth someone even begins to make a randomizer, to being able to read through, and understand a lot of the code for one. In only about six weeks. 


  • peteandwally likes this

#27 Saffith

Saffith

    IPv7 user

  • ZC Developers
  • Gender:Male

Posted 16 August 2018 - 12:57 PM

Another project you could look at is PySolFC, a solitaire collection. It's a bit more interesting than just lists (and it's a really good collection of games). You can edit it pretty easily - maybe try adding another game or two.

Let me add, by the way: feel free to PM me any questions I might be able to answer. I don't know if I'd be your first choice for guidance, but I'm always happy to help out with stuff like this.
 

I've also been thinking a lot about programming as more of a way of solving a series of problems. I think that "knowing code" and "knowing how to code well" are two different things.

Absolutely. I think it's fair to say that breaking big problems into smaller ones is the most fundamental aspect of good programming. I know you saw it before, but a lot of what I wrote here applies to application programming as well as scripts.
  • Anthus likes this


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users