Is it None or int? (Type Error) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is it None or int? (Type Error)

Hi guys! I'm back because I've got a type error that I don't know how to solve. Here's a piece of my function: books = {'harry potter': 3, 'the midnight library': 0, 'the alchemist': 1} def sell_books(): while True: book = input('Which book do you want? ').lower() # books we sell if book in books.keys(): copies = books.get(book) if copies >= 0: # HERE'S THE TYPE ERROR print('Book sold!') books.update({book: copies - 1}) The error says: " '>=' not supported between instances of 'NoneType' and 'int'." According to this, the None type is ''copies'' (that is ''books.get(book)'') but the type() function returns it actually as an int... type(copies) <class int> # I've also tried so: copies is None False I'm quite confused. Is copies None or is it int? And then, how can I do the comparison? I don't get it! D:

30th Jun 2021, 5:16 PM
Carola
Carola - avatar
7 Answers
+ 4
Carola Mollica , the only possible way that "copies" gets None is if get() is used but the key is not found in dict. but in the code you have presented this can not happen, because the code checks if the input (title) is present as a key in the dictionary before calling get().
30th Jun 2021, 7:28 PM
Lothar
Lothar - avatar
+ 3
I don't see any issues with your code. Anyway, here is a shorter version of your code: books = {...} # All the books def sell_books(): while (book := input("Which book do you want?").lower()) != "exit": if books.get(book, 0): print("Book sold!") books[book] -= 1
2nd Jul 2021, 3:43 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Try to add an `else` block in anticipation to the event where <book> was not IN <books>, let me know how it goes ...
30th Jun 2021, 6:07 PM
Ipang
+ 2
Yes. It is working fine with Indented code which you posted. But my guess only that 'if idented out then on first input ,if 1st if fails then it is error. But also not type error. Something else .. according to information you posted , it is working fine. It may better post full code or post inputs which you are trying...
30th Jun 2021, 6:29 PM
Jayakrishna 🇮🇳
+ 1
Funny, I don't get such error when trying that code. You sure this is the correct & complete code? Try to save it and share its link instead. Maybe someone can see beyond what I can see. https://www.sololearn.com/post/75089/?ref=app
30th Jun 2021, 5:43 PM
Ipang
+ 1
Indeed, I noticed sometimes it works and sometimes it doesn't. I'm suspecting there could be a bug in my editor (?). Anyway, I know I could post the file, but I didn't because... I get an error here as well! :D The upload doesn't start, so I prefer just to paste the code. I'm sorry for that but I can't do otherwise.
30th Jun 2021, 6:00 PM
Carola
Carola - avatar
0
Jayakrishna🇮🇳 do you think it is due to an indentation mistake? But if that were the case, doesn't the variable "copies" still remain set in memory as int type, since the "books.get(book)" returns an int type? I still don't understand how "copies" sometimes becomes None.
30th Jun 2021, 6:09 PM
Carola
Carola - avatar