By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,944 Members | 1,819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,944 IT Pros & Developers. It's quick & easy.

Dictionary value remains unchanged in "for" loop

P: 6
Expand|Select|Wrap|Line Numbers
  1. k= {}
  2. for i in range(0, 101):
  3.     k[i]=0
  4. for j in range(0, 2):
  5.     number=raw_input("give number")
  6.     if number in k: #This part doesn't seem to work
  7.         k[j]+=1
  8. print k



Thanks in advance!
Jul 30 '11 #1

✓ answered by Borden

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!

Share this Question
Share on Google+
5 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
raw_input() always returns a string. Your dictionary keys are integers.
Jul 30 '11 #2

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

bvdet
Expert Mod 2.5K+
P: 2,851
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.
Jul 31 '11 #4

P: 6
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,thanks for your time.
Jul 31 '11 #5

P: 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!
Jul 31 '11 #6

Post your reply

Sign in to post your reply or Sign up for a free account.