Homework 5
this is the fifth homework for 12-13
What are procedures?
Fill in the blanks please:
Procedure: named group of programming instructions.
Parameters: input values of a procedure
Arguments: specify value of parameters when procedure is called
Modularity: seperating a program's funtions into seperate pieces of code
Procedural Abstraction:
What are some other names for procedures?:
Why are procedures effective?:
decimal = 7
def convertToBinary(n):
binary1 = 0
binary2 = 0
binary3 = 0
binary4 = 0
binary5 = 0
binary6 = 0
binary7 = 0
binary8 = 0
i = 0
while i < n:
if binary1 == 0: binary1 += 1
else:
binary1 = 0
if binary2 == 0: binary2 += 1
else:
binary2 = 0
if binary3 == 0: binary3 += 1
else:
binary3 = 0
if binary4 == 0: binary4 += 1
else:
binary4 = 0
if binary5 == 0: binary5 += 1
else:
binary5 = 0
if binary6 == 0: binary6 += 1
else:
binary6 = 0
if binary7 == 0: binary7 += 1
else:
binary7 = 0
if binary8 == 0: binary8 += 1
i += 1
print(n)
print(str(binary8) + str(binary7) + str(binary6) + str(binary5) + str(binary4) + str(binary3) + str(binary2) + str(binary1))
convertToBinary(decimal)
function findMax(numA, numB){
if (numA > numB){
console.log(numA)
}
else{
console.log(numB)
}
}
findMax(20, 40)
Homework/Hacks: For the hw, you have two options, easy or hard. The easy hack is for a 2.7 + extra work for the full 3. The easy hack is simply creating your own procedure with your own creativity. Since there is a lot of leeway for this one, you must do additional work to get a 3. For the hard hack, below is the start to a character to binary convertor. This is just a template, but the goal is to translate "APCSP" into binary. You can delete the existing code if you want. The only contraint is that you must use a procedure. Doing this will get you a 3.
def charToBinary(x):()
# if x.lower == x
# The output shown below is the output you are supposed to get
# Feel free to copy this code into your own fastpages to test it.
import random
# Make sure to include the import, it's very important
sword = 20
hammer = 30
num3 = [1,2,3]
healthOp = 15
health = 15
equipped = 0
defval = False
def equip(output):
global equipped
global hammer
global sword
equipped = output
def durLow():
global equipped
global sword
global hammer
dur = random.randrange(3)
if equipped == sword:
equipped -= num3[dur]
sword -= num3[dur]
elif equipped == hammer:
equipped -= num3[dur]
hammer -= num3[dur]
else:
print("No durability loss, the code broke")
def choose():
global choice
global hammer
global sword
choice = input("What would you like to do?")
if choice.lower() == "equip":
print("What would you like to equip?")
print("Hammer: " + str(hammer))
print("Sword: " + str(sword))
chooseEquip()
choose()
elif choice.lower() == "attack":
if equipped > 0:
attack()
durLow()
else:
print("You must have a weapon to fight")
choose()
elif choice.lower() == "defend":
global defval
defval = True
elif choice.lower() == "check":
statCheck()
choose()
elif choice.lower() == "break":
global healthOp
healthOp = 0
else:
print("You can not do "+str(choice)+", please do something else")
choose()
def chooseEquip():
don = input("Choose a weapon")
if don.lower() == "hammer":
equip(hammer)
elif don.lower() == "sword":
equip(sword)
else:
print("You do not have "+str(don)+", please choose a weapon")
chooseEquip()
def attackOp(): # test if the defval boolean works in the if statement
global health
global defval
if defval:
bic = random.randrange(3)
health -= bic
if bic == 0:
print("The opponent missed!")
else:
print("You blocked the attack. The opponent did " + str(bic) + " damage")
else:
bic = random.randrange(5)
health -= bic
if bic == 0:
print("The opponent missed!")
else:
print("The opponent did " + str(bic) + " damage")
defval = False
def attack():
global healthOp
bru = random.randrange(5)
if bru == 0:
print("Your attack missed!")
else:
healthOp -= bru
print("You did " + str(bru) + " damage")
def statCheck():
print("Your health: "+str(health))
print("Your opponent's health: "+str(healthOp))
if equipped > 0:
print("Your weapon's durablility: "+str(equipped))
else:
print("You must have a weapon equipped to check its durability")
print("There is an enemy in front of you. You have a sword and hammer. You must equip one to fight")
print("You can fight (attack), check battle statistics (check), and defend (defend). To equip a weapon you must type 'equip'")
while health > 0:
choose()
if healthOp <= 0:
break
if health <= 0:
break
attackOp()
if health <= 0:
print("You died")
if healthOp <= 0:
print("You win!")
print("End stats:")
statCheck()