. Advertisement .
..3..
. Advertisement .
..4..
The int() function in Python couldn’t convert every data type into integers. It might give you an error like this “TypeError: int() argument must be a string, a bytes-like object or a number, not ‘list'”. This guide will show you how to solve this problem.
TypeError: int() argument must be a string, a bytes-like object or a number, not ‘list’
Let’s say you want to write a number guessing game. The player gives a range from which the program will pick a random number.
The player then can enter a guess, one at a time. The program compares it with the true number and gives them a hint. When the number is correctly found, the game ends and prints the number of guesses the player has used.
This is an implementation, which uses str.split() method to get the range from the user. At first glance, there is no problem with this program. Suppose we want to play with the (1, 10) range.
import random
range1, range2 = int(input("Enter the range: ").split(", "))
random_num = random.randint(range1, range2)
guess = int(input("Enter your guess: "))
i = 1
while guess != random_num:
if range1 < guess < random_num:
print("Wrong. Hint: pick a bigger number!")
elif random_num < guess < range2:
print("Wrong. Hint: pick a smaller number!")
else:
print("Enter a guess between", range1, "and", range2)
i += 1
guess = int(input("Enter your guess: "))
print("Correct! Your number of guesses is", i)
Output:
Enter the range: 1, 10
Traceback (most recent call last):
File "int_argument.py", line 5, in <module>
range1, range2 = int(input("Enter the range in which the number should be in: ").split())
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
As you can see, the program has thrown out a TypeError exception. To understand this error, we should look into the int() function.
Per Python’s official documentation, int() only accepts a number or string value, a bytes literal, or a bytearray instance. We have seen its capabilities when using it to convert a binary to int in Python.
However, you can’t expect this function to convert a list, even when its elements are numbers or strings. Remember that the str.split() method returns a list that contains the numbers in the input. To solve this, you must find a way to give the int() function only arguments of the correct type.
One solution is to convert each number with the int() function separately.
import random
input1, input2 = input("Enter the range: ").split(", ")
range1 = int(input1)
range2 = int(input2)
random_num = random.randint(range1, range2)
guess = int(input("Enter your guess: "))
i = 1
while guess != random_num:
if range1 < guess < random_num:
print("Wrong. Hint: pick a bigger number!")
elif random_num < guess < range2:
print("Wrong. Hint: pick a smaller number!")
else:
print("Enter a guess between", range1, "and", range2)
i += 1
guess = int(input("Enter your guess: "))
print("Correct! Your number of guesses is", i)
The program now should run without a problem as the int() function now only has to convert two strings.
Conclusion
If you see this error message “TypeError: int() argument must be a string, a bytes-like object or a number, not ‘list'”, it means you have provided the int() function a list, which is a wrong argument type. Change it to a number or string instead, and your code will no longer end up with this error.
Leave a comment