Can the Python dictionary key value be output as a string of variable values?

Asked 2 years ago, Updated 2 years ago, 13 views

From Python Dictionary

A = { 'Nice to meet you':hi, 'Hello':bye} If there is, can I input the key value as input and output only the corresponding value?

python

2022-09-21 16:58

1 Answers

Hello!

A = { 'Nice to meet you' :'hi', 'Hello' :'bye'}

If you type user_input = input() # 'Hello',
print(A[user_input]) # 'bye' is output

# How to turn the loop... 
for k, v in A.items() :
    if user_input == k:
        print(v) # 'bye' is displayed

Have a nice day.

------- Add-------

A = { 'Nice to meet you' :'hi', 'Hello' :'bye'}

For_in range(2): # Number of times you want to repeat: #2 
    If you type user_input = input() # 'Hello',
    print(A[user_input]) # 'bye' is output


2022-09-21 16:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.