The Fibonacci Sequence:

A Fibonacci sequence is the integer sequence of 0,1,1,2,3,5,8...etc. Notice how the first two numbers are 0 and 1, and from there on out, the following numbers are simply the previous two added together.

In the code that I have below, I am storing the number of terms into a variable called "terms". I initialized the first term to 0 and the second to 1, because as long as those are in place, the rest of the sequence can be built. Since the user will likely enter an amount of terms greater than 2, a while loop is utilized to find the nxt term in the sequence by added the preceding two terms. The variables are then interchanged so that the process can continue. This is seen in the line "n2 = nth", and the "n1" transforms into the "n2" so that the process is infinite.

terms = int(input("How many terms? "))

# first two terms
n1, n2 = 0, 1
count = 0

# check if the number of terms is valid - fibonacci sequence only includes positive numbers and not negative
if terms <= 0:
   print("Sorry! Please enter positive integers only")
# if there is only one term, return n1
elif terms == 1:
   print("Fibonacci sequence upto",terms,":")
   print(n1)
# generate fibonacci sequence
else:
   print("Fibonacci sequence:")
   while count < terms:
       print(n1)
       nth = n1 + n2
       # update all new values
       n1 = n2
       n2 = nth
       count += 1
Fibonacci sequence:
0
1
1
2
3

Palindromes

Palindromes are words that are exactly the same when spelt backwards. For example, "poop" would considered a palindrome because it is spelt the same way both backwards, and forwards. The code below determines whether or not the user's input is indeed a palindrome, and if not, it will execute a short block that turns the user's input into an example of a palindrome. Test it out!

first = input("What would you like to become a palindrome?")
print("Your word:", first)
length = len(first)
firstpart = first[0:(length-1)]
lastchar = first[-1]
flip = first[::-1]

if first == flip:
    print(str(first) + " is a palindrome!")
else:
    print(str(first) +" isn't a palindrome")
    print("This is though:",firstpart+flip)
Your word: car
car isn't a palindrome
This is though: carac