Python
Some neat Python basics!
I learned a lot of things at GIV. This is a little presentation about some of the cool and interesting Python things I learned :) I hope you enjoy it! Here's a print statement:
print("Hello, World!")
What does it do? It "prints" out whatever you tell it to in the box executing the code. You can have it type other things too! For example:
print("Hi, my name is Matthew, and I'm excited to share the things I learned at GIV this summer :D")
Variables are numbers that can be changed and manipulated. You can define them and call them back, even use them in print statements like this:
myVariable = 16
print("I'm " + str(myVariable))
Which returns: "I'm 16", because the variable was called in the print function. We can use other kinds of statements to serve other purposes, such as "if", "elif", and "while".
myVariable = 16
if myVariable == 15:
print("I'm 15 lol")
elif myVariable == 17:
print("I must be 17 then")
else:
print("Truthfully, I am 16. But I checked two other numbers first just to make sure ;)")
Output: Truthfully, I am 16. But I checked two other numbers first just to make sure ;)
This opens up a world of possibilies. With what I've shown you so far, we can already make some simple games. But with just a couple additions, we can do even better. Let's make a simple number guessing game. But first, we need to learn about two more things:
import random
myRandomNumber = random.randint(1,10)
while iLoveWaffles == True:
print("I am a good person.")
break
"Random" is basically a Python extension you can add to add new functions. In this case, we can use it to
generate
a random number and assign it to a variable.
And a "while" loop can run until a condition is met (or unti a condition isn't met anymore.) That means we can
let
our player keep guessing numbers until they're correct. This is what a guessing game could look like:
import random
print("Welcome to Guess the Number!")
randomNumber = random.randint(1,1000)
guesses = 0
playerGuess = 0
while playerGuess != randomNumber:
playerGuess = int(input("Guess the number between 1 and 1000: "))
guesses = guesses + 1
if playerGuess == randomNumber:
print("you did it!")
print("it took you " + str(guesses) + " times!")
elif playerGuess > randomNumber:
print("Too High!")
elif playerGuess < randomNumber:
print("Too Low!")
else:
print("wtf")
if guesses > 9:
break
if playerGuess != randomNumber:
print("you lost :( the answer was: " + str(randomNumber))
Guessing between a thousand numbers, as I have done here, is quite challenging, but possible! Another interesting Python thing I learned was something called Functions. These are snippets of code we can call back, instead of having to write it all out. Here's what that looks like:
def myFunction:
print("myNumber")
myFunction()
Because we told the computer what we wanted "myFunction" to do in the beginning, it's really easy to recall it again. In this case, it's a number we wanted to remember without having to type the "print()" statement everytime. To wrap up, here's one of the bigger Python games I made:
print("You wake up, washed up onto shore.")
print()
print("You look around and see a barren island, with nothing on it.")
print()
print("Your clothes are ruined, and there is no land in sight, except for where you're standing.")
print()
def loop():
answer = input("""Here are your options: "give up", "survive", or "thrive" """)
if answer == "give up":
print("==================")
print("""With the last of your energy escaping from you, your soul is smashed into a million pieces and eternal darkness forever surrounds you. The End <3""")
restart()
elif answer == "survive":
print("==================")
print("""For the next ten years, you build up your strength. You use the extremely limited resources around to you teach yourself to fish. You build a house with the oceanic materials.""")
print("""Despite many close calls, you learn to thrive in this environment. One day, many years later, you are rescued. Your family, assuming you had died, was unspeakably happy to have you back. The End <3""")
restart()
elif answer == "thrive":
print("==================")
print("""YOU SWIM TO THE NEAREST INHABITED ISLAND, STEAL WEAPONS AND MILITARY VEHICLES, AND LIVE A LIFE OF PURE STRENGTH AND POWER. THE MOST UNSTOPPABLE HUMAN ON THE PLANET, YOU DESTROY EVERYTHING THAT EVER HARMED YOU. THE ENTIRETY OF HUMANITY IS YOURS. The End <3""")
restart()
else:
print("==================")
print("""HUH????????""")
loop()
def restart():
answer = input("""Would you like to restart? ("yes", or "no"): """)
if answer == "yes":
print()
print("Ok, let's start back from the beginning:")
loop()
elif answer == "no":
print("Ok then.")
else:
print("HUH???")
restart()
loop()
My teacher Liz told me I should expand it further, so I did! Making use of lists and their related functions, I added a second layer to the game. It's by no means the best coding, but it worked!
layer = 1
def restart():
answer = input("""Would you like to restart? ("yes", or "no"): """)
if answer == "yes":
print()
print("Ok, let's start back from the beginning:")
loop1()
elif answer == "no":
print("Ok then.")
else:
print("HUH???")
restart()
def br():
print("==================")
ans1 = [
"""With the last of your energy escaping from you, your soul is smashed into a million pieces and eternal darkness surrounds you.""",
"""For the next ten years, you build up your strength. You use the extremely limited resources around to you teach yourself to fish. You build a house with the oceanic materials.
Despite many close calls, you learn to thrive in this environment. One day, many years later, you are rescued. Your family, assuming you had died, is overjoyed to know you're alive!""",
"""YOU SWIM TO THE NEAREST INHABITED ISLAND, STEAL WEAPONS AND MILITARY VEHICLES, AND LIVE A LIFE OF PURE STRENGTH AND POWER."""
]
q2 = [
"""Here are your options: let go("1"), or defeat your mental enemies("2") """,
"""Here are your options: stay on the island and live a life of peace and solitude("1"), or go home("2") """,
"""Here are your options: use your powers for good("1"), or for bad("2") """
]
ans2 = [
"""You let go of everything around you. The darkness consumes you and the last strands of your mental being are dissolved into nothingness. The End <3""",
"""With fierce determination, you force yourself to stay alive in this dark empty world. When you feel the moment is right, you pull out and return to earth! As a bumblebee of course :3 The End <3""",
"""You stay on your island, living a peaceful life. You cut yourself off from modern humanity and resolve to be content in your own world. The End <3""",
"""You decide to go home to your family, who care about you very much! You live happily ever after. The End <3""",
"""A wise choice! With all your resources, you can make the world a better place for everyone. Life becomes much better for almost everyone! Good job! The End <3""",
"""Dang. You just want to cause destruction, huh? Well whatever, it's your life. And that's what you do: destroy and ruin the world around you. Does it bring you temporary joy? Perhaps. But are you truly happy? That's up to you to decide. The End <3"""
]
print("""You wake up, washed up onto shore.
You look around and see a barren island, with nothing on it.
Your clothes are ruined, and there is no land in sight, except for where you're standing.
""")
def loop1():
answer = input("""Here are your options: give up("1"), survive("2"), or thrive("3") """)
if answer == "1":
br()
print(ans1[0])
answer = input(q2[0])
br()
if answer == "1":
print(ans2[0])
else:
print(ans2[1])
restart()
elif answer == "2":
br()
print(ans1[1])
answer = input(q2[1])
br()
if answer == "1":
print(ans2[2])
else:
print(ans2[3])
restart()
elif answer == "3":
br()
print(ans1[2])
answer = input(q2[2])
br()
if answer == "1":
print(ans2[4])
else:
print(ans2[5])
restart()
else:
br()
print("""HUH????????""")
loop1()
loop1()
I hope this was a good demonstration of what I learned in this class! It was fun to work with others who share my interests in Python and coding in general :D