# Demo entry 6783884

Comp Sci Assignment

Submitted by anonymous on Feb 25, 2019 at 05:37
Language: Python 3. Code size: 4.5 kB.

```from fractions import Fraction as fraction  # For Fraction Adder

import os  # For the Clear Function

def clear():  # Defines fuction
os.system('cls' if os.name == 'nt' else 'clear')  # Clears the screen.

keepGoing = "Y"  # Variable set to "Y" so that program runs.

while keepGoing == "Y":  # While loop so multiple tasks can be executed in the same session.

clear()

taskNumber = int(
input("Which task would you like the program to compute? (1,2,3,4) ")
)  # Asks for input.

if taskNumber == 1:

# Defines Factorial Function, Uses Recursion
def factorial(num):

# Base Case
if num >= 0 and num <= 1:
return 1

# Recursive
else:
return (num * factorial(num - 1))

# Asks for Input
num = int(input("Enter a `!unban 241331728650600450`number: "))

# Calls Function, Outputs Result
print("The factorial of %i is %i" % (num, factorial(num)))

elif taskNumber == 2:

# Defines Function
def factors(num):

# Initializes 'perfect' variable as global
global perfect

# Initializes list of factors
factorList = []

# For Loop to find factors
for i in range(1, num):

if num % i == 0:
# Adds to factor list
factorList.append(i)

# Define Variables
if sum(factorList) == num:
perfect = True
else:
perfect = False
return perfect

# Asks for Input
num = int(input("Enter a number: "))

# Calls Function
factors(num)

# Outputs Result
if perfect:
print("The number you entered, %i, is a perfect number." % (num))
else:
print(
"The number you entered, %i, is not a perfect number." % (num))

elif taskNumber == 3:

# Defines Function with 2 Arguments
def fractionAdder(fraction1, fraction2):

# Defines Sum
sumFraction = fraction1 + fraction2

# Uses Fraction Module to Calculate
sumFraction = str(
fraction(sumFraction).limit_denominator(100000000000000))

# Outputs Result
print("The sum of the two fractions is: %s" % (sumFraction))

# Asks for Input
fraction1 = eval(input("First Fraction: "))
fraction2 = eval(input("Second Fraction: "))
# Calls Function
fractionAdder(fraction1, fraction2)

elif taskNumber == 4:

# Asks for Operations
operation = input(
"Which operation would you like to execute? (+,-,*,/): ")

if operation == "+":

# Asks for Input
num1 = float(input("Enter a number: "))
num2 = float(input("Enter a number: "))

# Prints Output
print("The sum of %f and %f is %f" % (num1, num2, (num1 + num2)))

elif operation == "-":

# Asks for Input
num1 = float(input("Enter a number: "))
num2 = float(input("Enter a number: "))

# Prints Output
print("The difference of %f and %f is %f" % (num1, num2,
(num1 - num2)))

elif operation == "*":

# Asks for Input
num1 = float(input("Enter a number: "))
num2 = float(input("Enter a number: "))

# Prints Output
print("The product of %f and %f is %f" % (num1, num2,
(num1 * num2)))

elif operation == "/":

# Asks for Input
num1 = float(input("Enter a number: "))
num2 = float(input("Enter a number: "))

# Prints Output
print("The quotient of %f and %f is %f" % (num1, num2,
(num1 / num2)))

else:  # Error

print("ERROR: Choose a Valid Operation.")

else:  # Error

print("\nERROR: Invalid Task")  # Catches error.

keepGoing = input(
("\nWould you like to try other tasks? (Y, N) "))  # Asks for input.
clear()  # Calls function defined above.
```

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.