Now that you know a few of the basics that come with the Python program, it is time to work on a few projects to make it even more interesting. This can help you to get more practice with what we have learned inside this book and ensures that you get some extra practice with writing out the code before getting started with your own code. In this final chapter, it is time to practice a few different codes that it is possible to try out, games and other guessing options, that you are able to try out with some of these Python codes. Let’s take a look at all the fun things that you are able to do when writing in Python to help see some of the power that comes with this great code.
Creating your own magic 8 ball game
Think back to your childhood when you played with a magic 8 ball, asking it questions and seeing what answers you were able to get at the same time. It was a simple game, you had to be careful with some of the questions that you asked because there were only a few answers that you were able to get out of the system, but it could be a fun game. You are able to make this same kind of game using the codes that we have discussed in the Python coding language. The code that you would need to use to make your own magic eight ball inside Python includes:
# Import the modules
import sys
import random
ans = True
while ans:
question = raw_input(“Ask the magic 8 ball a question: (press enter to quit)”)
answers = random.randint(1,8)
if question == “”
sys.exit()
elif answers ==1:
print(“It is certain”)
elif answers == 2:
print(“Outlook good”)
elif answers == 3:
print(“You may rely on it”)
elif answers == 4:
print(“Ask again later”)
elif answers == 5:
print(“Concentrate and ask again”)
elif answers == 6:
print(“Reply hazy, try again.”)
elif answers == 7:
print(“My reply is no”)
elif answers == 8:
print(“My sources say no”)
Here we just provided you with 8 answers, but you can cut it down a few to make it easier or give twenty answers if that is what you would like. It is set up to be random and pick the answers differently each time, which can make it easier for you to get something different each time that you ask a new questions. This is a good way for you to use some of the topics that we discussed in this book and you can have some fun playing the game by asking it questions during the testing period.
Let the computer roll the dice
Another game that you are able to create is one where you tell the Python program to roll the dice for you. This one will also use the random idea that we did above because you want the computer to determine different numbers on the dice each time that you roll; no one wants to play the game and get the exact same numbers each time the do it so the random module will help to make sure that this happens. In this code, it is your responsibility to set up two different variables, the maximum and the minimum, so that the system will only pick out numbers that are on your code. For example, you will want to make sure that your minimum on this one is 1 and the maximum is 6. If you would like to make a special die, you could add in more numbers, but we will just keep it simple for this one.
You will notice in this example that we are working with the loop function. This allows the user to roll the dice as many times as they would like without restarting the code, rather than just doing it one time. We are going to use the “y” for yes so that the user is able to click on it and roll the dice again and again as they would like.
So, to create this new game, we would need to use the following code to tell the compiler what to do:
Import random
min = 1
max = 6
roll_again = “yes”
while roll_again == “yes” or roll_again == “y”:
print(“Rolling the dice...”)
print(“The values are...”)
print random.randint(min, max)
print random.randint(min, max)
roll_again = raw_input(“Roll the dices again?)
Creating a Hangman game
Here we are going to look at how to create the Hangman game with the help of your Python compiler. If you are a fan of this game, you are going to see that it is pretty easy to create this game all on your own. You will be able to use some dashes that are in a row in order to represent the word that you are trying to have the user guess and when the user does guess a letter that is inside the word, the script will be able to place this letter in the correct spot.
For the game that we are going to make, we will allow the player to guess 10 times. After the 10 times, or when the user guesses the word ahead of that many times, the game is going to end. You will be able to choose the variables and make it a bit different, such as giving it the ability to let the user guess more or fewer times, but here we are going to keep things simple and stick with 10. The code that is needed in order to create this game includes:
# importing the time module
importing time
#welcoming the user
Name = raw_input(“What is your name?”)
print(“Hello, + name, “Time to play hangman!”)
print(“
“
#wait for 1 second
time.sleep(1)
print(“Start guessing...”)
time.sleep(.05)
#here we set the secret
word = “secret”
#creates a variable with an empty value
guesses = ‘ ‘
#determine the number of turns
turns = 10
#create a while loop
#check if the turns are more than zero
while turns > 0:
#make a counter that starts with zero
failed = 0
#for every character in secret_word
for car in word:
#see if the character is in the players guess
if char in guesses:
#print then out the character
print char,
else
# if not found, print a dash
print “_”,
# and increase the failed counter with one
failed += 1
#if failed is equal to zero
#print You Won
if failed == 0:
print(“You Won”)
#exit the script
Break
# ask the user go guess a character
guess = raw_input(“guess a character:”)
#set the players guess to guesses
guesses += guess
# if the guess is not found in the secret word
if guess not in word:
#turns counter decreases with 1 (now 9)
turns -= 1
#print wrong
print(“Wrong”)
# how many turns are left
Print(“You have,” + turns, ‘more guesses’)
#if the turns are equal to zero
if turns == 0
#print “You Loose”
print(“You Loose”)
As you can see, this is one that has some more complexities to it because of the nature of the game, but it has many of the different parts in it that you need to practice when using this code. You will be able to add in more parts, or take them away and maybe try just having 5 practices or tries inside of it to make it easier, but you will be able to try it out and play a fun game with all of the information that you learn inside of this guidebook and the code that is above.
Making some of your own projects inside of Python is one of the best ways to work on learning the code. Many of these games bring together different parts of the information that we learned inside of this code so that you can see that they aren’t just some random concepts that are talked about but you will never actually used. In fact, many of the options that we discuss are going to be used together inside the same code in order to make it functions. As you get used to seeing some more of these things together and you practice writing out codes like the one above, you will be better able to see how all of the parts need to be present in order for the code to work properly.