Uploaded by Ali Zuberi

L07.docx

advertisement
from random import randint
TAX = 3.625
OVER = 1.5
OVERH = 40
sh is
ar stu
ed d
v i y re
aC s
o
ou urc
rs e
eH w
er as
o.
co
m
def hi_lo_game(high):
"""
------------------------------------------------------Plays a random higher-lower guessing game.
Use: count = hi_lo_game(high)
------------------------------------------------------Parameters:
high - maximum random value (int > 1)
Returns:
count - the number of guesses the user made (int)
------------------------------------------------------"""
count = 1
number = randint(1, high)
guess = int(input("Guess: "))
while(guess != number):
if(guess > number):
print("Too high, try again.")
if(guess < number):
print("Too low, try again.")
count += 1
guess = int(input("Guess: "))
if(guess == number):
print("Congratulations - good guess!")
return count
Th
def power_of_two(target):
"""
------------------------------------------------------Determines the nearest power of 2 to a given target.
Use: power = power_of_two(target)
------------------------------------------------------Parameters:
target - value to find nearest power of 2 (int >= 0)
Returns:
power - first power of 2 >= target (int)
------------------------------------------------------"""
n=2
while(n < target):
This study source was downloaded by 100000831992239 from CourseHero.com on 11-05-2021 13:42:36 GMT -05:00
https://www.coursehero.com/file/57993440/L07docx/
n = n ** 2
return n
sh is
ar stu
ed d
v i y re
aC s
o
ou urc
rs e
eH w
er as
o.
co
m
def population_growth(target, current, rate):
"""
------------------------------------------------------Determines the number of years to reach a target population.
Use: years = population_growth(target, current, rate)
------------------------------------------------------Parameters:
target - target population (int >= 0)
current - current population (int > 0)
rate - percent rate of growth (float)
Returns:
years - the number of years to reach target population (int)
------------------------------------------------------"""
years = 0
while(current < target):
current = current + (current * (rate / 100))
years += 1
return years
Th
def sum_squares(target):
"""
------------------------------------------------------Determines the sum of squares closest to, and greater than or
equal to, a target value.
Use: final = sum_squares(target)
------------------------------------------------------Parameters:
target - target value (int >= 0)
Returns:
final - the final sum of squares >= target (int)
------------------------------------------------------"""
n=0
count = 1
while(n <= target):
n = n + (count ** 2)
count += 1
return n
def positive_statistics():
This study source was downloaded by 100000831992239 from CourseHero.com on 11-05-2021 13:42:36 GMT -05:00
https://www.coursehero.com/file/57993440/L07docx/
sh is
ar stu
ed d
v i y re
aC s
o
ou urc
rs e
eH w
er as
o.
co
m
"""
------------------------------------------------------Asks a user to enter a series of positive numbers, then calculates
and returns the minimum, maximum, total, and average of those numbers.
Stop processing values when the user enters a negative number.
The first number entered must be positive.
Use: minimum, maximum, total, average = positive_statistics()
------------------------------------------------------Returns:
minimum - smallest of the entered values (float)
maximum - largest of the entered values (float)
total - total of the entered values (float)
average - average of the entered values (float)
-----------------------------------------------------"""
count = 1
num = float(input("First positive value: "))
while(num < 0):
num = float(input("First positive value: "))
minimum = num
maximum = num
total = num
num2 = float(input("Next positive value: "))
while(num2 > 0):
if(num2 < minimum):
minimum = num2
if(num2 > minimum):
maximum = num2
count += 1
total = total + num2
num2 = float(input("Next positive value: "))
average = total / count
return minimum, maximum, total, average
Th
def num_categories():
"""
------------------------------------------------------Asks a user to enter a series of numbers, then counts and returns
how may positives, negatives, and zeroes there are.
Stop processing values when the user enters -999.
Use: negatives, zeroes, positives = num_categories()
------------------------------------------------------Returns:
negatives - number of negative values (int)
zeroes - number of zero values (int)
positives - number of positive values (int)
------------------------------------------------------
This study source was downloaded by 100000831992239 from CourseHero.com on 11-05-2021 13:42:36 GMT -05:00
https://www.coursehero.com/file/57993440/L07docx/
sh is
ar stu
ed d
v i y re
aC s
o
ou urc
rs e
eH w
er as
o.
co
m
"""
negatives = 0
zeroes = 0
positives = 0
num = int(input("First Value: "))
while(num != -999):
if(num < 0):
negatives += 1
elif(num > 0):
positives += 1
else:
zeroes += 1
num = int(input("Next Value: "))
return negatives, zeroes, positives
Th
def meal_costs():
"""
------------------------------------------------------Asks a user the costs of breakfast, lunch, and supper for each
day the user was away. Assumes there is at least one day, and
after entering data for each day asks the user whether they want
to enter data for another day. Calculates total costs for meals.
Use: b_total, l_total, s_total, a_total = meal_costs()
------------------------------------------------------Returns:
b_total - total breakfasts cost (float)
l_total - total lunches cost (float)
s_total - total suppers cost (float)
a_total - all meals cost (float)
-----------------------------------------------------"""
count = 1
b_total = 0
l_total = 0
s_total = 0
t_total = 0
print("Day {}".format(count))
b = float(input("How much was breakfast? "))
l = float(input("How much was lunch? "))
s = float(input("How much was supper? "))
t=b+l+s
t_total += t
b_total += b
l_total += l
s_total += s
print("Your total for the day was: {}".format(t))
day = str(input("Were you away another day (Y/N)? "))
This study source was downloaded by 100000831992239 from CourseHero.com on 11-05-2021 13:42:36 GMT -05:00
https://www.coursehero.com/file/57993440/L07docx/
sh is
ar stu
ed d
v i y re
aC s
o
ou urc
rs e
eH w
er as
o.
co
m
while(day == 'Y'):
count += 1
print("Day {}".format(count))
b = float(input("How much was breakfast? "))
l = float(input("How much was lunch? "))
s = float(input("How much was supper? "))
t=b+l+s
t_total += t
b_total += b
l_total += l
s_total += s
print("Your total for the day was: {}".format(t))
day = str(input("Were you away another day (Y/N)? "))
return b_total, l_total, s_total, t_total
Th
def budget(available):
"""
------------------------------------------------------Asks a user for a series of expenses in a month. Calculate the
total expenses and determines whether the user is in "Surplus",
"Deficit", or "Balanced"
Use: expenses, balance, status = budget(available)
------------------------------------------------------Parameters:
target - target value (int >= 0)
Returns:
expenses - total monthly expenses (float)
balance - remaining balance (float)
status - One of (str):
"Surplus" if user budget is in surplus
"Deficit" if user budget is in deficit
"Balanced" if user budget is balanced
-----------------------------------------------------"""
total = 0
exp = float(input("Enter an expense (0 to quit): "))
total += exp
while(exp != 0):
exp = float(input("Enter another expense (0 to quit): "))
total += exp
net = available - total
status = ""
if(net < 0):
status = "Deficit"
elif(net > 0):
status = "Surplus"
This study source was downloaded by 100000831992239 from CourseHero.com on 11-05-2021 13:42:36 GMT -05:00
https://www.coursehero.com/file/57993440/L07docx/
else:
status = "Balanced"
return total, net, status
sh is
ar stu
ed d
v i y re
aC s
o
ou urc
rs e
eH w
er as
o.
co
m
def get_int(low, high):
"""
------------------------------------------------------Asks a user for an integer value between low and high, and
continues asking until an acceptable value is input.
Use: value = get_int(low, high)
------------------------------------------------------Parameters:
low - the lowest acceptable integer (inclusive) (int)
high - the higest acceptable integer (inclusive) (int > low)
Returns:
value - a number between low and high (int)
-----------------------------------------------------"""
value = int(input("Enter a value between {} and {}: ".format(low, high)))
while(value > high or value < low):
if(value > high):
print("Value entered is too high")
if(value < low):
print("Value entered is too low")
value = int(input("Enter a value between {} and {}: ".format(low, high)))
return value
Th
def employee_payroll():
"""
------------------------------------------------------Calculates and returns the weekly employee payroll for all employees
in an organization. For each employee, ask the user for the employee ID
number, the hourly wage rate, and the number of hours worked during a week.
An employee number of zero indicates the end of user input.
Each employee is paid 1.5 times their regular hourly rate for all hours
over 40. A tax amount of 3.625 percent of gross salary is deducted.
Use: total, average = employee_payroll()
------------------------------------------------------Returns:
total - total net employee wages (i.e. after taxes) (float)
average - average employee net wages (float)
-----------------------------------------------------"""
count = 0
num = 0
total = 0
This study source was downloaded by 100000831992239 from CourseHero.com on 11-05-2021 13:42:36 GMT -05:00
https://www.coursehero.com/file/57993440/L07docx/
Th
sh is
ar stu
ed d
v i y re
aC s
o
ou urc
rs e
eH w
er as
o.
co
m
ID = int(input("Employee ID: "))
while(ID != 0):
rate = int(input("Hourly wage rate: "))
hours = int(input("Hours worked: "))
if(hours > OVERH):
num = ((OVERH * rate) + ((hours - OVERH) * (rate * OVER))) * (1 - (TAX / 100))
else:
num = (hours * rate) * (1 - (TAX / 100))
print("Net payment for employee {}: ${:.2f}".format(ID, num))
count += 1
total = total + num
ID = int(input("Employee ID: "))
average = total / count
return total, average
This study source was downloaded by 100000831992239 from CourseHero.com on 11-05-2021 13:42:36 GMT -05:00
https://www.coursehero.com/file/57993440/L07docx/
Powered by TCPDF (www.tcpdf.org)
Download