Dictionary value remains unchanged in "for" loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Borden
    New Member
    • Jul 2011
    • 6

    Dictionary value remains unchanged in "for" loop

    Code:
    k= {}
    for i in range(0, 101):
        k[i]=0
    for j in range(0, 2):
        number=raw_input("give number")
        if number in k: #This part doesn't seem to work
            k[j]+=1
    print k



    Thanks in advance!
    Last edited by bvdet; Jul 30 '11, 11:46 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    raw_input() always returns a string. Your dictionary keys are integers.

    Comment

    • Borden
      New Member
      • Jul 2011
      • 6

      #3
      thanks for your reply,I changed it to "input" but still doesn't work :/

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        It works using input() and type casting number to int.

        >>> {0: 1, 1: 1, 2: 0, 3: 0, 4: 0,............. ...

        As you can see, k[0] and k[1] are incremented by 1.

        Comment

        • Borden
          New Member
          • Jul 2011
          • 6

          #5
          huh!Well,yes it does,but just not how i wanted!lol.I want the program to expect a number(0-80) from the user and then show on a "matrix" how many times that number was given!But it appears i am way far from that right now!Anyway,than ks for your time.

          Comment

          • Borden
            New Member
            • Jul 2011
            • 6

            #6
            My demanding is false,I wanted to change the keys of a dict,something that can't be done.I will use lists for my program now.Sorry for your trouble!

            Comment

            Working...