I'm getting an invalid literal for int() with base 10 where it seems as if the input stream is concatenating values generated in a loop.
def collectData():
lst = makeCases()
cases = int(input())
for i in range(cases):
size = int(input())
case = []
for j in range(size):
value = int(input())
case +=[value]
insert(lst,case)
return lst
That is the function generating the issue. The value and size variables are the problem as they seem to concatenate subsequent values before the conversion.
The makeCases() function generates a tuple on the form ("Case",[]).
EDIT: The code works fine locally but won't work in the hackerrank IDE
As Tom Karzes has said, input() is returning something other than a number. Looking at the error message that is produced by Python, I assume you are using Python 3 but I will cover Python 2 too.
Python 2
input() checks if the given input from stdin is something it can evaluate to. It is equivalent to eval(raw_input()). (Pydoc)
>>> a = input()
123
>>> a
123
>>> abc = 3
>>> a = input()
abc
>>> a
3
It produces a different error when you input() something it cannot eval:
>>> a = input()
abc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'abc' is not defined
Since it performs eval on the given input, it will assume that abc may be a variable name. It searches for it but throws an error because it couldn't find it.
Python 3
input() is the new raw_input() in Python 2. input() returns a string even if the given input is a number. (Pydoc)
>>> a = input()
123
>>> a
'123'
This is the most likely reason you would convert it to an int() after you get an input().
I can replicate your error message as so (in Python 3):
>>> a = int(input())
abc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'abc'
>>> a = int(input())
123 123
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '123 123'
In conclusion, the most likely reason why it is returning such an error is because you are giving it something it cannot convert to an int.
Related
This question already has answers here:
Python Error: "ValueError: need more than 1 value to unpack"
(8 answers)
Closed 6 years ago.
This is my program
from sys import argv
script, first, second, third = argv
print ("the script is:", script)
print("the first variable is:", first)
print("the second variable is:", second)
print ("the third variable is:", third)
The error is:
Traceback (most recent call last):
File "C:/Users/ravikishore/PycharmProjects/Test/.idea/MyPython.py", line 2, in <module>
[script, first, second, third] = argv
ValueError: need more than 1 value to unpack
argv is a list:
>>> from sys import argv
>>> type(argv)
<type 'list'>
So you're attempting to do a conversion from a list to a tuple, which only works if the number of elements in the tuple exactly matches the list length:
>>> a,b,c = [1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack
>>> a,b,c = [1,2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
>>> a,b,c = [1,2,3]
>>> a,b,c = [1,2,3,4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
So you need to add some checks on the argv length prior to attempting the conversion.
That code works just fine, assuming that you actually give it three arguments to unpack, as with:
c:\pax> python yourProg.py A B C
the script is: yourProg.py
the first variable is: A
the second variable is: B
the third variable is: C
The problem occurs when you don't give it enough arguments:
c:\pax> python yourProg.py A
Traceback (most recent call last):
File "yourProg.py", line 2, in <module>
script, first, second, third = argv
ValueError: not enough values to unpack (expected 4, got 2)
If you want to ensure there are enough arguments before trying to unpack them, you can use len(argv) to get the argument count, and compare that to what you need, something like:
import sys
if len(sys.argv) != 4:
print("Need three arguments after script name")
sys.exit(1)
script, first, second, third = sys.argv
print ("the script is:", script)
print("the first variable is:", first)
print("the second variable is:", second)
print ("the third variable is:", third)
In the code below, int(x) throws an exception. I understand that x should be a string but -numeric or non-numeric string?
def temp_convert(var):
try:
return int(var)
except ValueError, Argument:
print "The argument does not contain numbers\n", Argument
# Call above function here.
temp_convert("xyz")
The string you supply as the function argument has to be representable as an integer. What would you consider the numerical representation of "xyz" to be?
If you pass the function string representations of numbers, positive or negative, then you won't trigger the exception.
When numbers are encoded as strings there are no problems,
>>> int("10")
10
>>> int("-10")
-10
When symbols that aren't readily represented by a number is supplied to the function the exception will triggered,
>>> int("-10a")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '-10a'
int(x) does not accept floating-point numbers either:
>>> int("10.0")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '10.0'
Here is the complete code. What's confusing me is that I'm using int to turn the ceiling of l/2 into an integer and it's still not working. The error would have to be elsewhere in the code unless... well I dun goofd. Thanks
def switcheroo(vec):
from math import ceil
from __future__ import division
l = len(vec) - 1
holdingcell = []
for i in range(int(ceil(l/2))):
holdingcell = vec[i]
vec[i] = vec[l-i+1]
vec[l-i+1] = holdingcell
return vec
The error I am getting is:
>>> o = switcheroo(v)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "switcheroo.py", line 8, in switcheroo
for i in range(int(ceil(l/2))):
TypeError: range() integer end argument expected, got float.
In python 2.7, if we take an integer as input, which one is more faster and efficient or there is no difference at all:
input() or int(raw_input())
From the Python docs
input([prompt])
Equivalent to eval(raw_input(prompt)).
This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.
If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.
Consider using the raw_input() function for general input from users.
int(raw_input()) will be faster, more secure and produce less confusing results.
Consider:
>>> b = 5
>>> a = input()
[1, 2, 3]
>>> a + b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "int") to list
>>> a = int(raw_input())
[1, 2, 3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '[1, 2, 3]'
The ValueError raised when reading input is far more desirable than the TypeError raised when using the variable.
In Python it is possible to call either del x or del (x) . I know how to define a function called F(x) , but I do not know how to define a function that cal be called like del, without a tuple as parameters.
What is the difference between F x and F(x), and how can I define a function that can be called without parenthesis ?
>>> a = 10
>>> a
10
>>> del a <------------ can be called without parenthesis
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> a = 1
>>> del (a)
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> def f(x): 1
...
>>> f (10)
>>> print f (10)
None
>>> def f(x): return 1
...
>>> print f (10)
1
>>> f 1 <------ cannot be called so
File "<stdin>", line 1
f 1
^
SyntaxError: invalid syntax
>>>
The main reason is that del is actually a statement and therefore has special behavior in Python. Therefore you cannot actually define these (and this behavior) yourself* - it is a built-in part of the language for a set of reserved keywords.
**I guess you could potentially edit the source of Python itself and build your own in, but I don't think that is what you're after :)*