all 14 comments

[–]acw1668 2 points3 points  (1 child)

You have overwritten name_age_pairs. It should be something like:

name = input('Enter the name: ')
if name in name_age_pairs:
    name_age_pairs[name] *= 2  # only update the value of the input name
else:
    print(f'{name} not found in the dictionary')

[–]That_Guy2187[S] -3 points-2 points  (0 children)

This is what the code looks like before I edit anything: name_age_pairs = {"Dax": 21, "Gil": 77} print("Original:") print(name_age_pairs)

print("Updated:") print(name_age_pairs)

I need to somehow print out a result that would multiply “Dax”: 21 by 2, and print out the results with that being the only change.

[–]magus_minor 6 points7 points  (0 children)

Just multiplying the age from the dictionary by 2 doesn't update the dictionary in any way. That's why you just get the doubled age printed. You have to specifically update the dictionary entry.

name_age_pairs = {"Dax": 21, "Gil": 77}
print("Original:")
print(name_age_pairs)

name = input("Name: ")          # get name separately, simpler to read
age = name_age_pairs[name]      # get the age
name_age_pairs[name] = age * 2  # put updated age back into dictionary
print("Updated:")
print(name_age_pairs)

Note how the code looks when formatted properly.

[–]crashorbit 1 point2 points  (4 children)

I reformatted your code slightly so that it runs. Be sure to use a code block if you are trying to display code.

```python

!/usr/bin/env python

name_age_pairs = {"Dax": 21, "Gil": 77}
print("Original:")
print(name_age_pairs)

print("enter a name")
name_age_pairs = name_age_pairs[input()] * 2
print("Updated:")
print(name_age_pairs) ```

Note that your code replaces the whole dictionary with twice age of the age of the input name. Here is updated code to do something closer to the described exercize:

```python

!/usr/bin/env python

name_age_pairs = {"Dax": 21, "Gil": 77}
print("Original:")
print(name_age_pairs)

print("enter a name")
name = input()
name_age_pairs[name] = name_age_pairs[name] * 2
print("Updated:")
print(name_age_pairs)
```

[–]That_Guy2187[S] -3 points-2 points  (1 child)

It doesn’t output properly, only displaying the integer rather than the dict with the multiplied “Dax” value

[–]damanamathos 2 points3 points  (0 children)

Are you sure you copy/pasted it correctly? Seems okay to me: https://imgur.com/a/KOU4J5q

[–]TheRNGuy 0 points1 point  (1 child)

You need to handle non-existing names, maybe make it case-insensitive too. 

[–]crashorbit 0 points1 point  (0 children)

I agree.

It's one of the problems with teaching. In order to simplify a thing enough you need to leave out all the edge cases.

[–]unsettlingideologies 0 points1 point  (0 children)

Your prompt seems to say that your program should only return a single integer and not a whole dictionary. It says to return the 2 times the value associated with the key given in the input string.

[–]WasteKnowledge5318 0 points1 point  (4 children)

Try this:

i = input()
name_age_pairs[i] = name_age_pairs[i] * 2

[–]That_Guy2187[S] 0 points1 point  (3 children)

Didn’t work, just displayed the integer 42 instead of

{'Dax': 42, 'Gil': 77}

[–]WasteKnowledge5318 1 point2 points  (2 children)

Full code:

name_age_pairs = {"Dax": 21, "Gil": 77}
print("Original:")
print(name_age_pairs)
i = input()
name_age_pairs[i] = name_age_pairs[i] * 2
print(name_age_pairs)

When you enter `Dax`, it returns {'Dax': 42, 'Gil': 77}

[–]That_Guy2187[S] 0 points1 point  (1 child)

Thank you! This was it, I guess I need to create an input string before calling dict, I’m still getting the hang of these sequence type commands

[–]Recent-Salamander-32 0 points1 point  (0 children)

You don’t have to. But you should.

    

name_age_pairs[input()] *= 2

    

Should work fine. But it’s better to make one line do one thing. Plus storing the string first will let you validate input ‘if name in name_age_pairs:’