CSCI 110, Spring 2011
Home | | Course Schedule | | Assignments | | Lecture Notes Homework 3 solutionPart a: Solution to battle1.py program:
# program battle1.py # Author: A. Professor # Section: CSCI 110 # Date: February 18, 2010 # Purpose: Battle a troll. import random #This imports the random function from a library print "You find yourself in a dark cave, facing a large troll." print "Your health level is 10." print "The troll's health level is 10." print "You may attack the troll or run away." print "If you attack, you might miss, allowing the troll to attack you." print "If you run away, you stay healthy, but you will not complete the mission." user_health = 10; troll_health = 10; response = raw_input("Do you want to attack? ") while response == "yes": attack = random.randrange(1, 6) #strength of attack #attack = random.randrage(5) + 1 works as well print "Your attack succeeded in wounding the troll." troll_health = troll_health - attack print "Your health level is", user_health print "The troll's health level is", troll_health response = raw_input("Do you want to attack? ") if troll_health <= 0: print "You have defeated the troll!" print "You may continue your quest." else: print "You ran away. You live to fight another day!"
Part b: Solution to battle2.py program:
# program battle2.py # Author: A. Professor # Section: CSCI 110 # Date: February 18, 2010 # Purpose: Battle a troll. import random #This imports the random function from a library print "You find yourself in a dark cave, facing a large troll." print "Your health level is 10." print "The troll's health level is 10." print "You may attack the troll or run away." print "If you attack, you might miss, allowing the troll to attack you." print "If you run away, you stay healthy, but you will not complete the mission." user_health = 10; troll_health = 10; response = raw_input("Do you want to attack? ") while response == "yes" and troll_health > 0: attack = random.randrange(1, 6) #strength of attack print "Your attack succeeded in wounding the troll." troll_health = troll_health - attack print "Your health level is", user_health print "The troll's health level is", troll_health if troll_health > 0: response = raw_input("Do you want to attack? ") if troll_health <= 0: print "You have defeated the troll!" print "You may continue your quest." else: print "You ran away. You live to fight another day!"
Part c: Solution to battle3.py program:
# program choose.py # Author: A. Professor # Section: CSCI 110 # Date: February 18, 2010 # Purpose: Battle a troll. import random #This imports the random function from a library print "You find yourself in a dark cave, facing a large troll." print "Your health level is 10." print "The troll's health level is 10." print "You may attack the troll or run away." print "If you attack, you might miss, allowing the troll to attack you." print "If you run away, you stay healthy, but you will not complete the mission." response = raw_input("Do you want to attack? ") user_health = 10; troll_health = 10; while response == "yes" and user_health > 0 and troll_health > 0: attack = random.randrange(6) #strength of attack if attack == 0: print "Your attack missed. The troll attacks you." user_health = 0 else: print "Your attack succeeded in wounding the troll." troll_health = troll_health - attack print "Your health level is", user_health print "The troll's health level is", troll_health if user_health > 0 and troll_health > 0: response = raw_input("Do you want to attack? ") if user_health <= 0: print "The troll delivered a devastating attack with his battle-axe." print "Game over!" elif troll_health <= 0: print "You have defeated the troll!" print "You may continue your quest." else: print "You ran away. You live to fight another day!"