Remove this ad

avatar

Lunartic

Posts: 3,609

Blue Thumbs Gaming

Lead

Mar 20 14 3:30 AM

Tags : :



Learning the basics in Python 3.1

Following the advice of Trebz, my first goal is to learn the basics of coding in Python using Al Sweigart's Invent Your Own Computer Games with Python.

It seems like it's a very good resource for complete newbs to coding, it's 100% free and the website for the book includes heaps of additional stuff to help navigate your way through the book.

You can check it out here: http://inventwithpython.com/

"Wow man. You're the greatest kangaroo blooded ninja I've ever cyber met."
"a lot of strange people playing this game but you're a special one..."
Lunartic on Facebook Twitter Everyplay YouTube Badland Forum Blue Thumbs Gaming

Quote    Reply   
Remove this ad
Remove this ad
avatar

Lunartic

Posts: 3,609 Blue Thumbs Gaming

#1 [url]

Mar 20 14 4:43 AM

Re: Learning the basics in Python 3.1

PG001: Hello World

It doesn't get much simpler than this!
Funny thing is, that I seem to recall doing something very similar on an old inherited VIC-20 I got when I was about 9 years old. AFAIR it came with a couple of books and one of them was a coding book, that I copied code out to run a few simple games (avoid the wall etc.). Doing this Hello World code reminded me about that and also the pain that comes with having one incorrect character that you can't find in the code.

Standard code

# This program says hello and asks for my name.
print('Hello world!')
print('What is your name?')
myName = input()
print('It is good to meet you, ' + myName)


Tweaked code

# This program asks additional details.
print('Hello, what is your name?')
Name = input()
print('It is nice to meet you, ' + Name)
print('How old are you?')
Age = input()
print('And where are you from ' + Name)
From = input()
print('What do you do ' + Name)
print('from ' + From)
print('?')
Job = input()

print('OK, so your name is ' + Name)
print('You\'re ' + Age)
print('You\'re from ' + From)
print('and you\'re ' + Job)
print('Is that correct?')
Answer = input()

print('Great. Well thanks for taking the time to chat!')


Further Tweaked code

# This program asks additional details and displays the reply in a single sentence.
print('Hello, what is your name?')
Name = input()
print('It\'s nice to meet you, ' + Name + '.')
print('How old are you?')
Age = input()
print('And where are you from ' + Name + '?')
From = input()
print('What do you do ' + Name + ' from ' + From + '?')
Job = input()

print('OK, so your name is ' + Name + ',' + ' you\'re ' + Age + ', you\'re from ' + From + ' and you\'re a ' + Job + '.')
print('Is that correct?')
Answer = input()

print('Great. Well thanks for taking the time to chat!')


Summary and final output
Really simple stuff here, but it was good to get a solid grasp of simple string concatenation.

>>>
Hello, what is your name?
Dexter Morgan
It's nice to meet you, Dexter Morgan.
How old are you?
42
And where are you from Dexter Morgan?
Miami
What do you do Dexter Morgan from Miami?
blood spatter analyst
OK, so your name is Dexter Morgan, you're 42, you're from Miami and you're a blood spatter analyst.
Is that correct?
yes
Great. Well thanks for taking the time to chat!
>>>

"Wow man. You're the greatest kangaroo blooded ninja I've ever cyber met."
"a lot of strange people playing this game but you're a special one..."
Lunartic on Facebook Twitter Everyplay YouTube Badland Forum Blue Thumbs Gaming

Quote    Reply   
avatar

Lunartic

Posts: 3,609 Blue Thumbs Gaming

#2 [url]

Mar 20 14 4:53 AM

Re: Learning the basics in Python 3.1

PG002: Guess the number game

Standard code

# This is a guess the number game.
import random

guessesTaken = 0

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

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

while guessesTaken < 6:
print('Take a guess.') # There are four spaces in front of print.
guess = input()
guess = int(guess)

guessesTaken = guessesTaken + 1

if guess < number:
print('Your guess is too low.') # There are eight spaces in front of print.

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

if guess == number:
break

if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

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


Tweaked code

#This is a guess the number between 1 and 20 game
import random

guessesTaken = 0

print('G-day! This is the NUMBERS GUESS game by Project 68')
print('What is your name?')
myName = input()

number = random.randint(1,20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
print('I bet your stupid arse can\'t guess what it is in 6 guesses, or less?')

while guessesTaken < 6:
print('Take a guess genious!')
guess = input()
guess = int(guess)

guessesTaken = guessesTaken + 1

if guess < number:
print('HAHAHA Your guess is too low stupid!')

if guess > number:
print('Derp Derp... Your guess is too high numbnuts!')

if guess == number:
break

if guess == number:
guessesTaken = str(guessesTaken)
print('Son of a bitch! ' + myName + ', you guessed my number in ' +
guessesTaken + ' guesses! ')
print('Guess that makes me the dumbarse...')

if guess != number:
number = str(number)
print('HAHAHAHA Nice try idiot but you have run out of guesses!')
print('The number I was thinking of was ' + number + '! ')
print('Better luck next time jerk!')


Further tweaked code

#This is a guess the murderer in the police lineup game.

import random

guessesTaken = 0

print('Hello, what\'s your name?')
Name = input()

number = random.randint(1,10)
print('Hello detective ' + Name + '. You\'ve been tasked with identifying a murderer from 10 potential suspects, who we will refer to from now on as the numbers 1-10.')

while guessesTaken<4:
print('Take a good look at all of the suspects and then make your choice.')
guess = input()
guess = int(guess)

guessesTaken = guessesTaken+1

if guess < number:
print('No, he\'s not the murderer detective ' + Name + '!')

if guess > number:
print('No, he\'s not the murderer detective ' + Name + '!')

if guess == number:
break

if guess == number:
guessesTaken=str(guessesTaken)
print('Congratulations detective ' + Name + '. You\'ve correctly identified the murderer!')

if guess != number:
print('Detective ' + Name + ', you\'ve failed to correctly identify the murderer. You officially suck!')


Summary and final output

There was a bunch of stuff here that I had to go through several times, take notes of and then experiment with in the Phython Shell before I really got a decent handle on what was happening and why.

>>>
Hello, what's your name?
Smart
Hello detective Smart. You've been tasked with identifying a murderer from 10 potential suspects, who we will refer to from now on as the numbers 1-10.
Take a good look at all of the suspects and then make your choice.
4
No, he's not the murderer detective Smart!
Take a good look at all of the suspects and then make your choice.
7
No, he's not the murderer detective Smart!
Take a good look at all of the suspects and then make your choice.
9
Congratulations detective Smart. You've correctly identified the murderer!
>>>

"Wow man. You're the greatest kangaroo blooded ninja I've ever cyber met."
"a lot of strange people playing this game but you're a special one..."
Lunartic on Facebook Twitter Everyplay YouTube Badland Forum Blue Thumbs Gaming

Quote    Reply   
avatar

Lunartic

Posts: 3,609 Blue Thumbs Gaming

#3 [url]

Mar 20 14 5:12 AM

Re: Learning the basics in Python 3.1

PG003: DRAGON REALM GAME

Standard Code

import random
import time

def displayIntro():
print('You are in a land full of dragons. In front of you,')
print('you see two caves. In one cave, the dragon is friendly')
print('and will share his treasure with you. The other dragon')
print('is greedy and hungry, and will eat you on sight.')
print()

def chooseCave():
cave = ''
while cave != '1' and cave != '2':
print('Which cave will you go into? (1 or 2)')
cave = input()

return cave

def checkCave(chosenCave):
print('You approach the cave...')
time.sleep(2)
print('It is dark and spooky...')
time.sleep(2)
print('A large dragon jumps out in front of you! He opens his jaws and...')
print()
time.sleep(2)

friendlyCave = random.randint(1, 2)

if chosenCave == str(friendlyCave):
print('Gives you his treasure!')
else:
print('Gobbles you down in one bite!')

playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':

displayIntro()

caveNumber = chooseCave()

checkCave(caveNumber)

print('Do you want to play again? (yes or no)')
playAgain = input()


Tweaked Code

import random
import time

def displayIntro():
print('You are walking around lost in South Logan and need to ask someone for directions.')
print('In front of you there are two parked utes.')
print('In one ute, a bogan is happy and drunk, will share his beer with you and then give you the directions you need.')
print('In the other ute, a bogan is sober and pissed off, and will knock you the fuck out on sight before driving off, which will leave you lost and of course knocked out.')
print()

def chooseUte():
ute = ''
while ute != '1' and ute != '2':
print('Which ute will you approach? (ute number 1 or ute number 2)')
ute = input()

return ute

def checkUte(chosenUte):
print('You approach the ute...')
time.sleep(2)
print('It has AC/DC pumping on the stereo...')
time.sleep(2)
print('The bogan looks out his window at you! He opens his door and...')
print()
time.sleep(2)

friendlyUte = random.randint(1, 2)

if chosenUte == str(friendlyUte):
print('shakes your hand and gives you a beer!')
else:
print('Knocks you the fuck out!')

playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':

displayIntro()

uteNumber = chooseUte()

checkUte(uteNumber)

print('Do you want to play again? (yes or no)')
playAgain = input()


Summary and final output

This was fun to play around with, however most of the tweaks I did were only story based. I need to go through this again to fully appreciated what is happening and how etc.

>>>
You are walking around lost in South Logan and need to ask someone for directions.
In front of you there are two parked utes.
In one ute, a bogan is happy and drunk, will share his beer with you and then give you the directions you need.
In the other ute, a bogan is sober and pissed off, and will knock you the fuck out on sight before driving off, which will leave you lost and of course knocked out.

Which ute will you approach? (ute number 1 or ute number 2)
2
You approach the ute...
It has AC/DC pumping on the stereo...
The bogan looks out his window at you! He opens his door and...

shakes your hand and gives you a beer!
Do you want to play again? (yes or no)
yes
You are walking around lost in South Logan and need to ask someone for directions.
In front of you there are two parked utes.
In one ute, a bogan is happy and drunk, will share his beer with you and then give you the directions you need.
In the other ute, a bogan is sober and pissed off, and will knock you the fuck out on sight before driving off, which will leave you lost and of course knocked out.

Which ute will you approach? (ute number 1 or ute number 2)
1
You approach the ute...
It has AC/DC pumping on the stereo...
The bogan looks out his window at you! He opens his door and...

Knocks you the fuck out!
Do you want to play again? (yes or no)
no
>>>

"Wow man. You're the greatest kangaroo blooded ninja I've ever cyber met."
"a lot of strange people playing this game but you're a special one..."
Lunartic on Facebook Twitter Everyplay YouTube Badland Forum Blue Thumbs Gaming

Quote    Reply   
avatar

Lunartic

Posts: 3,609 Blue Thumbs Gaming

#4 [url]

Mar 22 14 9:00 AM

Re: Learning the basics in Python 3.1

Oh man I'm really starting to struggle getting my head around some of this code, now that the book has moved into more advanced territory. That's not a good sign considering how I'm only a few chapters in! I'm going to keep plugging away at it, but also start looking into a few other possibilities to maintain my enthusiasm.

"Wow man. You're the greatest kangaroo blooded ninja I've ever cyber met."
"a lot of strange people playing this game but you're a special one..."
Lunartic on Facebook Twitter Everyplay YouTube Badland Forum Blue Thumbs Gaming

Quote    Reply   
avatar

Lunartic

Posts: 3,609 Blue Thumbs Gaming

#5 [url]

Mar 22 14 1:46 PM

Re: Learning the basics in Python 3.1

Wow and just like that after switching my focus I've gone from banging my head against the wall, to having a scrolling background with a bunch of various sized sprite copies! (of what looks to be some kind of monkey-gimp)



"Wow man. You're the greatest kangaroo blooded ninja I've ever cyber met."
"a lot of strange people playing this game but you're a special one..."
Lunartic on Facebook Twitter Everyplay YouTube Badland Forum Blue Thumbs Gaming

Quote    Reply   
Add Reply

Quick Reply

bbcode help