Instructions
Requirements and Specifications
Source Code
EXERCISE 4
import random
def rpg():
print('***Welcome to the Greatest RPG Ever!***')
knight_hp = 10
orc_hp = 10
print('Your HP =', knight_hp)
print('The Orc\'s HP =', orc_hp)
run = False
while knight_hp > 0 and orc_hp > 0 and not run:
print()
choice = input('Do you Fight (F) or Run (R)? ').strip().lower()
if choice == 'f':
knight_attack = random.randrange(1, 7)
orc_attack = random.randrange(2, 5)
knight_hp -= orc_attack
orc_hp -= knight_attack
if knight_hp <= 0:
if orc_hp <= 0:
print('Tie! Everyone loses!')
else:
print('Failure! You lose!')
else:
if orc_hp <= 0:
print('Victorious! You Win!')
elif choice == 'r':
print('Chicken!')
run = True
else:
print('Error: Invalid Input!')
print('Your HP =', knight_hp)
print('The Orc\'s HP =', orc_hp)
print('***Do you dare to play again?***')
rpg()
LAB ASSIGNMENT
"""
@author: [your name]
@collaborators: [a list of students you collaborated with on the assignment, if any]
"""
ids = [4353, 2314, 2956, 3382, 9362, 3900]
print(ids)
print('The length of the \'ids\' =', len(ids))
ids_copy = ids.copy()
ids.remove(3382)
i = ids.index(9362)
print('The index of value 9362 =', i)
ids.insert(i+1, 4499)
ids.append(5566)
ids.append(1830)
ids.sort()
print(ids)
print('The length of the \'ids\' =', len(ids))
print(ids_copy)
print("Problem [1] Complete")
print('Index Value In \"ids_copy\"?')
for j in range(len(ids)):
elem = ids[j]
print(j, ' ', elem, ' ', elem in ids_copy)
print()
i = 0
print('Index Value In \"ids_copy\"?')
while i < len(ids):
elem = ids[i]
print(i, ' ', elem, ' ', elem in ids_copy)
i += 1
print("Problem [2] Complete")
print()
def mean(arr):
if len(arr) == 0:
return None
total = 0
for i in arr:
total += i
return total / len(arr)
print(mean([7,5,3,4,6,8,9,7,6,4,2,4,5,6,8,6,4,5,6,8,1,2]))
print(mean([7,5,3,4,6,8,9,7,6,4,2,4,5,6,8,6,4,5,6,8,1,2,1,1,1,1,1]))
print(mean([]))
print("Problem [3] Complete")
print()
def median(arr):
n = len(arr)
if n == 0:
return None
arr_copy = arr.copy()
arr_copy.sort()
if n % 2 == 1:
return arr_copy[n // 2]
else:
return (arr_copy[n // 2] + arr_copy[(n // 2) - 1]) / 2
data = [7,5,3,4,6,8,9,7,6,4,2,4,5,6,8,6,4,5,6,8,1,2]
print(median(data))
data.append(1)
print(median(data))
print(data)
print(median([]))
print("Problem [4] Complete")
print()
def mode(arr):
n = len(arr)
if n == 0:
return None
mode = -1
mode_times = 0
for i in arr:
curr_count = arr.count(i)
if curr_count > mode_times:
mode_times = curr_count
mode = i
return mode
print(mode([7,5,3,4,6,8,9,7,6,4,2,4,5,6,8,6,4,5,6,8,1,2]))
print(mode([7,5,3,4,6,8,9,7,6,4,2,4,5,6,8,6,4,5,6,8,1,2,1,1,1,1,1]))
print(mode([]))
print("Problem [5] Complete")
print()
def getStats(arr):
if len(arr) == 0:
print('ERROR: The input list is empty...cannot calculate statistics...')
return
arr_copy = arr.copy()
arr_copy.sort(reverse=True)
print('The collected data in the descending order is', arr_copy)
print('The maximum of the collected data is', arr_copy[0])
print('The minimum of the collected data is', arr_copy[-1])
print('The mean of the collected data is', mean(arr_copy))
print('The median of the collected data is', median(arr_copy))
print('The mode of the collected data is', mode(arr_copy))
getStats([])
populations = [1000, 2000, 4000, 6000, 3000, 2000, 6000, 5000, 10000, 2000]
getStats(populations)
print(populations)
print("Problem [6] Complete")
print()
def getData():
data = []
while True:
value = int(input('Enter a number (-1 to quit): '))
if value < 0:
if value == -1:
break
else:
print('ERROR: Please enter a value greater than or equal to zero...')
else:
data.append(value)
return data
data = getData()
print(data)
getStats(data)
print()
def acronymGenerator():
s = input('Please enter a phrase: ')
words = s.split()
acronym = ''
for word in words:
if word[0].isalpha() and word[0].isupper():
acronym += word[0]
print('Acronym for \"' + s + '\" is:', acronym)
acronymGenerator()