Someone please solve this. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Someone please solve this.

Hello, The following code was written by someone else. **It is working fine** textbooks = { ("Maths","Calculus") : 600, ("Maths","Algebra") : 450, ("Science","Physics") : 700, ("Science","Chemistry") : 650 } student_borrow = ("Maths","Calculus") pages = textbooks[student_borrow] print(pages) but when I change student_borrow = input() **because I want to take input from user** **it's showing error** textbooks = { ("Maths","Calculus") : 600, ("Maths","Algebra") : 450, ("Science","Physics") : 700, ("Science","Chemistry") : 650 } student_borrow = input() pages = textbooks[student_borrow] print(pages) Can someone please explain why I can't able to take input from user? Thanks in advance!!!

26th Aug 2022, 6:14 AM
Lithin Naga Datta Sravan Sunkara
1 Answer
+ 2
Because input is string but you need it in format ("Maths","Calculus") Input will be '("Maths","Calculus")', You can wrap keys to be string like this: textbooks = { '("Maths","Calculus")' : 600, '("Maths","Algebra")' : 450, '("Science","Physics")' : 700, '("Science","Chemistry")' : 650 } student_borrow = input() pages = textbooks[student_borrow] print(pages) Now code will accept input because format is same PS: error code exualy was showing you exacly format, that there are not key '("Maths","Calculus")', note single quote.
26th Aug 2022, 6:44 AM
PanicS
PanicS - avatar