I have a question regarding Python input and output. (Newbie)

Asked 2 years ago, Updated 2 years ago, 18 views


water=[2,3,4,5]
fire=[6,7,8,9]
list_redblue=[water, fire]

answer=input('input=')
if answer in list_redblue:
    print(answer)
else:
    print('You have entered it incorrectly.')

When I was fire have been entered, or water here. 출력값을 [2,3,4,5]또는 [6,7,8,9]로 하고싶은데 w a t e r just so is printed.

What code should I write?

Python! I'm starting now, New B.

python

2022-09-20 20:14

2 Answers

#python3.8
water = [2,3,4,5]
fire = [6,7,8,9]
list_redblue=[water, fire]
answer = input('input :')

if answer in list_redblue:
    try:
        print(locals()[answer]) # 
    except:
        print('No!')
else:
    print('Wrong :(')


2022-09-20 20:14

It is not good to input the variable name defined in the program and output the value of the variable name.

Variable names must be independent of program behavior. What I mean is that I don't like the variable name water so it's better to work the same way even if I change it to washer later.

If you enter water and you want [2, 3, 4, 5] to be output, like this....

data = { "water":[2, 3, 4, 5], "fire":[6, 7, 8, 9] }
answer = input("input :")

if answer in data:
    print(data[answer])
else:
    print("Wrong :(")


2022-09-20 20:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.