Last active
November 7, 2022 16:14
-
-
Save RamenJunkie/c5499b3ee1732ce5952615e1dd7a56ea to your computer and use it in GitHub Desktop.
A Joke adder that just makes a string out of an addition equation input.
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
looper = True | |
# Continues to ask until a valid input is given | |
while looper: | |
# Get input | |
input_var = input("Please enter an addition formula.\n") | |
# Needs to contain a + to be an addition problem. | |
if "+" not in input_var: | |
print("A valid addition formula has a + sign in it!\n") | |
else: | |
# Split the string to get out two numbers | |
formula = input_var.split("+") | |
# Makes sure the result contains at least to parts to eliminate input like "2+" | |
if len(formula) >= 2: | |
# Make sure there are ACTUALLY numbers on either side of the equation even if they are surrounded by letters | |
result1 = any(chr.isdigit() for chr in formula[0]) | |
result2 = any(chr.isdigit() for chr in formula[1]) | |
# If there are numbers, remove any letters of special characters that might also exist | |
if result1 and result2: | |
digit1 = formula[0] | |
for chr in digit1: | |
if chr.isdigit() == False: | |
digit1 = digit1.replace(chr,"") | |
digit2 = formula[1] | |
for chr in digit2: | |
if chr.isdigit() == False: | |
digit2 = digit2.replace(chr,"") | |
#Break out of the loop, we have two valid numbers! | |
looper = False | |
else: | |
print("Not a valid formula! You need two numbers to add!\n") | |
#print the string version of the addition, IE "2+2 = 22" | |
print(digit1+digit2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment