diff --git a/conditions.py b/conditions.py index 8ff3d8e..1571c9d 100644 --- a/conditions.py +++ b/conditions.py @@ -1,21 +1,16 @@ -# 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. -# - 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." - - - -age = int(input("Enter your age: ")) - - -if age >= 18: - print("You are eligible to vote.") -else: - print("You are not eligible to vote.") +#Code to check wheteher a user is eligible to vote or not. +while True: + age = int(input("Enter your age or 0 to exit: ")) + name = input("Enter your name: ") + if age == 0: + break + elif age >= 18: + print(f"\nDear {name} You are eligible to vote.\nPlease vote wisely.\n") + else: + print("\nSorry {} You are not eligible to vote.".format(name)) + print("You can vote after {} years.".format(18 - age)) + quit = input("Do you want to continue? (y/n): ") + if quit == 'y' or quit == 'Y': + break + else: + continue \ No newline at end of file diff --git a/functions.py b/functions.py index c80ee3c..de17c2f 100644 --- a/functions.py +++ b/functions.py @@ -1,32 +1,8 @@ -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. - """ - fibonacci_sequence = [] - if n <= 0: - return fibonacci_sequence - elif n == 1: - fibonacci_sequence.append(0) - else: - fibonacci_sequence.extend([0, 1]) # If n is greater than 1, add the first two terms (0 and 1) to the sequence - a, b = 0, 1 - for _ in range(2, n): - c = a + b - fibonacci_sequence.append(c) - a, b = b, c - return fibonacci_sequence - -# Get the number of terms from the user -num_terms = int(input("Enter the number of terms: ")) - -# Generate the Fibonacci sequence -fibonacci_sequence = fibonacci(num_terms) - -# Print the Fibonacci sequence -print(fibonacci_sequence) +#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. +def fibonacci_generator(n): + a, b = 0, 1 + for i in range(n): + yield a + a, b = b, a + b +n = int(input("Enter the number of terms: ")) +print(f"\n{list(fibonacci_generator(n))}+\n") diff --git a/image-1.png b/image-1.png new file mode 100644 index 0000000..7a86edd Binary files /dev/null and b/image-1.png differ diff --git a/image.png b/image.png new file mode 100644 index 0000000..c792c14 Binary files /dev/null and b/image.png differ diff --git a/readme.md b/readme.md index c6d05f0..701cf11 100644 --- a/readme.md +++ b/readme.md @@ -2,18 +2,24 @@ 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. -#### Your program should: +#### 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. +##### these instructions followed strictly and this is the ouput from the code +![alt text](image.png) ## 2. Python Conditional Statements +- I used while statement to iterate through the code untill the user chooses to exit the program + -Example code is [here](https://plpacademy.powerlearnproject.org/course-module/62fbec9d28ac4762bc524f92/week/62fe1efd28ac4762bc524f9c/lesson/62fe1fbd28ac4762bc524f9f) #### 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` + +![alt text](image-1.png) +- This screenshot demonstrate the functionality of the source code \ No newline at end of file