Q: If a certain variable value matches a dictionary key value with Python, you want to create a syntax that outputs a value value.
For example,
Season = "Spring" s_fruit = { 'Spring' : 'Strawberry', 'Summer' : 'Watermelon'}
Season's spring If the spring of s_fruit matches, This is an example where you want to print only the value 'strawberry'.
First of all, I write get in if, so it's printed as below.
if fruit_dir.get(season):
print(s_fruit.values())
-> [Strawberry, Plum] I think it's because it's printed only with the value of spring.
A matching value was pulled from the conditional statement, but nothing was output.
if season == s_fruit.keys():
print(s_fruit.values())
What syntax should I modify? Help me T..T
dictionary
>>> Season = 'Spring'
>>> s_fruit = { 'Spring' : 'Strawberry', 'Summer' : 'Watermelon'}
>>> s_fruit[season]
"Strawberries."
>>> s_fruit.get(season)
"Strawberries."
>>> Season = 'Autumn'
>>> s_fruit[season]
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
s_fruit[season]
KeyError: 'Autumn'
>>> if season not in s_fruit:
print("no fruit for", season)
No fruit for autumn
Thank you so much for your answer. There's no place to ask I was struggling by myself and cried. I'll deal with it bravely from now on. ^ Thanks once again.
^
925 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
626 Uncaught (inpromise) Error on Electron: An object could not be cloned
618 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.