A stacktrace would've helped, but presumably the error is:
materials = 1 + (level * 1)
‘level’ is a string, and you can't do arithmetic on strings. Python is a dynamically-typed language, but not a weakly-typed one.
level= raw_input('blah')
try:
level= int(level)
except ValueError:
# user put something non-numeric in, tell them off
In other parts of the program you are using input(), which will evaluate the entered string as Python, so for “1” will give you the number 1.
But! This is super-dangerous — imagine what happens if the user types “os.remove(filename)” instead of a number. Unless the user is only you and you don't care, never use input(). It will be going away in Python 3.0 (raw_input's behaviour will be renamed input).
Answer from bobince on Stack OverflowA stacktrace would've helped, but presumably the error is:
materials = 1 + (level * 1)
‘level’ is a string, and you can't do arithmetic on strings. Python is a dynamically-typed language, but not a weakly-typed one.
level= raw_input('blah')
try:
level= int(level)
except ValueError:
# user put something non-numeric in, tell them off
In other parts of the program you are using input(), which will evaluate the entered string as Python, so for “1” will give you the number 1.
But! This is super-dangerous — imagine what happens if the user types “os.remove(filename)” instead of a number. Unless the user is only you and you don't care, never use input(). It will be going away in Python 3.0 (raw_input's behaviour will be renamed input).
Here is an example of a Type Error and how to fix it:
# Type Error: can only concatenate str (not "int") to str
name = "John"
age = 30
message = "My name is " + name + " and I am " + age + " years old."
# Fix:
message = "My name is " + name + " and I am " + str(age) + " years old."
In the above example, the error message says that we're trying to concatenate a string and an integer which is not possible. So, we need to convert the integer to string using str() function to fix the error.
ValueError and TypeError in python - Stack Overflow
Python - TypeError - Stack Overflow
python - I keep getting a type error and don't know why - Stack Overflow
python - I'm getting a TypeError. How do I fix it? - Stack Overflow
A Value error is
Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value
the float function can take a string, ie float('5'), it's just that the value 'string' in float('string') is an inappropriate (non-convertible) string
On the other hand,
Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError
so you would get a TypeError if you tried float(['5']) because a list can never be converted into a float.
Cite
ValueError a function is called on a value of the correct type, but with an inappropriate value
TypeError : a function is called on a value of an inappropriate type
At the fifth line of giris
kn=an
you set kn to string which is the sequence, python complains about.
You're assigning several of your variables (abes, be, an, kn, and x) to the function float, rather than to an actual floating-point value. Then when you try to multiply this times anything else, you get a TypeError because you can't multiply type objects. If you want to initialize these values outside the function, just set them to 0 or None.
Of course, your specific problem is that you then reassign kn=an after you've assigned an to a string from your input call. That's why Python complains about a "sequence". But like I said, you've got bigger problems than just that.
You had the right idea with this line:
int(float(b))
but that doesn't change b in-place. You have to keep the result. Use this:
b = int(float(b))
Calling int(float(b)) does not change the state of b. After that line b is still a string, while range() expects an integer. I might change that line to b = int(b) to modify b to what you need it to be.
What is a TypeError?
It means exactly what it sounds like: there is an Error that is caused by the Type of one or more of the values in the code.
... but what is a "type"?
In a Python program, every object has a type. By "object" (equivalently in Python, "value") we mean something that can be given a name in the source code. Most names are simple variables: if we write x = 1, then 1 is an object, it has a name x, and its type is int - the type itself has a name.
"Type" means more or less what it sounds like: it tells you what kind of thing something else is. 1, 2 and 3 are all integers; they have the same type, int. You can think of it as representing the concept of an integer.
Not every type has a built-in name. For example, functions are objects (most other languages don't work this way!), and they have a type, but we can't directly refer to that type by name in our code.
Every type does have a representation as an object, however, whether it's named or not. You can use the built-in type to get such a "type object":
>>> type(1) # the result from this...
<class 'int'>
>>> int # is the same:
<class 'int'>
>>> type(int) # We can look a bit deeper:
<class 'type'>
>>> def func():
... pass
>>> type(func) # and get types that aren't named:
<class 'function'>
>>> type(type) # and there's this special case:
<class 'type'>
Notably, the type of type is type itself.
You may notice that Python (3.x) displays these type objects with the word class. This is a useful reminder: when you create a class, you are defining a new type of data. That is the purpose of classes.
What do messages like this mean?
We can break the examples down into a few categories:
TypeError: func() takes 0 positional arguments but 1 was given
TypeError: func() takes from 1 to 2 positional arguments but 3 were given
TypeError: func() got an unexpected keyword argument 'arg'
TypeError: func() missing 1 required positional argument: 'arg'
TypeError: func() missing 1 required keyword-only argument: 'arg'
TypeError: func() got multiple values for argument 'arg'
TypeError: MyClass() takes no arguments
These exceptions are telling you that the arguments (the things you put between the ()) for calling func (or creating an instance of MyClass) are wrong. Either there are too many, not enough, or they are not properly labelled.
This is admittedly a little confusing. We're trying to call a function, and the thing we're calling is a function - so the type does actually match. The identified problem is with the number of arguments. However, Python reports this as a TypeError rather than a ValueError. This might be in order to look more familiar to programmers from other languages such as C++, where "types" are checked at compile time and can be very complex - such that functions that accept different types or numbers of arguments, are themselves considered to have different types.
TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: can only concatenate str (not "int") to str
TypeError: '>' not supported between instances of 'int' and 'str'
TypeError: can't multiply sequence by non-int of type 'float'
TypeError: string indices must be integers
These exceptions are telling you that the left-hand side and right-hand side of an operator (a symbol like + or > or ^, used to compute a result) don't make sense. For example, trying to divide or subtract strings, or repeat a string a non-integer number of times, or (in 3.x) compare strings to numbers. As a special case, you can use + between two strings (or lists, or tuples), but it doesn't "add" them in a mathematical sense. If you try to use + between an integer and a string, the error message will be different depending on the order.
TypeError: %d format: a number is required, not str
TypeError: not all arguments converted during string formatting
These ones are a bit tricky. The % operator is used to get the modulus (remainder when dividing numbers), but it can also be used to format strings by replacing some placeholders. (This is an outdated system that's hard to get right and has weird special cases; in new code, please use f-strings or the .format method.)
An error occurs because the placeholders in the string on the left-hand side don't match up with what's on the right-hand side. In the second case, it's likely that you actually wanted to calculate a modulus, so the left-hand side should be a number (most likely an integer) instead. It's debatable whether these should be ValueErrors instead, since it could be that the contents of the string are wrong. However, Python cannot read your mind.
TypeError: list indices must be integers or slices, not str
This is also a problem with an operator, this time the [] operator (used for indexing into a list, slicing a list, or looking up a key in a dictionary). A string makes sense inside the [] if we are looking up a key in a dictionary that has strings as keys; but we cannot index into a list with it.
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
TypeError: a bytes-like object is required, not 'str'
TypeError: bad operand type for abs(): 'str'
These mean that something wrong was passed to a built-in function (or another callable, such as a type). Functions that you get from a library may raise their own TypeErrors with custom messages. The message should be pretty straightforward.
TypeError: descriptor 'to_bytes' for 'int' objects doesn't apply to a 'str' object
This one is very unusual, and most people who ask this question would never run into it (except maybe with the datetime standard library module). It happens because of trying to use a method as if it were a function, but giving it the wrong type of thing for self: for example, int.to_bytes('1'). The code is wrong because '1' is a string, and strings don't support .to_bytes. Python will not convert the string to integer; and it cannot give an AttributeError instead because to_bytes was looked up in the class, not on the string.
TypeError: 'int' object is not iterable
TypeError: cannot unpack non-iterable int object
TypeError: 'int' object is not callable
TypeError: 'int' object is not subscriptable
These mean exactly what they sound like. "iterable" means "able to be iterated"; i.e., checked repeatedly to get separate values. This happens in for loops, comprehensions and when trying to convert to list etc. The "unpack" variation of the message results from trying to use unpacking syntax on the non-iterable.
"callable" means "able to be called"; to "call" something is to write () after it (possibly with arguments between the ()). Code like 1('test') doesn't make sense because 1 isn't a function (or a type).
"subscriptable" means "able to be subscripted"; here, "subscripting" means either using slice syntax (x[1:2:3]), or indexing or looking for a key (x['test']). We can only do this with sequences (like lists or strings) and mappings (like dicts).
How can I understand and fix the problem?
First, look at the traceback to see where in the code the error occurs. If it's in a library, work backwards to the point where your code uses the library. Then read the error message carefully, and compare it to the code, to figure out what causes the complaint. Finally, think carefully: is the operation wrong, or the values?
Examples
(TODO)
Some non-obvious things
Reusing names
Did you perhaps reassign the name of a built-in callable, such as str or input or list? Did you try to reuse a name for two different things (for example, a function and some global data that it uses)?
Names in Python can only refer to one thing at a time. If you use, say, list as a variable name, then it isn't also the name of "the abstract concept of a list" any more, so you can't use it to create more lists (which includes converting other things to lists). If you create a global variable months with a list of strings, and then write a function months, the function replaces the list, and the function's code can't look up the list. This can easily happen accidentally when using from some_module import * syntax.
Similarly, if you try to make a class that uses the same name for an method as for a data attribute of the instances, that will cause the same problem. (There's also a tricky special case with @staticmethod).
Processing lists
Sometimes people expect to be able to use a list like a Numpy array, and "broadcast" an operation or a function call to each element of the list. That doesn't work. Use a list comprehension instead.
Handling None
Consider whether you need to handle None as a special case. But try to avoid getting into that situation in the first place; "special cases aren't special enough to break the rules", as they say.
Trying to use a library (including a standard library)
If something doesn't work like you'd expect (for example, trying to subtract datetime.times or serialize an instance of a user-defined class as JSON) - rather than trying to treat the problem as a debugging question, search for solutions for what you want that part of the code to do.
If the error mentions a 'str' type and you thought it should be a number
Did you get it from the input function? That gives you a str, even if it looks like a number. Please see How can I read inputs as numbers?.
If the error mentions a 'function' type or 'type' type
Did you forget to call the function, or create an instance of a class?
Error messages about wrong arguments
The error message will tell you the name of the function; so look at the part of the line that calls that function, and check the arguments. Is there a correct number of positional arguments? Is there a keyword argument that must be provided, and is missing? Is there a keyword argument that shouldn't be provided? Is there a positional argument that is also provided by keyword?
If you are writing a method for a class, remember to allow for self. It is necessary for instance methods. If you are calling a method, keep in mind that self will be counted as an argument (both for the amount "required" and the amount "given").
A common beginner error is attempting to use a method (which
isn't a classmethod) from a class without instantiating it. You'd do something like
value = MyClass.method(things)
where you should be doing something like
instance = MyCLass()
value = instance.method(things)
which (obscurely) passes instance as the first (self) argument to method, and things as the second argument.
If you are using a callback that takes arguments from an indirect source, check the source.
If you are trying to create an instance of your own class, and get an TypeError from __init__, make sure that you actually wrote an __init__.
If you don't know what the arguments should be, check the documentation. If the arguments make sense, maybe the function is wrong - make sure you didn't confuse it for another one in the same library.
Error messages about operand types
Make sure the operator is correct for what you want the code to do (for example: ^ is not exponentiation; you want **), and then check the operand types.
In most cases, it will be appropriate to convert the type - but think carefully. Make sure the operation will make sense with the new types. For example, if the code is l + 'second', and l is a list that currently contains ['first'], chances are good we don't want to concatenate strings, but instead create a modified list that also has 'second' as an element. So actually we wanted to "add" another list: l + ['second'].
If string indices must be integers, it could be that the string being indexed is JSON or something of that sort, which should have been parsed already to create a dictionary (possibly with nested lists and dictionaries).
If list indices must be integers or slices, it's likely that the problem is with the list, rather than the index. If you expected the list to be a dict, check whether it contains a dict - especially if it contains exactly one element, which is a dict. Then check if that's the dict that should actually be looked into. If so, the solution is easy: simply add another level of indexing, in order to grab that dict first. This commonly happens when trying to grab data from parsed JSON.
Error messages about string formatting
Seriously, did you intend to do string formatting? If you do want to format a string, consider using f-strings or the .format method - these are easier to debug, and have fewer special cases. But more likely, the left-hand side is some string like '1' that should have been converted to int (or maybe float) first.
Error messages about a "descriptor"
Python's error message here is fairly cryptic - it's using terminology that most programmers rarely if ever have to worry about. But once recognized, the error is very easy to pattern-match. Take special care if the class can be instantiated with no arguments - an empty pair of parentheses () is still necessary to instantiate the class; otherwise, the code refers to the class itself. An instance is required in order to use methods.
Custom error messages from built-in functions
A "bad operand" for a "unary" operator (for example bad operand type for unary +: 'str') can be caused by a stray comma. 'a', + 'b' is not the same as 'a' + 'b'; it is trying to use + as a unary operator on the 'b' string, and then make a tuple. (You know how you can write e.g. -1 to get a negative number? The - there is a unary operator. It turns out you can similarly write +1; it means the same as 1, of course.)
Especially if you have had to migrate code from 2.x to 3.x, be very careful about the distinction between bytes and str types in 3.x. bytes represents raw data; str represents text. These are fundamentally different and unrelated things, and it is only possible to convert from one to the other by using an encoding. In Python 3.x, files that are opened in a binary mode (using 'b' in the mode string) produce bytes when read, and must be given something compatible with bytes when written to. str does not qualify; you must specify an encoding explicitly. The canonical for this problem is "TypeError: a bytes-like object is required, not 'str'" when handling file content in Python 3.
Error messages where something "is not" usable in some way
Did you want to use it that way?
Python can't read your intent. For example, accessing an element of a list is done using [], not (). If the code says () instead, that will be interpreted as an attempt to call the list, so the error message will complain that the list is not callable.
Not iterable
When something is not iterable, the problem is very likely with the thing, rather than the iteration. If you want a for loop to run a specific number of times, you still need something to iterate over; a range is the usual choice. The same is true if you are using a list comprehension etc. to make multiple copies of a value. If you have an integer x and you want to make a list with one item, which is that integer, that is spelled [x], not list(x).
It's especially common to see 'NoneType' object is not iterable. There is exactly one 'NoneType' object: the special value None - Python forbids creating any more instances of that class. Python methods that work in-place - especially list methods - generally return None rather than the list that was modified. See also TypeError: 'NoneType' object is not iterable in Python.
Not callable
If a 'module' object is not callable, it's most likely because you want a function or class from the module, that has the same name as the module, rather than the module itself. The linked example is for the socket standard library; other common cases include datetime and random.
Also make sure that the code doesn't call a function and remember the result, instead of remembering the function itself. This is a common problem with APIs that expect a "callback" function. (If you need to choose the arguments ahead of time, but not actually call the function, see How can I bind arguments to a function in Python? .) Sometimes people also try to provide the name of a function as a string, rather than providing the function itself.
Beginners sometimes expect to be able to do "implicit multiplication" in a mathematical formula, the way that it works in math class. In a Python program (like other popular languages), code like a(b + c) does not multiply the integer a by the result of b + c; it attempts to call a as if it were a function. See Why do I get "TypeError: 'int' object is not callable" from code like "5(side_length**2)"?.
Not subscriptable
Sometimes, people try to get "digits" from a number by indexing into it as if it were a string. int and float values aren't strings; they don't have digits in them. So this will cause a "is not subscriptable" TypeError. The numeric value is the same no matter what base you write them in, and there are other ways to write a number besides base ten; so it is your responsibility to create the appropriate string first.
If you are trying to work with nested lists, be careful about indexing into them. A list like example = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] should be indexed like example[i][j], not e.g. example[i[j]]. The logic here should be pretty simple: the correct code means to index into example (getting a list of integers), and then index into that result. The incorrect code means to use j as an index into i first, because of how the brackets are nested.
If you are trying to call a function or use a class (such as the built-in range), remember that this uses parentheses, not square brackets:
# WRONG
range[10]
# RIGHT
range(10)
What are you trying to achieve? python2 and python3 both throw 'TypeError', which means the caller is misusing the function 'fun'
def foo(a, b, c = None):
pass
try:
foo(1)
except TypeError:
print("TypeError")
Calling it for python2:
python2 code_for_exception_type_error.py
TypeError
And python3:
python3 code_for_exception_type_error.py
TypeError
The reason you are getting this error is because you are not passing a value for the parameter 'b'. If you want to not pass a value for 'b', do the following:
def fun(a, b = None, c = None):
pass
fun(1)
If you want to pass a value for the parameter b, do the following:
def fun(a, b, c = None):
pass
bValue = None # whatever you want to pass for b
fun(1, bValue)
Because sqrt is a float and range expects strictly integers.
You probably want this:
for b in range(3, int(sqrt) + 1, 2):
sqrt is of type float and therefore can't be used with range():
>>> range(1, 2.0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer
To fix, convert it to integer:
sqrt = int(a ** 0.5)
Although a bit more research could've probably solved this question:
You are trying to divide two numbers (I guess) but they are formatted as str. Try to convert them to something that is divisible, like int or float, simple example:
**income_statement['as_%_of_revenue'] = int(income_statement) / int(income_statement.iloc[0]**)
Update
It seems you are trying to divide a value from a row in a pandas Dataframe by another number from that row. If you have the Dataframe
df = pd.DataFrame(data=([100, 200], [500, 500])):
0 1
0 100 200
1 500 500
You can add a column containing the percentage of first over second column like this:
df['as_%'] = (df[0] / df[1])*100. This results in:
0 1 as_%
0 100 200 50.0
1 500 500 100.0
this line was missing
net_income = IS[0]['netIncome']
thats why the question "what i want to achieve with the last row was unclear". Hence the whole code is:
net_income = IS[0]['netIncome']
BS = requests.get(f'https://financialmodelingprep.com/api/v3/balance-sheet-statement/{company}?apikey={demo}').json()
# get income statement as % of revenue for future predictions and forecast 5 next IS years
income_statement = pd.DataFrame.from_dict(IS[0], orient='index')
income_statement = income_statement[5:26]
income_statement.columns = ['current_year']
income_statement['as_%_of_revenue'] = income_statement /income_statement.iloc[0]
What i want to achieve with this is to divide the income_statement by the revenues(sales) = income/revenue (both current year). The result shall give me a Percentage of Revenue to Forecast the next Years, its a Multiplicator.
The Data before dividing it looks like this.
The problem is that income_statement is defined as a class because of the dataframe
<class 'pandas.core.frame.DataFrame'>
current_year
period FY
revenue 495308000
costOfRevenue 43839000
grossProfit 451469000
grossProfitRatio 0.911491
researchAndDevelopmentExpenses 101117000
generalAndAdministrativeExpenses 101439000
sellingAndMarketingExpenses 252820000
otherExpenses 0
operatingExpenses 455376000
costAndExpenses 499215000
interestExpense 38119000
depreciationAndAmortization 12101000
ebitda 8.194e+06
ebitdaratio 0.0165432
operatingIncome -3.907e+06
operatingIncomeRatio -0.00788802
totalOtherIncomeExpensesNet 14382000
incomeBeforeTax -27645000
incomeBeforeTaxRatio -0.0558138
incomeTaxExpense 0
Im kind of new with python (3 months) so im not sure even of all easy problems..i guess
Thanks for your help
UPDATE
i just did it like this now (even will cost me more time)
i declared also revenue
net_income = IS[0]['netIncome']
revenue = IS[0]["revenue"]
and calculated like this
income_statement['as_%_of_revenue'] = (net_income/revenue)*100
looking forward if this causes any further problems what i guess
Data looks like this now
current_year as_%_of_revenue
period FY -4.920978
revenue 495308000 -4.920978
costOfRevenue 43839000 -4.920978
grossProfit 451469000 -4.920978
grossProfitRatio 0.911491 -4.920978
researchAndDevelopmentExpenses 101117000 -4.920978
generalAndAdministrativeExpenses 101439000 -4.920978
sellingAndMarketingExpenses 252820000 -4.920978
otherExpenses 0 -4.920978
operatingExpenses 455376000 -4.920978
costAndExpenses 499215000 -4.920978
interestExpense 38119000 -4.920978
depreciationAndAmortization 12101000 -4.920978
ebitda 8.194e+06 -4.920978
ebitdaratio 0.0165432 -4.920978
operatingIncome -3.907e+06 -4.920978
operatingIncomeRatio -0.00788802 -4.920978
totalOtherIncomeExpensesNet 14382000 -4.920978
incomeBeforeTax -27645000 -4.920978
incomeBeforeTaxRatio -0.0558138 -4.920978
incomeTaxExpense 0 -4.920978
netIncome -24374000 -4.920978
Just as the error message says, you can't modify a string.
One approach, which will let you keep most of the code you've written, is to convert your string to a list of characters:
word = list(word)
Then join it back into a sting when you're done:
word = "".join(word)
Another approach is to build up a new string and conditionally choose which character is concatenated to it:
result = ""
for c in word:
if c = " ":
result += space # should be named slash
else:
result += c
But a far better solution is to replace all that code with a single line:
word = word.replace(" ", "/")
Strings are in python immutable, which means, their elements cannot be changed or rearranged.
Try creating a new string and put your characters inside it.
You can check that the arguments are floats with type().
For example if type(start) != float
Or you can use isinstance() function like that:
if not isinstance(start, float):
raise TypeError
There are several ways, the most common is probably: EAFP (easier to ask forgivness than permission):
def m(start:float, up:float=1, down:float=1):
try:
if start <= 0.0:
raise ValueError
if up < 0.0:
raise ValueError
if down < 0.0:
raise ValueError
except TypeError:
# This is just to give a more meaningful error message.
raise TypeError('Arguments must be numbers.')
this will silently fail for python 2 but in python 3 it will raise a TypeError when comparing a not-number to a number. In that sense it will make him aware that he has done something wrong.
The other option is type-checks and the concept of LBYL (look before you leap) also known as defensive programming:
from numbers import Real # or Number if you want to allow compley-types too
def m(start, up=1, down=1):
# Compare if each argument is a real (floating point) number:
if not isinstance(start, Real) or not isinstance(up, Real) or not isinstance(down, Real) :
raise TypeError("Arguments must be numbers.")
...
Most people (including myself) think EAFP with try ... except ... is more pythonic but if you feel more comfortable with isinstance-checks then these provide an alternative.
You can use a try-except to catch the error, and throw for example another one:
def prefill(n,v):
try:
n = int(n)
except ValueError:
raise TypeError("{0} is invalid".format(n))
else:
return [v] * n
For example:
>>> prefill(3,1)
[1, 1, 1]
>>> prefill(2,"abc")
['abc', 'abc']
>>> prefill("1", 1)
[1]
>>> prefill(3, prefill(2,'2d'))
[['2d', '2d'], ['2d', '2d'], ['2d', '2d']]
>>> prefill("xyz", 1)
Traceback (most recent call last):
File "<stdin>", line 1, in prefill
ValueError: invalid literal for int() with base 10: 'xyz'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in prefill
TypeError: xyz is invalid
You do not per se need to specify an exception to raise. In case you want to re-raise the exception, simply writing raise is sufficient. You can furthermore specify a tuple of exceptions to catch, and make v optional here, for example:
def prefill(n,v=None):
try:
n = int(n)
except (TypeError, ValueError):
raise TypeError("{0} is invalid".format(n))
else:
return [v] * n Use the raise keyword to raise an exception, rather than return it. raise is used to generate a new exception:
>>> raise TypeError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError
In this example I raised the TypeError by using the exception class directly. You can also create instances of the error like this:
>>> t=TypeError()
>>> raise t
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError
Doing it that way allows various properties to be set on the object before using raise. The problem posted includes this requirement:
When throwing a TypeError, the message should be n is invalid, where you replace n for the actual value passed to the function.
That is an example of a situation where it is necessary to create an instance of the error and set a message property on it before using raise. That can still be done in one line:
>>> raise TypeError("foo")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo
To catch exceptions, use a try-except block, rather than an if block:
x = 'hello, world!'
try:
y = x / 2
except TypeError as e:
e.args = (*e.args, str(x) + " is not valid")
raise
This will raise an error:
TypeError: ("unsupported operand type(s) for /: 'str' and 'int'", 'hello, world! is not valid')
note that you can check the datatype of a variable using type():
>>> x = 5
>>> type(x)
<class 'int'>
>>> if type(x) == int:
... print("it's an int")
...
it's an int
Also, the code sample could be simplified to:
return [v for _ in range(n)]
import sys
sys.setrecursionlimit(10**8)
def ackermann(m,n):
if m == 0:
return (n + 1)
elif n == 0:
return ackermann(m - 1, 1)
else:
return ackermann(m - 1, ackermann(m, n - 1))
for x in range(5):
for y in range(5):
print(ackermann(x, y))
Python's default recursion limit is 10**4. Changable with setrecursionlimit() so you can get a 'Stack overflow' error because Ackermann function's output is too long.
I like to use
def test(n):
if n == 1:
return 1
else:
return test(n-1)*n
to test max recursion. It will throw a 'RecursionError: maximum recursion depth exceeded in comparison' if n is to large.