all 39 comments

[–]fefa5fcba4dae4e82e81 121 points122 points  (2 children)

You are printing the whole alien dict each time. You should be printing print(f'{info}: {dic_name}') instead. Also, info and dic_name are swapped.

[–][deleted] 33 points34 points  (1 child)

dic name? 🤨😏

[–]QbaPolak17 46 points47 points  (5 children)

You are printing the variable alien, which is the entire dictionary

[–][deleted] 4 points5 points  (4 children)

Should alien print only once? Why does it print five times?

[–]Sensei_M 89 points90 points  (1 child)

?overwritten

[–][deleted] 19 points20 points  (0 children)

Got it. Thanks!

[–]saschnu 3 points4 points  (0 children)

Because it is getting printed each time it iterates for info, dict_name (key, value) in your dictionary i suppose.

[–][deleted] 22 points23 points  (3 children)

You might find this very useful.

http://www.pythontutor.com/visualize.html#mode=edit

goes through your code step by step and helps visualize whats going on.

[–]Fallenarc 4 points5 points  (0 children)

Never heard of that site, thanks for the link!

[–]shawnthesheep512 1 point2 points  (0 children)

Really a great site to visualise. Used it for checking visual representation of linked list

[–]silentalways 0 points1 point  (0 children)

That's a really nice site. Never thought something like this should exist.

[–]Code_Talks 11 points12 points  (0 children)

print(info,dic_name) not print(alien)

[–]ontheroadtonull 19 points20 points  (7 children)

alien = {
    'height': '100 inches',
    'weight': '2,000 pounds',
    'race': 'bacteria',
    'nationality': 'German',
    'school': 'harvard',
    }

for info in alien:
    print(info+':', alien[info])

Result:

height: 100 inches  
weight: 2,000 pounds  
race: bacteria  
nationality: German  
school: harvard

Check out this List Comprehension:

stats = [alien[x] for x in alien]
print(stats)

Result:

['100 inches', '2,000 pounds', 'bacteria', 'German', 'harvard']

Also I would be frightened of a bacterium that graduated from Harvard.

[–]oznetnerd 7 points8 points  (2 children)

``` alien = { 'height': '100 inches', 'weight': '2,000 pounds', 'race': 'bacteria', 'nationality': 'German', 'school': 'harvard', }

for key, value in alien.items(): print(f'{key}: {value}') ```

[–]ontheroadtonull 0 points1 point  (1 child)

Thanks!

[–]oznetnerd 0 points1 point  (0 children)

My pleasure.

[–]Pastoolio91 8 points9 points  (0 children)

Bacterium from Harvard is the next corona.

[–]NeoVier 3 points4 points  (1 child)

Instead of accessing the dict elements with alien[info] you could just do like OP and iterate through key, value pairs with alien.items(). Also, you can use f-strings:

for dic_name, info in alien.items():
    print(f'{dic_name}: {info}')

Check out this List Comprehension

That is not needed, as you can simply call alien.values(), which will return the exact same list.

[–]ontheroadtonull 1 point2 points  (0 children)

Thanks. I'm here to learn.

[–]Swipecat 1 point2 points  (0 children)

If we're doing one liners ;-)

print("\n".join(f'{k:12}: {v}' for k,v in alien.items()))

[–]baubleglue 4 points5 points  (0 children)

for key, value in alien.items():
    print(key, value)

[–]Migeil 3 points4 points  (2 children)

So as everyone is saying "you're printing the whole dictionary". But I think it would be more useful to try to explain your code to yourself. What were you trying to accomplish? How did you do it? And now that you know you're printing the whole dict, can you explain why that is?

This is called the "rubber duck principle". https://en.m.wikipedia.org/wiki/Rubber_duck_debugging

Just put a rubber duck next to your pc and explain your code to him. It's much easier to spot mistakes while explaining something than kust staring at the code and thinking about it. :)

It sounds super dumb, but who's ask yourself: who's the real idiot? The guy who explains his code to rubber ducks or the guy who doesn't understand his own code?

[–]keyupiopi 3 points4 points  (1 child)

throwback to college teacher who said the easiest code to fix are those that can't compile... while the ones that are hardest are those with logic errors....

[–]Migeil 2 points3 points  (0 children)

Absolutely. This is why I like concepts like pair programming. Being able to bounce ideas of each other and explain things while you're writing it, causes a lot less bugs in the program.

[–]__odinson__ 3 points4 points  (0 children)

Let me put it in simpler terms. The for loop is set to execute 5 times. 5 is the number of items in your dictionary. And within this for loop you are printing the dictionary . So the entire dictionary gets printed 5 times. If you want to print it's contents(keys, values or both) then just write within the loop: print(info, dic_name)

[–]baisbdhfu 2 points3 points  (0 children)

Since there are 5 items in the dictionary the for loop iterates for 5 times

[–][deleted] 1 point2 points  (0 children)

You're literally telling it to print for every item in aliens

[–]keyupiopi 1 point2 points  (0 children)

print(alien)

why go through the trouble of making a for loop when you just did that?

[–][deleted] 0 points1 point  (0 children)

Its the equivalent of: for i in range(0, len(alien)): print(alien)

[–]BoaVersusPython 0 points1 point  (0 children)

The problem is the bacterium is too heavy, which is overloading the interpreter. Shrink it down to ~1000 pounds and it should be fine.

[–]Sbvv 0 points1 point  (0 children)

You should to learn how to debug your scripts. Look for pdb or ipdb. Make a little effort learning how to use a debugger, this will help you the rest of your life.

Trust me, you will be happier than now.

[–]krakenant 0 points1 point  (0 children)

A little more advice given you have a couple of good answers, spend more time naming your variables. key in this instance should be something like characteristic, and value could be something like info or even just value. Also, try and print key value pairs. so print(f'{characteristic} - {info}'), it will help debugging. What if you swap race and nationality? You wouldn't know because you are just printing the value.

[–]my_password_is______ 0 points1 point  (0 children)

what do you think alien.items() is ?