NordKivu
NordKivu

Reputation: 9

Printing out specific string that corresponds to a number

Hello I've been learning pyhton for about 2 weeks using mobile apps but I figured forcing myself to create a project is helping me learn faster. Is there a script can I use to assign a number to a string so when that specific int is pressed, it prints out it's definition?

>>>>d = {1:"apple", 2:"peach", 3:"cherry"}
>>>>mylist = ["apple", 1, "peach", 2, "cherry", 3 ]
>>>>num = int(input())
>>>>If num == int in mylist:
>>>>print(str(num))

sorry if I put the code in wrong format, but that's what I currently have. I wanted to make the outcome of user input recognize the int and print it's value without having to write every single out come. Is that possible or do I have to write them all?

Upvotes: 0

Views: 730

Answers (1)

Lakshya Raj
Lakshya Raj

Reputation: 1775

You can create dictionaries. Dictionaries are made exactly for that purpose. Here's an example:

dictionary = {1: 'my string', 'string': 'other'}
print(dictionary[1])
print(dictionary['string'])

That will output

my string
other

You can assign numbers based on that. Read the tutorial for a better understanding.

Upvotes: 1

Related Questions