1
Cow_id_list = []

Herd_Size = int(input("Enter the size of the herd."))

for x in range(Herd_Size):
    Cow_id = int("Enter a unique 3 digit ID tag for cow",x+1)
    Cow_id_list.append(Cow_id)

print("Initiating yield entry...")

The user inputs an integer (n) which will be stored in variable Herd_Size. How to create a dictionary with the same (n) number of keys and with names of our choice? Name of key value will be ID tags

4
  • So what should the keys and values of the dict be? Are they both user input? Commented Apr 1, 2018 at 14:32
  • The keys should be the ID tag of the cow and the values should be the yield of milk that the user will input. Commented Apr 1, 2018 at 14:35
  • But the ID tag is also user input? Commented Apr 1, 2018 at 14:37
  • Yes, but I am confused on how can we create the (n) number of keys on a dictionary... Name of Key values will be the ID Commented Apr 1, 2018 at 14:40

3 Answers 3

2

This is one way.

Cow_id_list = []
Yield_list = []

Herd_Size = int(input("Enter the size of the herd."))

for x in range(Herd_Size):
    Cow_id = int(input("Enter a unique 3 digit ID tag for cow"))
    Yield = int(input("Enter yield for {0}".format(Cow_id)))
    Cow_id_list.append(Cow_id)
    Yield_list.append(Yield)

d = dict(zip(Cow_id_list, Yield_list))

Explanation

  • Ask for integer input for Cow_id the same way you do for Herd_Size.
  • Do the same for Yield. Create a Yield_list like Cow_id_list.
  • Create a dictionary at the end via dict(zip(ids, yields)). zip is used to iterate 2 lists simultaneously by index.
  • Applying dict will create a dictionary from the resulting pairs of values.

To improve your logic further, I recommend you consider adding some controls; for instance, to ensure that ids actually consist of 3 digits.

Sign up to request clarification or add additional context in comments.

Comments

2

jpp's way is probably better, because you get yield and ID all at once. However, if you want to keep your structure, you can do it like this:

Cow_id_list = []

Herd_Size = int(input("Enter the size of the herd."))

for x in range(Herd_Size):
    Cow_id = int(input("Enter a unique 3 digit ID tag for cow"))
    Cow_id_list.append(Cow_id)

print("Initiating yield entry...")

d = {}
for i in Cow_id_list:
    y = float(input("Enter yielf for cow {}: ".format(i)))
    d[i] = y

What is going on here is that you initiate d as an empty dictionary, then iterate through your cow IDs, get the yield one by one, and then add a key for each cow with the corresponding yield as a value to the dictionary d.

Comments

0

not sure why you need to create a dictionary with a specific number of keys. You can just ask the user for input on the ID and yield and put it into the dictionary.

if you have 10 cows, you're not going to have 15 ID's and yields.

def add_cow_info():
    add_info = str(input("input cow id and yield?: (y/n)")
    if add_info == "y":
        return True
    elif add_info == "n":
        return False


def main():
    cow_yield = {}
    input_cow = True
    while input_cow():
        ID = str(input("Enter ID: ")) #if the ID is 001, or 010, the result
                                      # will be 1, 10, respectively. 
                                      #Generally keys should be strings anyways.
        _yield = int(input("Enter yield: "))
        cow_yield[ID] = _yield

    return cow_yield

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.