This program interacts with the user to collect their name and favorite numbers, then performs various operations on these numbers. Below are the steps performed in the code:
The code begins by prompting the user for their name and three favorite numbers:
name = input("Enter your name: ")
first_number = int(input("Enter your first favorite number: "))
second_number = int(input("Enter your second favorite number: "))
third_number = int(input("Enter your third favorite number: "))
- Input:
- The user's name is stored in the variable
name
. - Three favorite numbers are stored as integers in the variables
first_number
,second_number
, andthird_number
.
- The user's name is stored in the variable
The favorite numbers are then stored in a list for easier manipulation:
numbers = [first_number, second_number, third_number]
- Purpose: Grouping the three favorite numbers into a list called
numbers
.
A new list status_list
is created to store tuples of the favorite numbers and their even/odd status:
status_list = []
The code loops through the list of numbers to check if each one is even or odd, and appends the results as tuples to status_list
:
for number in numbers:
if number % 2 == 0:
status_list.append((number, "even"))
else:
status_list.append((number, "odd"))
- Logic:
- If the number is divisible by 2, it is considered "even".
- If not, the number is classified as "odd".
A greeting message is displayed using the user's name, followed by an introduction to their favorite numbers:
print(f"\nHello, {name}! Let's explore your favorite numbers:")
The program prints out the even/odd status of each favorite number:
for number, status in status_list:
print(f"The number {number} is {status}.")
The code calculates and prints each number along with its square using a loop:
for number in numbers:
print(f"The number {number} and its square: ({number}, {number ** 2})")
- Explanation: For each number in the list, its square is calculated using the
**
operator and printed in a tuple format.
The sum of the user's favorite numbers is calculated using Python's built-in sum()
function:
total_sum = sum(numbers)
print(f"\nAmazing! The sum of your favorite numbers is: {total_sum}")
The program checks whether the sum of the numbers is a prime number. A prime number is only divisible by 1 and itself:
is_prime = True
if total_sum < 2:
is_prime = False
else:
for i in range(2, int(total_sum ** 0.5) + 1):
if total_sum % i == 0:
is_prime = False
break
- Logic:
- Numbers less than 2 are automatically considered non-prime.
- For numbers greater than or equal to 2, the program checks if any integer from 2 to the square root of the sum divides evenly into the total. If so, the sum is not prime.
Finally, the program informs the user whether the sum of their favorite numbers is a prime number:
if is_prime:
print(f"Wow, {total_sum} is a prime number!")
else:
print(f"{total_sum} is not a prime number.")
This is a simple yet effective program that interacts with the user and performs mathematical checks on their input, including checking the parity (even/odd) and primality of the sum of their favorite numbers.