Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dictionary
#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


Messages In This Thread
dictionary - by garikhgh0 - Feb-06-2018, 05:43 AM
RE: dictionary - by gjenkinslb - Feb-06-2018, 07:19 AM
RE: dictionary - by garikhgh0 - Feb-06-2018, 07:41 AM
RE: dictionary - by buran - Feb-06-2018, 07:47 AM

Forum Jump:

User Panel Messages

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