osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

(no subject)


On 1/4/20 2:29 PM, William Johnsson wrote:
> Hello! My name is William and im 14 years old and live in sweden.  Im pretty new to programing in python and i need some help with code, (That?s why i?m here). But i couldn?t really find what i was searching for on the internet. I?m trying to write code that can check only the first line in a .txt file and check if it?s the same as the int 1000.   I preferably want it to be an if satement but anything works, i just don?t know how i should write it. I?ve been browsing the internet for a solution but sadly can?t seem to find a solution to it.
> 
> I don?t really know if this is the right way to get help or even if this email is for something else. Hopefully i will get some kind of response.
> 
> Best regards, William
> 

It won't be; it can't be.  A text file contains only text.  The line you read 
from it might be the string '1000' (or more likely '1000\n', which is 4 digits 
followed by a newline character), but it cannot be the int 1000 because data has 
types.

str(1000) gives you '1000', because ints can be converted to strings. 
int('1000') gives you 1000 because and strings consisting only of digits can be 
converted to ints.  So the two types are convertible into one another.  But 
that's a thing you need to do explicitly.  If x is a thing you just read from a 
text file then

if x == 1000:

can never be true.