Monday, February 23, 2015

Python Extra Credit Assignment - EC0

So I had some free time today and I decided to do one of the extra credit assignments for my python class.

The assignment is to write two different variations of functions that take in two numbers and compares them returning True if they are the same value and False if not. The trick is that we can't use the regular cooperators. Ex: ==, >=, <=, >, <, !=, etc. 

I decided to use "is" and "is not" to compare the input by the user. Here is the code I wrote and thanks for reading:


#EC0_ChristianMunoz.py
#02/23/2015

import sys
import time

def sleeper(seconds):
    """This functions suspends the thread for a given amount of seconds."""
    time.sleep(seconds)


def make_list(user_input):
    """This function takes the input by the user and makes it into a list."""
    user_input = user_input.split()
    return user_input


def play_again():
    """This function will run the next session if the user wants to continue."""
    decision = input("Would you like to play again? Y/N\n").lower()

    if decision == "yes" or decision == "y":
        main()
    elif decision == "no" or decision == "n":
        print("See you later!")
    else:
        print("That is not a valid entry!\n")
        play_again()

def variation_one(list):
    """This function uses the Identity Comparison "is" to compare both number."""
    if int(list[0]) is int(list[1]):
        return True
    else:
        return False


def variation_two(list):
    """This function uses the Identity Comparison "is not" to compare both number."""
    if int(list[0]) is not int(list[1]):
        return False
    else:
        return True


def main():
    player_input = input("Enter two numbers that you would like to compare:\n")
    player_input = make_list(player_input)
    print("Variaton one is :",variation_one(player_input))
    print("Variaton two is :",variation_one(player_input))
    play_again()


if __name__ == "__main__":
    main()
    sleeper(1.0)
    sys.exit()

No comments:

Post a Comment