pythonpoetry - Somerset Learning Platform

advertisement
Python Programming – Poetry writing
Challenge 1
Create a new Python document – File, New Window
Copy this code and paste it into a new file
print("Mashed potatoes on the ceiling.")
print("Green beans on the floor.")
print("Stewed tomatoes in the corner.")
print("Squash upon the door.")
Click on File and then “Save as” naming it mash1.
Click on Run and then Run Module.
The poem should print out in the interpreter.
Congratulations you have used the print command.
Challenge 2
Create a variable called veg by copying and adapting this above the print lines
veg = "carrots"
Delete the word potatoes on the first print line and change that line to look like this.
Make sure you put the commas and speechmarks in exactly the same place.
print("Mashed ",veg," on the ceiling.")
Click on File and then “Save as” naming it mash2.
Click on Run and then Run Module.
If you have coded correctly whatever word you put inside your variable called veg should appear in your poem when
printed to screen.
lead ▪ learn ▪ protect ▪ engage
www.somersetelim.org
1
Python Programming – Poetry writing
Challenge 3
In the last challenge you deleted potato and replaced it with a variable called veg.
In this challenge delete the other vegetables on other lines and replace them with the same veg variable (Squash in
the last line is a vegetable).
Don't forget to surround veg with commas and close the ordinary printed words with speech marks.
# for example
print("Green",veg,"on the floor.")
print(veg,"upon the door.")
Click on File and then “Save as” naming it mash3.
Click on Run and then Run Module.
Does the word inside your variable appear in every line?
If it does, try changing the word inside the variable.
Save and run your work again.
Challenge 4
Change your veg variable by adding an input command. (make sure you only have one veg= variable at the top)
veg = input("Type in the name of a vegetable ")
This will ask the user to type in a vegetable when the program is run.
Whatever they type in will then appear inside the printed poem.
Click on File and then “Save as” naming it mash4.
Click on Run and then Run Module.
If your program asks the user to type in a vegetable and then puts their typed text inside the lines of the poem you
have passed the challenge.
lead ▪ learn ▪ protect ▪ engage
www.somersetelim.org
2
Python Programming – Poetry writing
Challenge 5
Open mash3.py that you created earlier.
At the top of the code create a new variable for each line of the poem. You will need four.
# for example
veg1=”carrot”
veg2="cabbage"
veg3="radish"
veg4=”lettuce”
Put these new variables into all of the different lines of your print commands.
# for example
print("Green",veg2,"on the floor.")
Click on File and then “Save as” naming it mash5.
Click on Run and then Run Module.
If it works correctly each line should have a different vegetable taken from each variable.
Challenge 6
Open mash3.py or copy this code
print("Mashed",veg," on the ceiling.")
print("Green",veg," on the floor.")
print("Stewed",veg," in the corner.")
print(veg,"upon the door.")
Above all your code import random
import random
#This allows us to use the random command later
Underneath random create a list by copying and expanding this code with some of the vegetables underneath
veglist = ["beans","broccoli"]
Artichoke Asparagus Beans peas Broccoli Sprouts Cabbage Carrots Cauliflower Celery Cucumber Eggplant Fennel Garlic
Lettuce Mushroom Okra Onion Pepper Potatoes Pumpkin Radish Rhubarb Spinach Squash Corn Tomato Turnip
Underneath veglist adapt your veg variable so that it is chosen randomly from your veglist
veg = random.choice(veglist)
# a random word is chosen from your veglist and put into the variable veg
Click on File and then “Save as” naming it mash6.
lead ▪ learn ▪ protect ▪ engage
www.somersetelim.org
3
Python Programming – Poetry writing
Click on Run and then Run Module.
If your coding is correct a randomly choosen vegetable will be used within the poem.
Extra Challenges
Choose a challenge you fancy doing or make one up.
Challenge 7
Save as mash7.py
Adapt the poem challenge 6 so that is randomly chooses a different vegetable from the veglist on each line.
Challenge 8
save as mash8.py
Create a program that randomly chooses a member of your class so that your teacher can use it in class.
Create a new list with people in your class in it. Randomly choose a name from the list and put it into a new variable.
Finally put the randomly chosen name in a variable into a print command. Don't forget to import random at the top.
Challenge 9
Save as mash9.py
Using techniques learn't, write a program to roll a six sided dice.
Challenge 10
Use the instructions at http://www.pythoncode.co.uk/python-adventure to create an adventure game.
lead ▪ learn ▪ protect ▪ engage
www.somersetelim.org
4
Download