I am creating a program that reads a file and if the first line of the file is not blank, it reads the next four lines. Calculations are performed on those lines and then t
The following are totally acceptable in python:
int
float
float
int
float
But you get a ValueError
if you pass a string representation of a float into int
, or a string representation of anything but an integer (including empty string). If you do want to pass a string representation of a float to an int
, as @katyhuff points out above, you can convert to a float first, then to an integer:
>>> int('5')
5
>>> float('5.0')
5.0
>>> float('5')
5.0
>>> int(5.0)
5
>>> float(5)
5.0
>>> int('5.0')
Traceback (most recent call last):
File "", line 1, in
ValueError: invalid literal for int() with base 10: '5.0'
>>> int(float('5.0'))
5