Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: new_fibtest.py
- # Author: Jeoi Reqi
- """
- Module to generate and display Fibonacci series up to a specified limit. Eg; n = 9,999,999,999,999,999,999,999,999,999,999 (MAX VALUE).
- Or...
- ('nine nonillion nine hundred ninety-nine octillion nine hundred ninety-nine septillion nine hundred ninety-nine sextillion nine hundred ninety-nine quintillion nine hundred ninety-nine quadrillion nine hundred ninety-nine trillion nine hundred ninety-nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine.')
- Requirements:
- - Python 3.x
- Usage:
- 1. Import the module in your Python script.
- 2. Use the 'fib' function to print the Fibonacci series up to a given limit of 'n'.
- 3. Use the 'fib2' function to obtain the Fibonacci series as a list.
- 4. Save to 'fiblist.txt' file or exit program.
- """
- import string
- # Fibonacci numbers module
- def fib(n):
- """Print Fibonacci series up to n."""
- a, b = 0, 1
- while b < n:
- print(b, end=' ')
- a, b = b, a + b
- def fib2(n):
- """Return Fibonacci series up to n as a list."""
- result = []
- a, b = 0, 1
- while b < n:
- result.append(b)
- a, b = b, a + b
- return result
- # Example usage:
- if __name__ == "__main__":
- user_input = input("\nEnter the limit for Fibonacci series (MAX: 9,999,999,999,999,999,999,999,999,999,999): ")
- # Remove punctuation from user input
- user_input = user_input.translate(str.maketrans("", "", string.punctuation))
- n = int(user_input)
- fib_list = fib2(n)
- print(fib_list)
- print()
- # Save Option
- save_option = input("Do you want to [SAVE] the Fibonacci series to 'fiblist.txt' or [EXIT] the program?\n1: SAVE\n2: EXIT\nEnter your selection (1 Or 2)\n")
- if save_option == '1':
- with open('fiblist.txt', 'w') as file:
- for number in fib_list:
- file.write(str(number) + '\n')
- print("\nFibonacci series saved to fiblist.txt.\n")
- elif save_option == '2':
- print("\nProgram exited without saving.\n")
- else:
- print("\nInvalid option. Program exited.\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement