Krzysiek Partyka
Krzysiek Partyka

Reputation: 11

TypeError: keys() takes no arguments (1 given)

When I hit "5", my program doesn't give me back all the keys in a form of a list (as expected) but an error occurs instead:

TypeError: keys() takes no arguments (1 given)

Why is that?

elif choice == "5":
    print("\nOto wszystkie terminy znajdujace sie w slowniku:")
    for i in dictionary:
        print(dictionary.keys(i))

Upvotes: 1

Views: 10386

Answers (6)

I got the same error below:

TypeError: keys() takes no arguments (1 given)

Because I used keys() with an argument to get the specific key's value in a dictionary as shown below:

dict = {"name": "John", "age": 36}
          # ↓ Here ↓
print(dict.keys("name")) # Error

So, I used [] as shown below, then the error was solved:

dict = {"name": "John", "age": 36}
        # ↓ Here ↓
print(dict["name"]) # John

Upvotes: 0

Krzysiek Partyka
Krzysiek Partyka

Reputation: 11

If I use print(dictionary.get(i)) or print(dictionary[i]) it gives me back list of values not keys:

The code is:

choice = None

dictionary = {"OCB":"bletki do krecenia papierosow","kompas":"laptok, lub kopmputer",
              "prezesura":"niepodwazalna jakosc","kopalnia":"miejsce gdzie sie pracuje",
              "hitler":"laska meczaca bule","bula":"tak zwana muka" }


while choice != "0":
    print("""
                         MENU
                 0 - zakoncz program
                 1 - znajdz termin
                 2 - dodaj nowy termin
                 3 - zmien definicje terminu
                 4 - usun termin ze slownika
                 5 - pokaz cala zawartosc slownika
                                            """)

    choice = input("\nPodaj swoj wybor: ")

    if choice == "0":
        print("\nNARRRA")

    elif choice == "1":
        term = input("\nPodaj termin ktory mam znalezc: ")
        if term in dictionary:
            definition = dictionary[term]
            print("\nTermin ", term, " oznacza: ", definition)

        else:
            print("\nTen termin nie znajduje sie w slowniku mozesz sprobowac go dodac")


    elif choice == "2":
        term = input("\nPodaj termin ktory chcesz dodac: ")
        if term not in dictionary:
            definition = input("\nPodaj definicje: ")
            dictionary[term] = definition

        else:
            print("\nTen termin juz znajduje sie w slowniku")

    elif  choice == "3":
        term = input("\nPodaj termin ktoremu chcesz zmienic definicje: ")
        if term in dictionary:
            definition = input("\nPodaj nowa definicje: ")
            dictionary[term] = definition
        else:
            print("\nTego terminu nie ma w slowniku")


    elif choice == "4":
        term = input("\nPodaj temin ktory chcesz usunac ze slownika: ")
        if term in dictionary:
            del dictionary[term]
            print("\nOK usunalem termin: ", term," ze slownika")

    elif choice == "5":
        print("\nOto wszystkie terminy znajdujace sie w slowniku:")
        for i in dictionary:
            print(dictionary[i])



    else:
        print("\nZly wybor")


input("\nZAKONCZ ENTER")        

Upvotes: 0

Krzysiek Partyka
Krzysiek Partyka

Reputation: 11

elif choice == "5":
    print("\nOto wszystkie terminy znajdujace sie w slowniku:")
    for i in dictionary.keys():
        print(i)

thats what i've been looking for :) Now it works how it supposed to, thank you for your help :) I just started learning python so all those small things are still making me a problem :)

Upvotes: 0

faebser
faebser

Reputation: 149

Your call to dictionary.keys(i) is not valid. The .keys() method does not take an argument and will always return a list of keys. To print the 5th key in the dictonary use the following code:

print(dictionary[i])

Upvotes: 0

Anuj Gautam
Anuj Gautam

Reputation: 1255

You use dictionary[i] or dictionary.get(i) to retrieve the value of the specific key.
dictionary.keys() returns the list of keys in the dictionary and is does not take any argument.
So, you should use:

elif choice == "5":
    print("\nOto wszystkie terminy znajdujace sie w slowniku:")
    for i in dictionary:
        print(dictionary.get(i))

Upvotes: 0

zipa
zipa

Reputation: 27879

So you just want to print:

print(dictionary[i])

dictionary.keys() is a method that takes no arguments and returns a list of dictionary keys.

Upvotes: 1

Related Questions