Skip to content

Instantly share code, notes, and snippets.

@Ghdhdhdh
Created June 27, 2024 00:31
Show Gist options
  • Save Ghdhdhdh/2ed5c588a64bc23e656d089363ec3682 to your computer and use it in GitHub Desktop.
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
# 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