+ 3

Problem with loop and .txt

I'm trying to create a program that executes commands within a loop from time to time. I wanted to create this alternation by using a .txt file, in which a number (number 1) is continuously written. So when the number is written a total of times (45 or 60 for example), the code performed a function, like this: if count.read == "1" * (45 or 60):     print ( "hello") I noticed that, however, it was printed "hello" only at the achievement of 45 numbers 1 written. I thought I was wrong writing "1" * (45 or 60) so I wrote if count.read == "1" * 45:     print ( "hello") if count.read == "1" * 60:     print ( "hello") But still I print bye only at 45 and not at 60. Below is a complete example of what I've tried: import os localpath = (os.path.join (os.environ ['USERPROFILE'], "Documents \\ AppNanaBot")) def write_count_a (string):     count = open (localpath + "\\ count.txt", "a")     count.write (string) def write_count_w (string):     count = open (localpath + "\\ count.txt", "w")     count.write (string) def into_the_breach ():     print ( "hello") write_count_w ( "") while True:     write_count_a ( "1")     count = open (localpath + "\\ count.txt", "r")     if count.read () == "1" * 45:         into_the_breach ()     if count.read () == "1" * 60:         into_the_breach ()     print ( ".....")

9th Jan 2019, 4:12 PM
Giordano Fratti
Giordano Fratti - avatar
6 Answers
+ 6
I don't fully understand what you're trying to do, but "a or b" always equals a, unless a is 0. If a is 0, it equals b, unless b is 0. If both a and b are 0, it equals 0 (or False). => 'iiii' == (4 or 5) * 'i' => True => 'iiii' == (5 or 4) * 'i' => False => 'iiii' == 5 * 'i' or 'iiii' == 4 * 'i' => True => "45 or 60" = 45. So "1" * (45 or 60) will always be 45 x "i", never 60 times. If you call count.read() two times on the same file, you'll probably have to jump back to the beginning of the file after the first read(), using seek(0). Otherwise you try to read beginnig from the end of the file and you'll just end up with an empty string. In this case the comparison with 60 * 'i' will of course be False.
9th Jan 2019, 6:03 PM
Anna
Anna - avatar
+ 7
Aww. You're welcome ☺️ Good luck on your learning journey. If you have questions, feel free to ask 👌
10th Jan 2019, 3:51 PM
Anna
Anna - avatar
+ 5
Thanks Anna and HonFu, I have resolved putting count.seek(0) between every if condiction :))) I'm so glad of our SoloLearn's community because I'm only 15yo, and I don't do Informatic Language at School, so I have to learn by myself and also I have a lot of gaps for this reason. In fact for example, I know nothing about read.seek(). So I apprecciate a lot, thanks guys.
10th Jan 2019, 3:37 PM
Giordano Fratti
Giordano Fratti - avatar
+ 3
Can you solve your problem with modulo? Pseudocode: if index(or whatever you are counting) % 45 == 0: ... EDIT: After Anna's post I think I may have misunderstood the problem. I'll watch where this goes...
9th Jan 2019, 5:41 PM
HonFu
HonFu - avatar
+ 2
I would assign count.read() to a variable and then use if variable == "1"*45 or variable == "1"*60: print("hello")
11th Jan 2019, 9:35 AM
Seb TheS
Seb TheS - avatar
+ 2
or is an logical operator, which takes the first non-zero value or the last zero value.
11th Jan 2019, 5:15 PM
Seb TheS
Seb TheS - avatar