Wednesday, February 4, 2015

Fibonacci Speaks Parseltongue

Get it? Because I'm making a Fibonacci Sequence generator in Python... No? Ok...

The next homework assignment is divided into two parts, I have to created a Fibonacci generator that will print a list of numbers however long the user wants. And second, I have make a very simple text based Yahtzee game.

While I was in class tonight, I managed finish the first part of the assignment. Here is what I have:

#Assignment003_ChristianMunoz.py
#02/04/2014

import random
import math
import sys
import time

#This function suspends the thread for around 1 
def sleeper():
    time.sleep(1.0)

def fibonacci():

    finalSequence = []
        
    aInt = 0
    bInt = 1
    count = 0

    #Player decides how many digits of the fibonacci sequence they want
    amount = eval(input('How many digits of the sequence do you want? (At least 2) '))

    print(" ")

    #This checks to makes sure the number will always be an interger
    amount = math.floor(amount)

    #This checks to make sure its a valid entry
    if amount < 2:
        print("That is not a valid entry!")

        print(" ")
        
        fibonacci()

    #If the player just wants to see 2 digits then we print them right away
    elif amount == 2:
        finalSequence.append(aInt)
        finalSequence.append(bInt)

        print(finalSequence)

    #If the entry is valid, then execute code
    else:

        finalSequence.append(aInt)
        finalSequence.append(bInt)

        while (count < amount - 2):

            tempInt = aInt + bInt

            aInt = bInt
            bInt = tempInt

            finalSequence.append(tempInt)

            count = count + 1

        print(finalSequence)

    print(" ")

    playAgain()

#This function will run the next game if the player wants to start again
def playAgain():

    decision = input("Would you like to play again? Y/N ")

    print(" ")
    
    decision = decision.lower()

    if decision == "yes" or decision == "y":
        main()

    else:
        print("See you later!")
        sleeper()
        sys.exit()

#main function
def main():
    
    playerChoice = eval(input("Press 1 to see the Fibonacci Sequence or 2 for Dice: "))

    print(" ")

    #This makes sure that the number will always be an integer
    playerChoice = math.floor(playerChoice)

    #This checks to make sure the entry is a valid entry 
    if playerChoice < 1 or playerChoice > 2:
        print("That is not a valid entry!")

        print(" ")
        
        main()

    #If the entry was valid, then execute code
    elif playerChoice == 1:
        print("You have chosen the Fibonacci Sequence!")

        print(" ")
        
        fibonacci()

    else:
        print("You have chosen the Dice game!")
        
        diceGame()

#Starts the sequence
main()

I've been thinking about the Yahtzee game, and I don't think it will be that hard. That's all for now, thanks for reading!

No comments:

Post a Comment