Back to Python coding again! Let me stick with the food
to create my functions…
# begin of this block
def start():
print "I'm now in the supermarket. We will have a party tonight!"
print "I have to buy some food for my guests!"
print "I need to check my shopping list!"
def party_food():
return "cheese", "herb", "tomato", "garlic", "egg" # return the value
def my_party_food(shopping_list):
# attach the argument “shopping_list” to the function”my_party_food”
return "%s is in the shopping list of my party food!" % shopping_list
# “%” is the basic operator, “%s” is the string formatting way, any object can be formatted with this “%s” operator.
def the_amount_of_party_food(amount):
return "I need to buy %d boxes of party food!" % amount
# “%d” is another common operator for Integers.
def checklist_of_my_party_food():
my_list = party_food()
for shopping_list in my_list: # we use 'in' to check if the 'shopping_list' is in the container 'my_list'.
print my_party_food(shopping_list)
my_amount = party_food()
for amount in my_amount:
print the_amount_of_party_food(5) # here we directly give the function argument '5' .
break # use break to tell the system to print “I need to buy…..each party food” once.
start()
checklist_of_my_party_food() # call our function
# end of this block
*******************************************************************************************
If
we execute the code, we will see the following output:
I'm now in the supermarket. We will have a party tonight!
I have to buy some
food for my guests!
I need to check my
shopping list!
cheese is in the
shopping list of my party food!
herb is in the
shopping list of my party food!
tomato is in the
shopping list of my party food!
garlic is in the
shopping list of my party food!
egg is in the
shopping list of my party food!
I need to buy 5
boxes of party food!
*********************************************************************************************
CodeCombat Python:
Level ---- “Haunted
Kithmaze”
Goals:
- Survive!
- Navigate the maze.
- Under 6 statements.
Code:
while True:
self.moveRight(2)
self.moveUp(2)
screenshot of "Haunted Kithmaze"
Level --- “Cupboards of
Kithgard”
Goals:
·
Break
open the cupboard.
·
Under
7 statements.
Code:
self.moveUp()
self.moveRight(2)
self.moveDown(2)
while True:
self.attack(“Cupboard”)
screenshot of "Cupboards of Kithgard"
Level ---- “Known Enemy”
Goals:
- Survive!
- Kill the three ogres.
Code:
enemy1 = “Kratt”
enemy2 = “Gert”
enemy3 = “Ursa”
while True:
self.attack(“Kratt”)
self.attack(“Gert”)
self.attack(“Ursa”)
screenshot of "Known Enemy"
No comments:
Post a Comment