Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Don't excute my code
#11
(Oct-01-2019, 01:48 PM)jefsummers Wrote: In the last code snippet above I believe it should be in knights.items().

Yes, that is correct.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#12
Excuse me again, but I'm trying to learn how to use dictionaries and I'm having trouble understanding them.
Unfortunately I lack the logic in solving these questions that I find on sites like HackerRank.
I am trying, compared to before, to think but I still struggle because sometimes I don't understand the deliveries. Perhaps this is the first problem then writing the code I think comes to itself.
Thanks again,
RavCoder
Reply
#13
So I tried to code again (also thanks to some help that I found on the internet), but now it gives me this error can you help me? Blush

n = int(input()) 

address_book = {}  
for i in range (0, n):
    name , phoneNumber = input().split()
    address_book [name] = phoneNumber

    if (name is address_book.keys()):
        print(name + "=" + phoneNumber)
    else:
        print('Not found')
    
Error:
Compiler Message Wrong Answer Input (stdin) 3 sam 99912222 tom 11122222 harry 12299933 sam edward harry Your Output (stdout) Not found Not found Not found Expected Output sam=99912222 Not found harry=12299933
Reply
#14
You want name in, not name is.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#15
Ok it's works but I have still a error :
Error:
Compiler Message Wrong Answer Input (stdin) 3 sam 99912222 tom 11122222 harry 12299933 sam edward harry Your Output (stdout) sam=99912222 tom=11122222 harry=12299933 Expected Output sam=99912222 Not found harry=12299933
Reply
#16
First problem - you execute the if(name is address_book... statement every time you read a name when building the address book. De-indent that whole block to fix, but read further (you won't de-indent if you follow the below).
After that you have properly built the address book, but you are not input'ing the names to look up. You need to do that somewhere between lines 6 and 8. Putting a while True: at line 7, not indented, followed by name=input() will start the cycle to get names to look up.
Right after you read the name, you need to check if it is empty ("") and break out of your loop if it is.
In the next if statement you mean in, not is. (current line 8)
phone_number is meaningless at this point. You want to look up the number in that address book you worked so hard for. So, your print statement should be similar to
print(name+" = "+address_book[name])
Almost there...
Reply
#17
Thanks everyone, I resolved it
Reply


Forum Jump:

User Panel Messages

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