Common error messages
This page contains information about the most common error messages you may come across when completing the programming exercises on this course.
My printout looks identical to the example in the instructions, but my submission still fails
Make sure your program doesn't print any extra whitespace, such as space characters. Notice that the default behaviour of the print function is to add a space between any strings separated with a comma.
SyntaxError: bad input on line [line number]
This error message usually appears when there is a typo in your code which the Python interpreter finds hard to classify more specifically. For example, there may be a colon character missing from the end of an if statement, or a keyword, such as while , may have been misspelled. The only way to fix this problem is to inspect the line indicated in the error message.
If the line indicated in the error message looks correct, the error may often be one line above or below the line indicated, so check around the issue, too.
NB: The programming exercises in the early parts of this course use a framework called Skulpt to run Python code in the browser. Skulpt is quite limited compared to a regular Python interpreter, and thus the error messages printed out are often less informative. For example, the bad input on line error message may refer to many different programming errors, and it is difficult to find out the true cause of the error just based on this message.
SyntaxError: unindent does not match any outer indentation level on line [line number]
Your code is indented incorrectly at the line indicated in the error message. For example, all lines within an if block must be indented the same. To fix this error, indent all lines within a block of code with the exact same amount of whitespace. The following code would cause this error:
NameError: name [variable name] is not defined on line [line number]
You are trying to refer to a variable or object which does not exist at that specific point in your program. It may be that the variable has not yet been assigned a value, or there is a typo in the variable name. It may also be the case that you have defined a variable inside a function, and are trying to refer to that same variable outside the function.
TypeError: unsupported operand type(s) for Add: 'int' and 'str' on line [line number]
You may be trying to add an integer and a string together, without first converting the string into an integer value. Strings can be converted into integers with the int() function. A similar error message may appear if you try to perform other arithmetic operations, such as division or subtraction, on strings.
It may also be the case that you are trying to create a new string by combining a string and an integer. You should first convert the integer into a string with the str() function.
Syntax error: bad input — Python
I want to find the sum of the even numbers in the num array. Can anyone suggest what I did wrong here ?
2 Answers 2
Indentation is very important in python
Also, use consistent indentation e.g 4 spaces every where you have to introduce indentation.
Alternatively:
Sum( ) will return the summation of a list.
[i for i in num if i % 2 == 0] is a List Comprehensions.
For example:
![]()
-
Featured on Meta
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.9.6.43612
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
SyntaxError: bad input on line 5
SyntaxError: bad input at <unknown>
A=int(input(‘введите количество часов’)) B=int(input(‘введите количество часов’)).
Uncaught SyntaxError: Unexpected end of input
Вот js пишет,что ошибка в последней строке,помогите: jQuery(document).ready(function( $ ) < .
Uncaught SyntaxError: Unexpected end of input
вот этот код <script type="text/javascript" src="./jquery.min.js"></script> <script.
Сообщение от makardasdasd
Uncaught SyntaxError: Unexpected end of JSON input
json не хочет возвращать значения из бд мой php: <?php $servername ="localhost";.
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
Всем привет, пытаюсь произвести запрос к обработчику c помощью AJAX, выдает ошибку при парсинге.
Код 200 SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Ребята подскажите плиз, пытаюсь добавить форму отправки сообщений на сайт. После отправки выдает.
Постоянная ошибка SyntaxError: JSON.parse: unterminated string at line 1 column 132829 of the JSON data
что бы не делал, нормально в консоле JSON не выводит, нужно раз 20 обновить только тогда выведет.
SyntaxError: unexpected character after line continuation character
Доброго времени суток! Сегодня мне встретилась очень неожиданная ошибка "unexpected character after.
Dive Into Python
It is not enough to test that functions succeed when given good input; you must also test that they fail when given bad input. And not just any sort of failure; they must fail in the way you expect.
- toRoman should fail when given an integer outside the range 1 to 3999.
- toRoman should fail when given a non-integer number.
In Python , functions indicate failure by raising exceptions, and the unittest module provides methods for testing whether a function raises a particular exception when given bad input.
Example 13.3. Testing bad input to toRoman
| The TestCase class of the unittest provides the assertRaises method, which takes the following arguments: the exception you’re expecting, the function you’re testing, and the arguments you’re passing that function. (If the function you’re testing takes more than one argument, pass them all to assertRaises, in order, and it will pass them right along to the function you’re testing.) Pay close attention to what you’re doing here: instead of calling toRoman directly and manually checking that it raises a particular exception (by wrapping it in a try. except block), assertRaises has encapsulated all of that for us. All you do is give it the exception (roman.OutOfRangeError), the function (toRoman), and toRoman‘s arguments (4000), and assertRaises takes care of calling toRoman and checking to make sure that it raises roman.OutOfRangeError. (Also note that you’re passing the toRoman function itself as an argument; you’re not calling it, and you’re not passing the name of it as a string. Have I mentioned recently how handy it is that everything in Python is an object, including functions and exceptions?) | |
| Along with testing numbers that are too large, you need to test numbers that are too small. Remember, Roman numerals cannot express 0 or negative numbers, so you have a test case for each of those (testZero and testNegative). In testZero, you are testing that toRoman raises a roman.OutOfRangeError exception when called with 0; if it does not raise a roman.OutOfRangeError (either because it returns an actual value, or because it raises some other exception), this test is considered failed. | |
| Requirement #3 specifies that toRoman cannot accept a non-integer number, so here you test to make sure that toRoman raises a roman.NotIntegerError exception when called with 0.5. If toRoman does not raise a roman.NotIntegerError, this test is considered failed. |
The next two requirements are similar to the first three, except they apply to fromRoman instead of toRoman:
- fromRoman should take a valid Roman numeral and return the number that it represents.
- fromRoman should fail when given an invalid Roman numeral.
Requirement #4 is handled in the same way as requirement #1, iterating through a sampling of known values and testing each in turn. Requirement #5 is handled in the same way as requirements #2 and #3, by testing a series of bad inputs and making sure fromRoman raises the appropriate exception.