Skip to content

Instantly share code, notes, and snippets.

@flandolf
Created May 12, 2022 09:46
Show Gist options
  • Select an option

  • Save flandolf/79fd50061bbdc57b37a99ce35ca27490 to your computer and use it in GitHub Desktop.

Select an option

Save flandolf/79fd50061bbdc57b37a99ce35ca27490 to your computer and use it in GitHub Desktop.
ideal fraction calculator.
# ideal fractions are when 2 pairs of fractions. When they are added they are the same as when they are multiplied
# how to make one you have to make the denominators equal the numerator
# eg 16/7 and 16/9 are ideal as 7 + 9 = 16 (numerator)
from colorama import *
import time
init()
print(Fore.GREEN + "Welcome to the ideal fraction calculator / by Dumpy")
print(Fore.CYAN + "\033[1mhttps://gist.github.com/dumpydev\033[0m\n")
print(Fore.RED, end="")
print('Initialising...')
time.sleep(0.125)
def addFraction(fraction1, fraction2):
try:
print(Fore.BLUE + '[+] Splitting fractions...')
fraction1 = fraction1.split('/')
fraction2 = fraction2.split('/')
print(Fore.GREEN + '[+] Splitted!')
fraction1 = int(fraction1[0])/int(fraction1[1])
fraction2 = int(fraction2[0])/int(fraction2[1])
print(Fore.RED + '[+] Calculating {} + {} ...'.format(int(fraction1), int(fraction2)))
time.sleep(0.1)
fraction = fraction1 + fraction2
print(Fore.GREEN + '[+] Calculated!')
return fraction
except:
print("Invalid input. Try again. Fraction format should be: x/y")
exit(1)
def multiplyFraction(fraction1, fraction2):
try:
print(Fore.BLUE + '[x] Splitting fractions...')
fraction1 = fraction1.split('/')
fraction2 = fraction2.split('/')
print(Fore.GREEN + '[x] Splitted!')
fraction1 = int(fraction1[0])/int(fraction1[1])
fraction2 = int(fraction2[0])/int(fraction2[1])
print(Fore.RED + '[x] Calculating {} x {} ...'.format(int(fraction1), int(fraction2)))
fraction = fraction1 * fraction2
print(Fore.GREEN + '[x] Calculated!')
return fraction
except:
print("Invalid input. Try again. Fraction format should be: x/y")
exit(1)
fraction1 = input('Enter fraction 1: ' + Fore.YELLOW)
print(Fore.YELLOW, end="")
fraction2 = input('Enter fraction 2: ' + Fore.RED)
# add the 2 fractions together
print(Fore.BLUE, end="")
print('Adding Fraction...\n')
time.sleep(0.2)
fraction = addFraction(fraction1, fraction2)
print(Fore.YELLOW, end="")
print('Multiplying Fraction...\n')
time.sleep(0.2)
# multiply the 2 fractions together
fraction7 = multiplyFraction(fraction1, fraction2)
time.sleep(0.3)
# if fraction isnt fraction 7
print(Fore.GREEN, end="")
print('Checking....')
if fraction != fraction7:
# print the fraction
print(Fore.RED + "Fraction 1: " + fraction1)
print("Fraction 2: " + fraction2)
print(Fore.YELLOW +'Are NOT ideal fractions!')
print(Fore.GREEN + 'Ideal fractions are when 2 pairs of fractions. When they are added they are the same as when they are multiplied')
print(Fore.BLUE + 'How to make one you have to make the both denominators when added, to equal the numerator (which must be the same number for both)')
# if fraction is fraction 7
else:
# print the fraction
print(Fore.BLUE + "Fraction 1: " + fraction1)
print(Fore.YELLOW + "Fraction 2: " + fraction2)
print(Fore.GREEN +'Are ideal fractions!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment