A Video club that awards points to its customers based on the number of videos purchased each month. The points are awarded as follows:
This program asks the user to enter the number of videos that he or she has purchased this month, then displays the number of points awarded.
# I begin by prompting the user to enter the number of DVDs purchased
num_dvds = int(input("Enter the number of DVDs purchased this month: "))
# the int() function here is used to ensure the value entered reflects as an integer
# Calculate the number of points earned based on the number of DVDs using the if-else loop
if num_dvds == 0:
points = 0
elif num_dvds == 1:
points = 5
elif num_dvds == 2:
points = 15
elif num_dvds == 3:
points = 30
else:
points = 60
# The program ends with a display of the number of points earned
print("You earned", points, "points this month.")
Enter the number of DVDs purchased this month: 3 You earned 30 points this month.
This program calculates the BMI ( Body Mass Index ) for a user. The program asks the user for needed input:
Then, displays the BMI value on the screen with a message. The message depends on the BMI index, if the person is:
BMI index
Formula: BMI = weight / ( height^2)
# We begin by defining the variables weight and height
weight_kg = float(input("Enter your weight "))
height_m = float(input("Enter your height "))
# We then use the defined variables to calculate the value of BMI Index
bmi = weight_kg / (height_m**2)
# Now we use the if-elif-else statement to display result
if bmi < 18.5:
print("Your BMI is", bmi, "You are underweight")
elif 18.5 <= bmi <= 24.99:
print("Your BMI is", bmi, "You are normal weight")
elif 25.0 <= bmi <= 29.99:
print("Your BMI is", bmi, "You are overweight")
else:
print("Your BMI is", bmi, "You are obese!")
Enter your weight 78 Enter your height 1.65 Your BMI is 28.650137741046834 You are overweight
A county collects property taxes on the assessment value of property, which is 60 percent of the property’s actual value. For example, if an acre of land is valued at USD 10,000, its assessment value is USD 6,000. The property tax is then 72¢ for each USD 100 of the assessment value. The tax for the acre assessed at USD 6,000 will be USD 43.20.
This program that asks for the actual value of a piece of property and displays the assessment value and property tax.
# We begin by assigning the property's actual value as an input value
actual_value = float(input("Enter your property's actual value "))
# We use a float value since the resultant property tax value can be a decimal
# Now we will use the entered actual value to calculate the 60% assessment value
assessment_value = 0.6 * actual_value
# Then, we use simple arithmetic to calculate the dollar amount of property tax to be paid
property_tax = float((assessment_value * 0.72) / 100)
# Lastly, we ask for the assessment value and property tax amounts to be printed
print("Your property's assessment value is", assessment_value)
print("You will pay the value of $", property_tax, "in property tax")
Enter your property's actual value 500000 Your property's assessment value is 300000.0 You will pay the value of $ 2160.0 in property tax
This program uses a while loop, and within the loop asks the user to enter a series of positive numbers. The user can enter a negative number to signal the end of the series. After all the positive numbers have been entered, the program then displays their sum.
# Initialize the sum to 0
total = 0
# Ask the user to enter a series of positive numbers
while True:
num = int(input("Enter a positive number (or a negative number to quit): "))
if num < 0:
break
total += num
# Display the sum of the positive numbers entered
print("The sum of the positive numbers entered is:", total)
Enter a positive number (or a negative number to quit): 7 Enter a positive number (or a negative number to quit): 5 Enter a positive number (or a negative number to quit): 2 Enter a positive number (or a negative number to quit): 4 Enter a positive number (or a negative number to quit): 8 Enter a positive number (or a negative number to quit): 6 Enter a positive number (or a negative number to quit): 2 Enter a positive number (or a negative number to quit): -8 The sum of the positive numbers entered is: 34
This program contains a function named my_max that accepts two integer values as arguments and returns the value that is the greater of the two. For example, if 12 and 19 are passed as arguments to the function, the function returns 19. The function is then used in a program that prompts the user to enter two integer values. The program displays the value that is the greater of the two.
# We begin by defining the my_max function (if-else)
def my_max(num1, num2):
# To check which number is greater:
if num1 > num2:
# If num1 is greater, the program will return num1
return num1
else:
# Otherwise, the program will return num2
return num2
# Now we prompt the user to enter two integers and store them in num1 and num2 as defined
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
# Now we call the my_max function with num1 and num2 as arguments and store the result in max_num
max_num = my_max(num1, num2)
# Print the result to check the final result
print("The greater number is:", max_num)
Enter the first number: 88 Enter the second number: 52 The greater number is: 88
Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Write the following functions in the program:
Score Letter Grade: 90-100 = A; 80-89 = B; 70-79 = C; 60-69 = D; Below 60 = F
# We begin by defining the first function, given here as calc_average
def calc_average(scores):
# Calculate the average of the scores for the full length of scores
average = sum(scores) / len(scores)
return average
# Now we define the second function as determine_grade
def determine_grade(score):
# Determine the letter grade based on the score using the if-elif-else statement
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
# Now we prompt the user to enter five test scores, saved in the empty list scores = []
scores = []
for i in range(5):
score = int(input("Enter your 5 scores one at a time: "))
scores.append(score)
# We then calculate the average of the scores using the calc_average function
average = calc_average(scores)
# Display the letter grade for each score using the determine_grade function, and print the average score
print("Scores:")
for score in scores:
grade = determine_grade(score)
print("For the score of",score, "You got grade - ", grade)
print("Your average score is:", average)
Enter your 5 scores one at a time: 69 Enter your 5 scores one at a time: 88 Enter your 5 scores one at a time: 95 Enter your 5 scores one at a time: 46 Enter your 5 scores one at a time: 57 Scores: For the score of 69 You got grade - D For the score of 88 You got grade - B For the score of 95 You got grade - A For the score of 46 You got grade - F For the score of 57 You got grade - F Your average score is: 71.0
An organisation with multiple departments keeps keys to top secret information for each department at a secure facility protected by armed security. Each morning, a department head needs to be positively identified by security, after which they will be given a unique access code to go and collect whatever secret information is in the security safe. The following program generates a random and unique access code for each department upon request. The access code is completely random, hence, it does change each time even though the safe number remains the same.
Run the program, then enter the exact name of your department (case sensitive) and obtain your safe number and unique access code.
import random
import string
# Define the departments and their corresponding access codes
departments = {
'Accounting': '',
'Internal Audit': '',
'Human Resources': '',
'Operations': '',
'Marketing': '',
'Research and Development': ''
}
# Generate a unique access code for each department
def generate_access_code():
characters = string.ascii_uppercase + string.digits + string.punctuation
return ''.join(random.choice(characters) for _ in range(15))
# Assign a safe number to each department
def assign_safe_numbers():
count = 1
for department in departments:
departments[department] = {
'access_code': generate_access_code(),
'safe_number': count
}
count += 1
# Retrieve and display the access code and safe number for a department
def retrieve_department_info(department_name):
if department_name in departments:
department_info = departments[department_name]
print(f"Access Code: {department_info['access_code']}")
print(f"Safe Number: {department_info['safe_number']}")
else:
print("Department not found.")
# Main program
def main():
assign_safe_numbers()
print("Welcome to the Department Access Program!")
department_name = input("Please enter the department name: ")
retrieve_department_info(department_name)
# Run the program
if __name__ == '__main__':
main()
Welcome to the Department Access Program! Please enter the department name: Accounting Access Code: +[>7*O\,FL;&^|> Safe Number: 1
As a busy guy, I do not have time to sit and haggle for the price of services offered to me. With my knowledge of basic python, I have created a very simple bot which I will be deploying on my website. For the 2 services I need (laundry plus Kids Transport), I have told my bot the threshold at which:
The program begins by asking the user to state which service they are offering, then asks the provider to indicate their price offer. As long as the price being charged has not reached a certain threshold, my bot will keep haggling for a lower price. Obviously, in real life the code making up the program will be hidden from the user.
# Constants
LAUNDRY_MAX_THRESHOLD = 30 # Maximum acceptable threshold for Laundry service
KIDS_TRANSPORT_MAX_THRESHOLD = 100 # Maximum acceptable threshold for Kids transport service
MAX_DOUBLE_THRESHOLD = 2 # Maximum threshold for quitting
# Function for haggling the price
def haggle_price(service_option):
if service_option == 1:
service_name = "Laundry"
max_threshold = LAUNDRY_MAX_THRESHOLD
elif service_option == 2:
service_name = "Kids Transport"
max_threshold = KIDS_TRANSPORT_MAX_THRESHOLD
else:
print("Invalid service option.")
return
print(f"Welcome to the Price Haggling Program for {service_name}!")
print(f"How much do you wish to be paid for rendering the service?")
price = float(input("Enter your price offer: $"))
while price > max_threshold:
print("Can't you go a little lower, just for me?")
price = float(input("Enter a new price offer: $"))
if price > (max_threshold * MAX_DOUBLE_THRESHOLD):
print("You're impossible! This isn't going to work!")
return
print("It's a deal! Pleasure doing business with you!")
# Main program
def main():
print("Welcome to the Service Provider Program!")
print("Please select the service option:")
print("1. Laundry")
print("2. Kids Transport")
service_option = int(input("Enter the service option (1 or 2): "))
haggle_price(service_option)
# Run the program
if __name__ == '__main__':
main()
Welcome to the Service Provider Program! Please select the service option: 1. Laundry 2. Kids Transport Enter the service option (1 or 2): 2 Welcome to the Price Haggling Program for Kids Transport! How much do you wish to be paid for rendering the service? Enter your price offer: $150 Can't you go a little lower, just for me? Enter a new price offer: $105 Can't you go a little lower, just for me? Enter a new price offer: $94 It's a deal! Pleasure doing business with you!
~ End ~