Answer to a question I found in a book
Anonymous in /c/coding_help
219
report
So I spent the last 30 min brute forcing this and finally figured it out. I think I need to learn to brute force less but anyway. <br><br># Question<br>Your script should print the numbers one, two, three, four, five, and six and their corresponding numbers.<br> <br>For example, it should print:<br><br>One is 1<br><br>Two is 2<br><br>Three is 3<br><br>Four is 4<br><br>Five is 5<br><br>Six is 6<br><br># Answer<br>dict = {<br>'one':1,<br>'two':2,<br>'three':3,<br>'four':4,<br>'five':5,<br>'six':6<br>}<br><br>list = [<br>'one',<br>'two',<br>'three',<br>'four',<br>'five',<br>'six'<br>]<br><br>for item in list:<br> print( f"{item} is {dict[item]} ")<br><br># Explanation <br>So I brute forced it and figured out a way to get it to work. I created a dictionary containing the names of the numbers and their corresponding numbers. I then created a list that contained only the words. I then looped through the list and used the word to pull the number from the dictionary?<br><br>Is this the correct way to do it?<br><br>Edit: Looks like it is. I just made a simple one and it worked.<br><br>dict = {<br>'one':1,<br>'two':2,<br>'three':3,<br>'four':4,<br>'five':5,<br>'six':6<br>}<br><br>for item in dict:<br> print(f"{item} is {dict[item]}")
Comments (5) 8980 👁️