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!!!
1 Respuesta
+ 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.