BREAK DOWN OF PROJECT
- The user is asked to enter a number and a second number.
- a variable named results is created by adding the two numbers together.
- We search for the results in the 1st if statement in a dictionary named ans (short for answers).
- This part functions as it should. It than gives those results back to the user by displaying the original numerical equation. And a version of the equation using letters (see full code below if you are confused).
However if the results do not match the 1st if statement it moves to the next. Before the whole if statement a variable named search is created by:
search = tuple(str(results))
I need to now take the results and search each individual character and see if those characters also appear in the dictionary (ans). However, it's not searching for the tuple character strings in the dictionary.
FULL CODE
translate_results = {
1: "A", 2: "B", 3: "C", 4: "D", 5: "E", 6: "F", 7: "G", 8: "H",
9: "I", 10: "J", 20: "T", 0: 0, "1": "A", "2": "B", "3": "C", "4": "D",
"5": "E", "6": "F", "7": "G", "8": "H", "9": "I", "0": "0",
}
ans = translate_results
user = input('Enter a number: ')
user2 = input('Enter another number: ')
results = int(user) + int(user2)
search = tuple(str(results))
if results in ans:
print(f"{user} + {user2} = {results}")
print(f"{ans[user]} + {ans[user2]} = {ans[results]}")
elif search in ans:
print(f"{user} + {user2} = {results}")
print(f"{ans[user]} + {ans[user2]} = {ans[search]}")
print(search)
else:
print(f" Answer is {results}")
print(f"However, elif statment failed. Tuple list created: {search}")
Example One
So if the user types in 1 (user) and 3 (user2) the output is:
1 + 2 = 3
A + B = C
Based on the 1st if statement.
Part 2 If Statement
This is suppose to activate when it finds the individual string characters from search (variable) in the dictionary (ans). Than it is suppose to take those characters match them in the dictionary and display the dictionary values instead.
Example Two (if it worked)
So the user enters 1 (user) and 29 (user2) the out put would be after matching variable search to ans:
1 + 29 = 30
A + BI = C0
Aucun commentaire:
Enregistrer un commentaire