Created
June 27, 2024 00:31
-
-
Save Ghdhdhdh/2ed5c588a64bc23e656d089363ec3682 to your computer and use it in GitHub Desktop.
Its a triangular number calculator incase you are tired of doing it manually
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# how many more triangular numbers to find | |
iter = 100 | |
# Is the answer compact | |
compact = True | |
def find_tri_nums(iter: int, compact: bool, print_ans:bool) -> int: | |
""" | |
:param iter: up to what triangular number do you want to calculate | |
:param compact: If printing is true, print it out in a compact format | |
:param print: Set to true if you want to print the results to the console. Set to false if you want a return dictionary | |
""" | |
nums = {} | |
n = 1 | |
tot = 1 | |
# Make dictionary of nums | |
for i in range(iter): | |
nums[n] = tot | |
n += 1 | |
tot += n | |
if print_ans: | |
if compact: | |
for key, value in nums.items(): | |
print(f"{key}: {value}") | |
else: | |
for key, value in nums.items(): | |
print(f"the {key}th triangular number is {value}") | |
else: | |
return nums | |
while True: | |
x = str(input("Do you want a list (type l) or 1 number (type n)")) | |
#check input is valid | |
if x == "l": | |
#set l value | |
l = True | |
n = False | |
# Put in text prompt | |
print("up to what number?", end='') | |
break | |
if x == "n": | |
# Set n value | |
l = False | |
n = True | |
# Insert text prompt | |
print("Which number?", end='') | |
break | |
else: | |
print("Incorrect values. Try again") | |
# Get number | |
while True: | |
try: | |
num = int(input()) | |
break | |
except TypeError: | |
print("Not a number, try again.") | |
# Return Value(s) | |
if x == "n": | |
dictioanry = find_tri_nums(num, compact, False) | |
print(dictioanry[num]) | |
if x == "l": | |
find_tri_nums(num, compact, True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment