Advertisement
Python253

new_fibtest

Mar 7th, 2024 (edited)
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: new_fibtest.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. 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).
  8.  
  9. Or...
  10.  
  11. ('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.')
  12.  
  13. Requirements:
  14. - Python 3.x
  15.  
  16. Usage:
  17. 1. Import the module in your Python script.
  18. 2. Use the 'fib' function to print the Fibonacci series up to a given limit of 'n'.
  19. 3. Use the 'fib2' function to obtain the Fibonacci series as a list.
  20. 4. Save to 'fiblist.txt' file or exit program.
  21. """
  22.  
  23. import string
  24.  
  25. # Fibonacci numbers module
  26. def fib(n):
  27.     """Print Fibonacci series up to n."""
  28.     a, b = 0, 1
  29.     while b < n:
  30.         print(b, end=' ')
  31.         a, b = b, a + b
  32.  
  33. def fib2(n):
  34.     """Return Fibonacci series up to n as a list."""
  35.     result = []
  36.     a, b = 0, 1
  37.     while b < n:
  38.         result.append(b)
  39.         a, b = b, a + b
  40.     return result
  41.  
  42. # Example usage:
  43. if __name__ == "__main__":
  44.     user_input = input("\nEnter the limit for Fibonacci series (MAX: 9,999,999,999,999,999,999,999,999,999,999): ")
  45.  
  46.     # Remove punctuation from user input
  47.     user_input = user_input.translate(str.maketrans("", "", string.punctuation))
  48.  
  49.     n = int(user_input)
  50.     fib_list = fib2(n)
  51.     print(fib_list)
  52.     print()
  53.  
  54.     # Save Option
  55.     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")
  56.     if save_option == '1':
  57.         with open('fiblist.txt', 'w') as file:
  58.             for number in fib_list:
  59.                 file.write(str(number) + '\n')
  60.         print("\nFibonacci series saved to fiblist.txt.\n")
  61.     elif save_option == '2':
  62.         print("\nProgram exited without saving.\n")
  63.     else:
  64.         print("\nInvalid option. Program exited.\n")
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement