Issue
With this code I would like to print Jessica è chiamata alla prova del tris
, then recall from the dictionary first the word "chiamare", then check whether to use the word "è chiamata" through the for loop (because Jessica is a feminine name and therefore there is the "a" "è chiamatA").
The use of that for loop is important, so if you want to help me, try to stick to it and stay on that loop by modifying it as little as possible, because the loop first checks which is the subject and then in relation to the subject checks which word to use by looking for it in the dictionaries. I just call it with {verb}
.
Previously I only needed to call it with {verb}
, because in the dictionary I had two separate words like "è chiamata" and "è chiamato". It worked fine. Now, however, I have grouped these two verbs into a single verb which is "to call".
PROBLEM
So now if I try to print, it just prints alla prova del tris
(without her she is called or called).
So I thought it takes some way to make it clear that I would like to recall the word "chiamare" and then check which one to use "è chiamata" or "è chiamato". So I tried to use {verb ['chiamare']}
, but it obviously doesn’t work, I know. Only later did I read why I can’t use this, so let’s avoid dwelling on why I don’t have to write like this. Let’s think about solving
WHAT I AM LOOKING FOR?
So what I would like is: in the sentence recall "call" to let him choose whether to use "is called" or is called. "To choose you need my for loop because you must first relate it to the subject. I’ll write you what I want to do in order: the for loop relates the subject and the verb. She sees that Jessica’s dictionary says it’s feminine and singular, so the for loop uses the verb "è chiamatA" because this verb in the dictionary is feminine and singular. The for loop works correctly. But now that I have added the verb "chiamare" which encloses the 2 verbs "è chiamato" or "è chiamata", the project no longer works. So I thought there was a need to indicate that I wanted to invoke the verb "to call" using {verb [‘to call’]}, but of course it doesn’t work.
So how could I fix it?
IMPORTANT: before checking inside "chiamare" whether to use "è chiamato" or "è chiamata", remember that I first check the subject with the cycle, because thanks to the cycle I put in relation subject and verb. So my cycle I use is important
Can you help me? Thank you
Name = {
"Jessica": {"gender" : "female", "quantity" : "singular"},
"David" : {"gender" : "male", "quantity" : "singular"}
}
dictionary_verbs = { "chiamare": {"è chiamata" : { "gender" : "female", "quantity" : "singular"},
"è chiamato" : { "gender" : "male", "quantity" : "singular"}},
#"other_verb": ...
#"other_verb": ...
}
subject = "Jessica"
#Search verb in relation to the subject
verb = ""
for key, value in dictionary_verbs.items():
if Name[subject] == value:
verb = key
break
phrase = (f"{verb['chiamare']} alla prova del tris")
print(phrase)
Solution
The following code should work for your usecase.
Name = {
"Jessica": {"gender" : "female", "quantity" : "singular"},
"David" : {"gender" : "male", "quantity" : "singular"}
}
dictionary_verbs = { "chiamare": {"è chiamata" : { "gender" : "female", "quantity" : "singular"},"è chiamato" : { "gender" : "male", "quantity" : "singular"}},
#"other_verb": ...
#"other_verb": ...
}
subject = "Jessica"
#Search verb in relation to the subject
verb = {}
for key, value in dictionary_verbs.items():
for nested_k,nested_v in value.items():
if Name[subject] == nested_v:
verb[key] = nested_k
break
phrase = (f"{subject} {verb['chiamare']} alla prova del tris")
print(phrase)
Adding the following explanation for the discussion in comments:
Following is your code:
for key, value in dictionary_verbs.items():
#variable value here contains this whole nested dictionary **{"è chiamata" : { "gender" : "female", "quantity" : "singular"},"è chiamato" : { "gender" : "male", "quantity" : "singular"}**
#what your code is comparing is {"gender" : "female", "quantity" : "singular"} from Name dictionary for key "jessica" with the value variable as mentioned above which will never match and the loop will never break,
if Name[subject] == value:
verb = key
break
for key, value in dictionary_verbs.items():
for nested_k,nested_v in value.items():
"""
values for nested_k are "è chiamata" and "è chiamato" in subsequent iteration;
values for nested_v are {"gender" : "female", "quantity" : "singular"} and { "gender" : "male", "quantity" : "singular"} respectively, which can be matched with "Name" dictionary
"""
if Name[subject] == nested_v:
verb[key] = nested_k
# key = chiamare
# nested_key = è chiamata or è chiamato
break
I would suggest using a different structure/format for the "dictionary_verbs" dictionary if possible. More nested the dictionary is more for-loops will be added and if its a large dictionary it will simply add too much time complexity.
Also in the code, we introduce a new "verb" dictionary so that we can use it to print the "phrase" later in the code. If dictionary_verbs is a large dictionary verbs dictionary also will be large and take up more space.
Answered By – apoorva kamath
Answer Checked By – Katrina (BugsFixing Volunteer)