Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dictionary
#1
Hello. I want to figure out if there are anther ways to create such kind of dictionary (user input):

d ={'Jane': [20, 1.7],
'Jack': [15, 1.8],
'John': [12, 1.5],
--------------
a lot of line
-------------- }
thanks in advance

d = {}
L = []
i = 0
while i < 10:
  name = input('Enter your Name  :')
  age = eval(input('Input your age :'))
  height = eval(input('Input your height :'))

  L.append(age)
  L.append(height)

  d[name] = L
  L = []
  i+=1
  
  
print(d)
Reply
#2
Here is a variation that is a little better (see my comments):
d = {}
for _ in range(10): # loop ten times
    name = input('Enter your Name  :') # store str input
    if name == 'stop': break # nice to be able to stop early to test it
    age = int(input('Input your age :')) # convert str input to int
    height = float(input('Input your height :')) # convert str input to float
    d[name] = [ age, height] # add new list with age and height to dict d

print(d)

Here is another more structured:

d = {}

def input_name_data():
    name = input('Enter your Name  :') # store str input
    if name == 'stop': return None, None
    age = int(input('Input your age :')) # convert str input to int
    height = float(input('Input your height :')) # convert str input to float
    return name, [age, height]
    
for _ in range(10):
    name, data = input_name_data()
    if name is not None:
        d[name] = data
    else:
        break
   
print(d)
Reply
#3
if the user wants to largen the list size
d = {}
L = []
i = 0

while i < 3:
  
  
  name = input('Enter your Name  :')
  for j in range(5):          #changing the lenght of the list
    integer = eval(input('Input your value :'))

  
    L.append(integer) 
    d[name] = L
    
    
  L = []
  print(i)  
  i+=1
  
print(d)
Reply
#4
Take a look at alternatives provided by gjenkinslb and try to change these in order to do what you want.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020