+ 2
Problem reading strings
Can anyone explain to me why the script doesn't print "Hello World", but continues to write an infinite number of "1" in the .txt? If necessary, correct my code directly. Thanks in advice :))) def write_count(string): count = open("count.txt", "a") count.write(string) while True: write_count("1") count = open("count.txt", "r") if count.read() == "11111": print("Hello World")
6 Réponses
+ 3
While True: -->cause of infinite loop plus no break statement
+ 1
First of all, thanks for your patience
- anyway, I was wrong writing if count.read () == "11111": "hello world" instead of if count.read () == "11111": print ("hello world"), now I have corrected, but That's not the point
- even using "r +" instead of "a" and chased "w", the loop never prints "hello world"
- I do not want the loop to end when the .txt is equal to "11111", I wanted it to print "Hello World": it's the same thing, it's about changing the function after if count.read () == " 11111 ": The problem is that if count.read () ==" 11111 ": does nothing
+ 1
I want that when the .txt is egual to "11111", it does something that can be: print("hello world") or break (to stop the loop). But it does nothing, it continue to print in the .txt 11111111111111111111111 whithout printing or doing something when it passes throught "11111"
+ 1
It's very strange beacuse when i try to run the following code:
def write_count(string):
count = open("count.txt", "r+")
count.write(string)
while True:
write_count("1")
count = open("count.txt", "r+")
if count.read() == "11111":
print("Hello World")
it doesn't print "Hello World" on the screen and when i check that it print 111111.... on the .txt file, i see that it does print nothing
maybe something is wrong with writing permissions? (the file is on the desktop)
I don't understand why it does so
+ 1
:((( really i don't know because now:
def write_count(string):
count = open("C:\\Users\\user_name\\Desktop\\count.txt", "r+")
count.write(string)
for i in range(10):
write_count("1")
count = open("C:\\Users\\user_name\\Desktop\\count.txt", "r+")
if count.read() == "11111":
print("Hello World")
it prints only "1" on the .txt and so obviously it doesn't print "Hello World" on screen
+ 1
No problem dude, we are here to learn :)))