Professional Documents
Culture Documents
SE 100 Assignment 5 by Karam 240419
SE 100 Assignment 5 by Karam 240419
SE 100 Assignment 5 by Karam 240419
Student
___karam__________________________________________________________
Name:
Student ID: _240419____________________________________________________________
Student _______section
Section: 4______________________________________________________
Question 1: Write a function named times_ten. The function should accept an argument and
display the product of its argument multiplied times 10.
def times_ten(number):
result = number * 10
print(f'The product of {number} multiplied by 10 is:
{result}')
# Example usage:
times_ten(5)
Question 2: Examine the following function header, then write a statement that calls the function,
passing 12 as an argument.
def show_value(quantity):
def show_value(quantity):
show_value(12)
When this call executes, what value will be assigned to a? What value will be assigned to b? What
value will be assigned to c?
1 3.4
0 0
1 3.4
d = (a + c) / b
print(d)
a. Write a statement that calls this function and uses keyword arguments to pass 2 into a, 4
into b, and 6 into c.
Question 6: Write a statement that generates a random number in the range of 1 through 100 and
assigns it to a variable named rand.
import random
Page 2 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Page 3 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Question 7: The following statement calls a function named half, which returns a value that is half
that of the argument. (Assume the number variable references a float value.)
def half(number):
return number / 2
number = 10.0
result = half(number)
print(result)
Write a statement that passes the value 4 to this function and assigns its return value to the variable
result.
def cube(num):
return num * num * num
result = cube(4)
Question 9: Write a function named times_ten that accepts a number as an argument. When the
function is called, it should return the value of its argument multiplied times 10.
def times_ten(number):
return number * 10
Page 4 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Question 10: Write a function named get_first_name that asks the user to enter his or her first
name, and returns it.
def get_first_name():
first_name = input("Please enter your first name: ")
return first_name
user_first_name = get_first_name()
print("Your first name is:", user_first_name)
Page 5 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Question 11 - Math Quiz: Write a program that gives simple math quizzes. The program must have
at least 3 functions including main. The program should display two random numbers that are to
be added, such as:
247
+ 129
The program should allow the student to enter the answer. If the answer is correct, a message of
congratulations should be displayed. If the answer is incorrect, a message showing the correct
answer should be displayed.
Write the code here and attach a screenshot of the output using some sample data as an example.
import random
def generate_question():
num1 = random.randint(1, 1000)
num2 = random.randint(1, 1000)
return num1, num2
def main():
num1, num2 = generate_question()
user_answer = get_user_answer(num1, num2)
if __name__ == "__main__":
main()
Page 6 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Page 7 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Question 12 - Falling Distance: When an object is falling because of gravity, the following formula
can be used to determine the distance the object falls in a specific time period:
1
𝑑 = 𝑔𝑡 2
2
The variables in the formula are as follows: d is the distance in meters, g is 9.8, and t is the amount
of time, in seconds, that the object has been falling.
Write a function named falling_distance that accepts an object’s falling time (in seconds) as
an argument. The function should return the distance, in meters, that the object has fallen during
that time interval. Write a program that calls the function in a loop that passes the values 1 through
10 as arguments and displays the return value.
def falling_distance(time):
g = 9.8 # Acceleration due to gravity in m/s^2
distance = 0.5 * g * time ** 2
return distance
def main():
for time in range(1, 11):
distance = falling_distance(time)
print(f"Time: {time} seconds - Distance: {distance:.2f} meters")
if __name__ == "__main__":
main()
Page 8 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Question 13 - Odd/Even Counter: Write a program that generates 100 random numbers and keeps a
count of how many of those random numbers are even, and how many of them are odd. The
program must have at least 2 functions including main.
import random
def generate_random_numbers(count):
return random_numbers
def count_even_odd(numbers):
even_count = 0
odd_count = 0
if number % 2 == 0:
even_count += 1
else:
odd_count += 1
def main():
random_numbers = generate_random_numbers(100)
print(random_numbers)
if __name__ == "__main__":
main()
Page 9 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Page 10 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Question 14 - Prime Numbers: A prime number is a number that is only evenly divisible by itself and
1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number
6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6.
Write a Boolean function named is_prime which takes an integer as an argument and returns
true if the argument is a prime number, or false otherwise. Use the function in a program that
prompts the user to enter a number then displays a message indicating whether the number is
prime.
Write the code here and attach a screenshot of the output using some sample data as an example.
def is_prime(number):
if number <= 1:
return False
if number <= 3:
return True
if number % 2 == 0 or number % 3 == 0:
return False
i = 5
while i * i <= number:
if number % i == 0 or number % (i + 2) == 0:
return False
i += 6
return True
def main():
user_input = int(input("Enter a number: "))
if is_prime(user_input):
print(f"{user_input} is a prime number.")
else:
print(f"{user_input} is not a prime number.")
if __name__ == "__main__":
main()
Page 11 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Page 12 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Question 15 - Prime Number List: After you finish question 14, write another program that displays
all prime numbers from 1 to 100. The program should have a loop that calls the is_prime
function.
if number % 2 == 0 or number % 3 == 0:
return False
i = 5
while i * i <= number:
if number % i == 0 or number % (i + 2) == 0:
return False
i += 6
return True
def display_prime_numbers():
print("Prime numbers from 1 to 100:")
for num in range(1, 101):
if is_prime(num):
print(num, end=" ")
if __name__ == "__main__":
display_prime_numbers()
Page 13 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Page 14 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Question 16 - Random Number Guessing Game: Write a program that generates a random number
in the range of 1 through 100, and asks the user to guess what the number is. If the user’s guess is
higher than the random number, the program should display “Too high, try again.” If the user’s
guess is lower than the random number, the program should display “Too low, try again.” If the user
guesses the number, the application should congratulate the user and generate a new random
number so the game can start over. The program must have at least 2 functions including main.
Write the code here and attach a screenshot of the output using some sample data as an example.
import random
def generate_random_number():
return random.randint(1, 100)
def main():
print("Welcome to the Random Number Guessing Game!")
play_again = "yes"
while True:
try:
user_guess = int(input("Guess the random number (1-100): "))
attempts += 1
if __name__ == "__main__":
main()
Page 15 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Page 16 of 18
SE 100 – Assignment 5 – Fall 2023-2024
Question 17 - Rock, Paper, Scissors Game: Write a program that lets the user play the game of Rock,
Paper, Scissors against the computer. The program should work as follows:
1. When the program begins, a random number in the range of 1 through 3 is generated. If the
number is 1, then the computer has chosen rock. If the number is 2, then the computer has
chosen paper. If the number is 3, then the computer has chosen scissors. (Don’t display the
computer’s choice yet.)
2. The user enters his or her choice of “rock,” “paper,” or “scissors” at the keyboard.
3. The computer’s choice is displayed.
4. A winner is selected according to the following rules:
• If one player chooses rock and the other player chooses scissors, then rock wins.
(Rock smashes scissors.)
• If one player chooses scissors and the other player chooses paper, then scissors wins.
(Scissors cuts paper.)
• If one player chooses paper and the other player chooses rock, then paper wins.
(Paper wraps rock.)
• If both players make the same choice, the game must be played again to determine the
winner.
Write the code here and attach a screenshot of the output using some sample data as an example.
import random
def get_computer_choice():
return random.choice(["rock", "paper", "scissors"])
def main():
print("Welcome to the Rock, Paper, Scissors game!")
while True:
user_choice = input("Enter your choice (rock, paper,
scissors): ").lower()
Page 17 of 18
SE 100 – Assignment 5 – Fall 2023-2024
computer_choice = get_computer_choice()
print(f"Computer's choice: {computer_choice}")
if __name__ == "__main__":
main()
Page 18 of 18