Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dict problem with if
#1
Hi , I have a little problem with my code , can you explain the problem to me and help to fix it please ? What should I do ? Thanks

dict={"Name":"Adem",
     "Surname":"Ben rhouma"}

if "Name"=="Adem":
    print("My name is {}".format("Name"))
else:
    print("Dont remember my name")
why doesn t it show me "My name is Adem" ?
Reply
#2
Try to be careful not to name something that is a built in name, you named your dictionary as dict which is a built in.
you are not accessing the dictionary it should be like this
person = {"Name": "Adem",
        "Surname": "Ben rhouma"}

if person["Name"] == "Adem":
    print("My name is {}".format(person["Name"]))
else:
    print("Dont remember my name")
Output:
My name is Adem
Reply
#3
thanks a lot it's working! have a nice day
Reply
#4
(Sep-21-2019, 08:55 PM)Adem Wrote:
if "Name"=="Adem":

Do you understand why the expression "Name" == "Adem" evaluates to False?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a dict in dict cherry_cherry 4 73,051 Apr-08-2020, 12:25 PM
Last Post: perfringo
  dict problem Sol 2 3,318 Feb-04-2018, 06:59 PM
Last Post: Sol

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020