Make a new file form IDLE, and write in it:
print("Hi!")
Hit F5 and enjoy!
Your first(or second) python program.. Quite useless, but nevertheless. A program is a program!
print()
is called a function, functions are kind of mini useful programs, this one will print whatever you tell it. In this case print("Hi!")
will output Hi!
. Pretty amazing, I hope one day to explain to you what goes into showing a character on your screen. Remind me to show you Ben Eater's 6502 videos when you grow up.
"Hi!"
is a string, strings are just series of characters, you can make them in python with "something"
or 'something'
, either single quotes or double quotes.
while True:
print("Hi!")
while True
that means forever. so you will see:
Hi!
Hi!
Hi!
Hi!
Hi!
Hi!
Hi!
Hi!
...
Hi forever.
while 1 == 1:
print("Hi!)
1 == 1
is also True, so this is the same as while True
So while <condition>
will keep running the code inside it, while the condition holds true, which in the case of 1 == 1
will always be the case.
while True:
zzz = input("what is your name: ")
print(zzz)
input
asks you to type something, and it returns whatever you typed.
zzz = input(....)
takes whatever input returns, and puts it into memory that we can use later. zzz
is just a name I picked so I can refer to this value later in the program, in this case on the next line when I do print(zzz)
print(name)
prints whatever is in the memory pointed by zzz
.
zzz
is called a variable
, you can choose the names of your variables, and zzz
is surely a poor name, because if I use it 100 lines later in the code, I will forget what kind of information it stores, so usually we give names of the variables that make sense, for example:
while True:
name = input("what is your name: ")
print(name)
A group of statements with common parent, is called code block, in python we use spaces to say what belongs where, so the closest while,for,if,elif,else
going up is the parent.
while True:
name = input("what is your name: ")
print(name)
Will throw an error: IndentationError: unexpected indent
because it makes no sense, there is no valid parent to print
, This is quite unique in python, most other languages make code blocks with {}
, but python makes it prettier and easier to read by using indentation (the spaces/tabs). print(name)
has 4 spaces, while name = input..
has 2 spaces, so python expect everything in the while:
to have 2 spaces unless a new block starts.
while True:
name = input("what is your name: ")
if name == "pikachu":
print(name)
This works fine, print(name)
now has valid parent that also has a parent.
while True:
| \
| \
| \
| \
if: name = input
|
print(name)
while True
has two children, name = input ..
and if name == ..
so they must have same indentation, then if ..:
also expects a code block, so the children of if name == ..
need to have one more indentation to whatever indentation the if
had.
Anyway..
Don't worry too much about it, just don't panic when you see IndentationError
, and I can promise you, you will see a lot of those. Look at where you got the spaces wrong, and if the block has correct parent.
You have seen by now in JavaScript we use {}
to group statements into blocks.
if (name == "pikachu") {
console.log("pikaaaachuuuuu")
console.log("evolutiooonn!")
}
Now lets break out of the loop!
while True:
name = input("what is your name: ")
if name == "pikachu":
break
if name == "pikachu":
will run the code inside if
if whatever is in the name
variable is equal to the string "pikachu". In this case its only 1 instruction inside it, the break
instruction.
break
breaks out of the closest while
loop, so basically this program will ask what is your name until you type pikachu. It is a bit boring because we never actually print what you typed.
while True:
name = input("what is your name: ")
print(name)
if name == "pikachu":
break
print("DONE")
There we go, now it will ask you to type a name, it will print whatever name you typed, and then it will check if the name is equal to "pikachu" it will break the while loop and stop asking. We can actually write this program in a different way. After you break out of the loop, it just continues to execute the program below, in this case will print DONE.
name = ''
while name != "pikachu":
name = input("what is your name: ")
print(name)
print("DONE")
MAGIC!
we start by making name equal to empty string, a string with no characters, and then we check is the statement name != "pikachu"
True? If this is True we will execute the code inside the while loop, after the last instruction in the loop, the program jumps back to while name != "pikachu"
and checks again is name still not equal to "pikachu"? if the statement is False it will not run the code inside but will continue, in the above example it will print DONE, because this is the first thing after the while loop.
Lets make a more complicated one, this one will ask you what is your name, and if you answer pikachu will print pikaaaaaaachuuuuu and stop, any other answer it will print "Hello, " + answer, so if you type "Jane" it will show "Hello, Jane" and it will ask again.
while True:
name = input("what is your name: ")
if name == "pikachu":
print("pikaaaaaaachuuuuu")
break
else:
print("Hello, " + name)
In python you can add two strings, if you type "charmander" for name, x = "Hello, " + name
will make x to be equal to "Hello, charmander". You can't do "hello" - name
, but you can do "hello" * 5
and get "hellohellohellohellohello".
while,if,else,break
are keywords, kind of like <html>
and <body>
, <h1>
etc, those are coming from python itself. There are not many python keywords, for reference, this is the complete list:
False
True
None
and -> name == "pikachu" and age == "33"
or -> name == "pikachu" or name == "charmander"
not -> not name == "pikachu" is the same as name != "pikachu"
for -> used if you know how many times you want to do something
while -> do something until condition is True
break -> break out of the for or while loop
continue -> go to the start of the while/for loop and continue from there
if -> if something is true
elif -> else if something else is true
else -> else do this
in -> checks if something is in somethhing else, e.g. "pika" in name
def -> make a function
as
assert
async
await
class
del
except
finally
from
global
import
is
lambda
nonlocal
pass
raise
return
try
with
yield
THATS THE WHOLE LANGUAGE, there are no more keywords.
print("Welcome to the forest!")
print("This is a world of magic and wonders. You are on a cross road, you can go north east south or west.")
print("South leads to the swamps, where the aligators live")
print("West leads to the mountains, where the yeti lives")
print("North leads to the jungle, where the tigres live")
print("East leads to the desert, where the meerkats live")
print("")
what = input("what would you do next: ")
if what == "east":
print("Welcome to the desert")
print("It is very hot, and you need remember to put sun screen.")
print("Oh, you remember to put your hat as well.")
print("In the distance you see a meerkat approaching.")
print("What would you do? Run or Fight the meerkat?")
print("")
what = input("what would you do next: ")
if what == "run":
print("You start running, and the meerkat is super fast, it catches you in no time.")
elif what == "fight":
print("You try to fight it, but turns out it was friendly and wants to become your friend.")
print("What would you do?")
print("")
what = input("Do you want to be its friend: ")
if what == "yes":
print("Great! Now the meerkat wants to introduce you to his family.")
elif what == "no":
print("The meerkat starts crying!")
else:
print("I dont understand: " + what)
That is a lot of typing.
In python there are few very important things. First even though it doesn't look like it, it is actually a tree, like HTML tree. The parent of print("The meerkat starts crying!")
is elif what == "no":
|
|
if what == "east"
|
/ | \
/ | \
/ | \
/ | \
/ | \
if else elif
what == "run" | what == "fight
| | |
print print / \
/ \
/ \
/ \
if elif
what == "yes" what == "no"
| |
[ ... ] [ ... ]
Lets discuss another example:
a = "he"
b = "ll"
c = "o"
if a == "he":
if b == "ll":
if c == "o":
print("hello")
Can you tell who is the parent of who?
BTW, you can also use and
, or
and not
when you want to see if something is True
a = "he"
b = "ll"
c = "o"
if a == "he" and b == "ll" and c == "o":
print("hello")
Our game is quite limited, and a quick step to improve it is to make you ask where you want to go until you pick one of the options.
def ask(possible_answers):
answer = ''
print("---") # print empty line
while True:
answer = input("> What would you do next: ")
if answer not in possible_answers:
print("> try again, it must be one of:", possible_answers)
else:
return answer
print("Welcome to the forest!")
#...
what = ask(["east","west","north","south"])
if what == "east":
print("Welcome to the desert")
#...
what = ask(["run","fight"])
if what == "run":
print("You start running, and the meerkat is super fast, it catches you in no time.")
elif what == "fight":
print("You try to fight it, but turns out it was friendly and wants to become your friend.")
# ...
what = ask(["yes","no"])
if what == "yes":
print("Great! Now the meerkat wants to introduce you to his family.")
elif what == "no":
print("The meerkat starts crying!")
elif what == "north":
print("Welcome to the north pole, it is super cold here.")
ask
is a function, just like print
or input
, it will keep asking you > What would you do next:
until the answer you type is in the possible_answers list, and if it is it will return it to wherever it is called from. So when we have zzz = ask(["yes","no"]
the value of zzz
will be whatever is returned from ask
. Same as name = input("what is your name)
will put in name
whatever is returned from input
which is whatever you typed on your keyboard.
To make a function in python you need to use the def
keyword.
def sum(a,b):
return a + b
r = sum(1737,1231231)
print(r)
Whatever is between def
and (
is the name of the function, in the above example its sum
. Between ()
you put in the name of the variables you expect to use when someone calls your function. I want to sum two numbers, I dont know what the numbers are, so I just make two variables a, b
and expect whoever calls my function to give me the numbers, like r = sum(1737,1231231)
FIGHT TO THE DEATH!
import random
import time
def fight(playerHP, enemyHP, enemy_name):
# fight to the death!
while playerHP >= 0 and enemyHP >= 0:
punch = random.randint(0, 20)
if random.choice(["player","enemy"]) == "player":
playerHP = playerHP - punch
print("<"+enemy_name+"> hits you for " + str(punch) + ", " + str(playerHP) + " left")
else:
enemyHP = enemyHP - punch
print("you hit <"+enemy_name+"> for " + str(punch) + ", " + str(enemyHP) + " left")
time.sleep(1)
return playerHP
fight(100, 50, "meerkat")
import
imports a module.
random
and time
those are the modules
you import, modules are just a group of functions that you can use, for example time.sleep(1)
makes python sleep for 1 second, it calls the function sleep()
in the time
module. random.choice()
you can give a list of things to choose from, and it will randomly select one of the list. random.randint(0,20)
means give me a random number between 0 and 20.
str
is needed to convert integer to string, because 1 is not the same as "1", "1" is actually the ASCII value of the character 1, so somehow we have to convert the raw number 1 to ASCII code 61, and str()
does that.
lets use it now in our dungeon game
import random
import time
def ask(possible_answers):
...
def fight(playerHP, enemyHP, enemy_name):
...
...
health = 100
what = ask(["east","west","north","south"])
if what == "east":
...
what = ask(["run","fight"])
if what == "run":
...
elif what == "fight":
meerkatHP = 50
health = fight(health, meerkatHP, "meerkat")
if health <= 0:
print("GAME OVER! THE MEERKAT BEAT YOU")
else:
print("You won against the meerkat, you have " + str(health) + " HP left))
Today you have to make only two programs:
bad = ["broccoli","chocolate","pineapple"]
while True:
food = input("what is your favorite food: ")
if food in bad:
print("ewwwww I hate " + food)
else:
print("yumm, I love " + food)
And the second one:
bad = ["broccoli","chocolate","pineapple"]
good = ["pizza","popcorn"]
while True:
food = input("what is your favorite food: ")
if food in bad:
print("ewwwww I hate " + food)
elif food in good:
print("yumm, I love " + food)
else:
print("I have never tried " + food)
Great job!
Spend the rest of the day touch typing.
for
does something number of times, if I want to print the numbers from 0 to 99, I could do:
for i range(100):
print(i)
or
for i in range(10, 20):
print(i)
will print the numbers from 10 to 19
colors = ["red","green","blue"]
for color in colors:
print(color)
will print each of the elements in the list. If you want to print each character of a string in a similar way you can do:
name = "jack"
for c in name:
print(c)
prints:
j
a
c
k
def love_test(a,b):
sum = 0
for c in a+b:
print(c + ': ' + str(ord(c)))
sum += ord(c)
print('sum is:' + str(sum))
return sum % 100
while True:
nameA = input("first name: ")
nameB = input("second name: ")
print("love test: " + str(love_test(nameA,nameB)))
ord
takes the ascii code of a character.
%
is the remainder, so 109 % 100
is 9, basically what is left after you can cleanly divide two numbers, 812 % 100
is 12, you can fit 100 in 819 exactly 8 times, and then there is 12 left, this 12 is the remainder.
So in this program we take two names into the variables nameA
and nameB
and then we give them to the love_test function, which sums the ascii code of nameA+nameB
, and then returns the remainder of 100.
first name: jack
second name: jane
j: 106
a: 97
c: 99
k: 107
j: 106
a: 97
n: 110
e: 101
sum is:823
love test: 23
BTW, now you know how those "professional" love testers are made, so if you use https://www.lovetester.nl/ or something similar.. don't read too much into the result. You can easily tweak it to do whatever you want.
Whoaa it has been a difficult week.
Relax with some touch typing.