Traceback (most recent call last): File "/piston/jobs/0c775e93-e6c7-44bd-ba6a-c7d9249119de/test.py", line 9, in <module> calories[day]=int(input())ValueError: invalid literal for int() with base 10: 'Day1'
Question
Traceback (most recent call last): File "/piston/jobs/0c775e93-e6c7-44bd-ba6a-c7d9249119de/test.py", line 9, in <module> calories[day]=int(input())ValueError: invalid literal for int() with base 10: 'Day1'
Solution
The error message you're seeing is a Python error. It's saying that the program is trying to convert a string to an integer, but the string it's trying to convert ("Day1") isn't a valid integer.
Here's a step-by-step breakdown of what's happening:
-
The program is running a line of code that looks something like this:
calories[day] = int(input()). This line is trying to read a line of input from the user, convert that input to an integer, and then store that integer in thecaloriesdictionary at the keyday. -
The
int(input())part of the line is where the error is happening. Theinput()function reads a line of input from the user as a string. Theint()function then tries to convert that string to an integer. -
In this case, the string that's being read is "Day1". The
int()function doesn't know how to convert "Day1" to an integer, because "Day1" isn't a valid representation of an integer. So, it throws aValueError.
To fix this error, you need to make sure that the input you're trying to convert to an integer is actually a valid integer. If you're expecting the user to enter a day of the week (like "Day1"), then you shouldn't be trying to convert that input to an integer. Instead, you should just store the input as a string in your dictionary.
Similar Questions
Traceback (most recent call last): File "/piston/jobs/574143c7-995f-44a0-a56c-d26a7cb37e64/test.py", line 12, in <module> Cal(calories) File "/piston/jobs/574143c7-995f-44a0-a56c-d26a7cb37e64/test.py", line 3, in Cal calories[key]=int(calories[key])ValueError: invalid literal for int() with base 10: 'Day1'
Traceback (most recent call last): File "/piston/jobs/17ed55fb-e658-48c5-a545-f2b7d7c51a47/test.py", line 3, in <module> c=int(input())ValueError: invalid literal for int() with base 10: 'Day1'
Traceback (most recent call last): File "/piston/jobs/39b97eff-97e6-4228-b7d8-6145197c2aa3/test.py", line 10, in <module> Cal(calories) File "/piston/jobs/39b97eff-97e6-4228-b7d8-6145197c2aa3/test.py", line 2, in Cal avg=sum(calories.values())/len(calories)TypeError: unsupported operand type(s) for +: 'int' and 'str'
Traceback (most recent call last): File "/piston/jobs/73cd8974-25a8-40b2-93f5-40ecd87f6049/test.py", line 21, in <module> a.append(int(input()))ValueError: invalid literal for int() with base 10: '1 2 3 4 5 6'
Traceback (most recent call last): File "/piston/jobs/aea0f700-51c4-48f4-b83d-0a1c37ad2b8b/test.py", line 8, in <module> n,m=map(int,input().split())ValueError: invalid literal for int() with base 10: '{{}}'1
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.