Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions conditions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# Python Conditional Statements
#example is https://plpacademy.powerlearnproject.org/course-module/62fbec9d28ac4762bc524f92/week/62fe1efd28ac4762bc524f9c/lesson/62fe1fbd28ac4762bc524f9f
# Prompt the user to enter their age
while True:
age_input = input("Enter your age: ")
try:
age = int(age_input)
if age < 0:
print("Age cannot be negative. Please enter a valid age.")
else:
break
except ValueError:
print("Invalid input. Please enter a valid age.")



# Create a Python program that:


# - Prompts a user to enter their age.
# - Uses a conditional statement to check if the age is greater than or equal to 18.
# - Prints "You are eligible to vote" if true, otherwise "You are not eligible to vote."
# Check if the age is greater than or equal to 18
if age >= 18:
print("You are eligible to vote")
else:
print("You are not eligible to vote")
-2
80 changes: 41 additions & 39 deletions functions.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
# Functions & Fibonacci Sequence
# Question
# Write a Python program to generate the Fibonacci sequence up to a specified term n. The Fibonacci sequence starts with 0 and 1, and each subsequent term is the sum of the two preceding terms.
#We have provided you with in-complete code, from the Knowledge learned from week 1 to week 3 please complete the missing parts to achieve the goal of the question.
def fibonacci(n):
"""
This function generates the Fibonacci sequence up to a specified term n using iteration.

Args:
n: The number of terms in the Fibonacci sequence.

Returns:
A list containing the Fibonacci sequence up to n terms.
"""
if n <= 1:
# Complete here
else:
a, b = # complete here
for _ in range(2, n + 1):
c = a + b
# Complete here
return # add the variable to be returned

# Get the number of terms from the user
num_terms = int(input("Enter the number of terms: "))

# Generate the Fibonacci sequence
fibonacci_sequence = []
for i in range(num_terms):
fibonacci_sequence.append(fibonacci(i))
def generate_fibonacci(n):
"""
Generate the Fibonacci sequence up to a specified term n using iteration.

Args:
n: The number of terms in the Fibonacci sequence.

Returns:
A list containing the Fibonacci sequence up to n terms.
"""
fibonacci_sequence = [] # Initialize an empty list to store the Fibonacci sequence

if n <= 0:
return fibonacci_sequence # Return an empty sequence if n is non-positive
elif n == 1:
fibonacci_sequence = [0] # If n is 1, Fibonacci sequence is [0]
else:
a, b = 0, 1 # Initialize the first two terms of the Fibonacci sequence
fibonacci_sequence = [a, b] # Start the sequence with the initial terms

for _ in range(2, n):
c = a + b # Calculate the next Fibonacci number
fibonacci_sequence.append(c) # Append the next Fibonacci number to the sequence
a, b = b, c # Update the values of a and b for the next iteration

return fibonacci_sequence


# Get the number of terms from the user with error checking
while True:
try:
num_terms = int(input("Enter the number of terms (a positive integer): "))
if num_terms <= 0:
raise ValueError("Please enter a positive integer.")
break # Exit the loop if input is valid
except ValueError as ve:
print(ve)

# Generate the Fibonacci sequence using the iterative approach
fibonacci_sequence = generate_fibonacci(num_terms)

# Print the Fibonacci sequence
print(fibonacci_sequence)


# Your program should:

# Ask the user to input the value of n.
# Create a function that takes n as a parameter and returns a list containing the first n terms of the Fibonacci sequence.
# Print the generated Fibonacci sequence.

print("Fibonacci sequence:", fibonacci_sequence)