omar wael omar wael - 3 months ago 17
Python Question

add keys and values from input method

I have a task and I did almost of it, but I'm stuck in how i will add keys and values into dictionary from user I think that I have to use

input
method but I'm not sure .

This code is what i done it shows the max value and key but i add the dictionary i want something like this

x= {'omar':20,'nagy':5}
maxKey= max(x, key=x.get)
maxValue=max(x.values())
print maxKey,maxValue


but the user is the one who enter the key and value

Answer

you can do:

count=2
x= {}
while count:
    name=input('name:')
    value=int(input('value:'))
    x[name]=value
    count-=1

maxKey= max(x, key=x.get)
maxValue=max(x.values())
print maxKey,maxValue

which will result in:

enter image description here

Comments