Here are five programming assignments that focus on using functions in Python:
### Assignment 1: Temperature Conversion
**Description**: Write a program with functions to convert temperatures between Celsius, Fahrenheit, and Kelvin.
**Requirements**:
1. Create three functions: `celsius_to_fahrenheit(celsius)`, `fahrenheit_to_celsius(fahrenheit)`, and `celsius_to_kelvin(celsius)`.
2. Each function should take a temperature as an argument and return the converted temperature.
3. The program should prompt the user to enter a temperature and the conversion they want to perform, then call the appropriate function and display the result.
### Assignment 2: Factorial Calculation
**Description**: Write a program that calculates the factorial of a number using a function.
**Requirements**:
1. Create a function `factorial(n)` that takes an integer `n` and returns its factorial.
2. The program should prompt the user to enter a non-negative integer.
3. Call the `factorial` function and display the result.
### Assignment 3: Prime Number Checker
**Description**: Write a program with a function to check if a given number is a prime number.
**Requirements**:
1. Create a function `is_prime(n)` that takes an integer `n` and returns `True` if the number is prime and `False` otherwise.
2. The program should prompt the user to enter a number.
3. Call the `is_prime` function and display whether the number is prime or not.
### Assignment 4: List Sorting
**Description**: Write a program with a function to sort a list of numbers in ascending order.
**Requirements**:
1. Create a function `sort_list(numbers)` that takes a list of numbers and returns a sorted list.
2. The program should prompt the user to enter a list of numbers (you can assume the user will enter numbers separated by spaces).
3. Call the `sort_list` function and display the sorted list.
### Assignment 5: String Palindrome Checker
**Description**: Write a program with a function to check if a given string is a palindrome.
**Requirements**:
1. Create a function `is_palindrome(s)` that takes a string `s` and returns `True` if the string is a palindrome and `False` otherwise.
2. The program should prompt the user to enter a string.
3. Call the `is_palindrome` function and display whether the string is a palindrome or not.
### Example Implementation
Below are some hints and brief examples of how you might structure these functions:
#### Example 1: Temperature Conversion
```python
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
def celsius_to_kelvin(celsius):
return celsius + 273.15
# Example usage:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius}C is {fahrenheit}F")
```
#### Example 2: Factorial Calculation
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
# Example usage:
number = int(input("Enter a non-negative integer: "))
result = factorial(number)
print(f"Factorial of {number} is {result}")
```
#### Example 3: Prime Number Checker
```python
def is_prime(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
# Example usage:
number = int(input("Enter a number: "))
if is_prime(number):
print(f"{number} is a prime number")
else:
print(f"{number} is not a prime number")
```
#### Example 4: List Sorting
```python
def sort_list(numbers):
return sorted(numbers)
# Example usage:
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
sorted_numbers = sort_list(numbers)
print("Sorted list:", sorted_numbers)
```
#### Example 5: String Palindrome Checker
```python
def is_palindrome(s):
return s == s[::-1]
# Example usage:
string = input("Enter a string: ")
if is_palindrome(string):
print(f"{string} is a palindrome")
else:
print(f"{string} is not a palindrome")
```
0 Comments