-1

I have this dictionary:

{2014: [12000, 14000], 2015: [14500, 16000], 2016: [18000, 19000], 2017: [17500, 8500], 2018: [9600, 1100]}

I need to print a list with with the 2 values like [12000, 4000] when the user types a specific key, check if the input year is in the keys of the dictionary and this procedure stops when the user types whitespace and not a valid year.

l1 = {2014: [12000, 14000], 2015: [14500, 16000], 2016: [18000, 19000], 2017: [17500, 8500], 2018: [9600, 1100]}
list2 = []
temp_total1 = []
temp_total2 = []
for key, value in l1.items():
  year = int(input("Give the Year"))
  if year < 2014 and year > 2018 or year == '':
    break
  if year in l1:
    print("There is a year in the dictionary")
    temp_total1 = temp_total1.append(value[0])

    temp_total2 = temp_total2.append(value[1])
    list2 = zip(temp_total1, temp_total2)
  else:
    break
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
scoute21
  • 21
  • 5
  • 1
    What is the problem? What do you want to know about this code? – mkrieger1 Jul 17 '21 at 18:54
  • gives me this `list2=zip(temp_total1,temp_total2) TypeError: zip argument #1 must support iteration` – scoute21 Jul 17 '21 at 19:06
  • So your question is "Why is the return value of calling `append` not a list and how do I use `append` correctly?", which is answered here: [Why does list.append() return None?](https://stackoverflow.com/questions/20016802/why-does-list-append-return-none) – mkrieger1 Jul 17 '21 at 19:08
  • exactly, while appending in a list doesn't return anything. it's just simply append the element in the list – Anonymous Coder Jul 17 '21 at 19:17

2 Answers2

0

I need to print a list with with the 2 values like [12000,4000] when the user types a specific key, check if the input year is in the keys of the dictionary and this procedure stops when the user types whitespace and not a valid year.

The implementation of that would be the following:

data = {2014: [12000, 14000], 2015: [14500, 16000], 2016: [18000, 19000], 2017: [17500, 8500], 2018: [9600, 1100]}

year = input("Give the Year")
while year.strip() != "" and year in data.keys():
    print(data[year])
TheEagle
  • 5,808
  • 3
  • 11
  • 39
-2

you might wants to do something like this:
try running this code once:

for key, value in l1.items():
  year=int(input("Give the Year"))
  if (year<2014) or (year>2018) or year=='':
      break
  if year in l1:
      print("There is a year in the dictionary")
      temp_total1.append(value[0])
      temp_total2.append(value[1])
      list2 = zip(temp_total1,temp_total2)
  else:
      break
Anonymous Coder
  • 556
  • 2
  • 16
  • What is the difference between your code and the code of the OP ? I don't see one ? – TheEagle Jul 17 '21 at 19:08
  • the above code is asking for a return value after appending in the list which creates error in the zip function. zip function is not iterable through None object – Anonymous Coder Jul 17 '21 at 19:09
  • `list2=list(zip(temp_total1,temp_total2))` Returns `[(12000, 14000)], [(14500, 16000)], [(18000, 19000)], [(17500, 8500)], [(9600, 1100)]` when the year is different the loop continue and each time it outputs the current values of the input year but the previous also until the dict has no values left – scoute21 Jul 17 '21 at 19:25
  • 1
    exactly @scoute21 when you enter year<2014 or year>2018, your loop will break and you can print the list2 – Anonymous Coder Jul 17 '21 at 19:31