From 9eeaf02c6aa99fa64401cc353c205bcb306f83a8 Mon Sep 17 00:00:00 2001 From: wanjikustl19 Date: Tue, 19 Mar 2024 11:46:46 +0300 Subject: [PATCH] Completed tasks: [the python Hackathon] --- conditions.py | 16 ++++++++----- functions.py | 64 ++++++++++++++++++--------------------------------- 2 files changed, 33 insertions(+), 47 deletions(-) diff --git a/conditions.py b/conditions.py index 1499e63..18fccff 100644 --- a/conditions.py +++ b/conditions.py @@ -1,11 +1,15 @@ # Python Conditional Statements #example is https://plpacademy.powerlearnproject.org/course-module/62fbec9d28ac4762bc524f92/week/62fe1efd28ac4762bc524f9c/lesson/62fe1fbd28ac4762bc524f9f - - - # Create a Python program that: - - # - Prompts a user to enter their age. +def main(): + age = int(input("Enter your 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." +# - Prints "You are eligible to vote" if true, otherwise "You are not eligible to vote. + if age >= 18: + print("You are eligible to vote") + else: + print("You are not eligible to vote") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/functions.py b/functions.py index 0d458e4..99c7826 100644 --- a/functions.py +++ b/functions.py @@ -1,41 +1,23 @@ -# 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)) - -# 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. - +# Input from the user +n = int(input("Enter the value of n: ")) +#Fibonacci Sequence Generation Function +def generate_fibonacci_sequence(n): + # Initialize the sequence with the first two terms + fibonacci_sequence = [0, 1] + + # Generate subsequent in terms of the Fibonacci sequence + for i in range(2, n): + next_term = fibonacci_sequence[-1] + fibonacci_sequence[-2] + fibonacci_sequence.append(next_term) + + return fibonacci_sequence +# Main Function: +def main(): + # Ask the user to input the value of n + n = int(input("Enter the value of n: ")) + + # Generate the Fibonacci sequence + fibonacci_sequence = generate_fibonacci_sequence(n) + + # Print the generated Fibonacci sequence + print("Generated Fibonacci sequence up to term", n, ":", fibonacci_sequence) \ No newline at end of file