Re: [Tutor] Problem with 'IF' condition
Please always reply all to include the tutor list On 04/12/17 14:29, a.ajm...@incycleautomation.com wrote: > Thank you so much for your quick reply. > I don't think there are any white space errors. Don't guess, prove it. To do that you *must* use repr() otherwise print() will hide the whitespace. Also do you have a valid reason for reading the first file then overwriting it? It seems like wasted effort unless you plan onm doing something with the original content at some point in the future? > > However, Whatever you understood is exactly same what I tried to > explain in my email. > In this email I have attached output screen shot. > abb and nao contain exactly same value(you can see in screen shot, as > I print values). > But, I don't understand as it is printing same values(in abb and nao); > it should give output "positions are same". > But it doesnot. > > Please let me know if you find any errors. > > Best Regards, > Achyut Ajmera > > > > ----- Original Message - > Subject: Re: [Tutor] Problem with 'IF' condition > From: "Alan Gauld via Tutor" > Date: 12/1/17 12:33 pm > To: tutor@python.org > > On 01/12/17 14:02, a.ajm...@incycleautomation.com wrote: > > > - If you see in my code, I'm writing to "test1.txt" and saving > that value in "nao" as well. > > On the other side, I'm reading from "test3.txt" and saving that > value in "abb" just like above. > > > > Now, my goal is to compare these two variables nao & abb. As it > written below. > > However, result never gives me "true" when actually this both > variable contain same values. > > Are you absolutely sure they are the same? Have you checked, for > example > for newline characters or other whitespeace? > You could do that by printing the repr() of the values: > > print repr(nao), repr(abb) > > Now simplifying your code slightly: > > > x = "C:/FTP_Folder/test1.txt" > > f = open(x) > > r = f.read(500) > > f.close() > > > f1 = open(x,'w') > > z = f1.write("1") > > f1.close() > > Note that this will have deleted everything in x > and replaced it with "1" > > > f = open(x) > > r0 = f.read(500) > > r0 will now contain '1' > > > nao = r0 > > As will nao > > > f.close() > > > y = "C:/FTP_Folder/test3.txt" > > f2 = open(y) > > r1 = f2.read(500) > > abb = r1 > > abb now contains whatever was in test3.txt. > Did it contain '1'? > > > if nao == abb: > > print("Positions are same") > > print r, r1 > > r contains the original contents of test1 > r1 contains the content of test3 > > > else: > > print("not same") > > print nao, abb > > whereas nao contains '1' and abb contains the > same as r1 > > Is my interpretation what you see, and is it what > you expect? > > In future with thee kinds of issues its good > to include an actual cut n paste of the program > output. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with 'IF' condition
On Fri, Dec 01, 2017 at 07:02:47AM -0700, a.ajm...@incycleautomation.com wrote: > I copied my program as plain text below, Unfortunately you didn't, as the text you quote below will not run as Python code due to indentation errors. So you have (accidentally, I trust) messed up the indentation. Please take more care to copy and paste accurately, without adding extra spaces at the start of lines. > tts.say("Hi") Since tts is not defined, this fails immediately with a NameError. How is this line tts.say() relevant to your problem? It just adds extra code, and makes it impossible for us to run your code. There is no need to show us irrelevant code. > x = "C:/FTP_Folder/test1.txt" > f = open(x) > r = f.read(500) > tts.say("Current position number in the text file is") > tts.say(r) > f.close() > f1 = open(x,'w') This line has a spurious space added to the start. That's what I mean about accidentally messing up the indentation. > z = f1.write("1") > f1.close() You have now over-written the contents of file 'x' with a single digit '1'. > tts.say("writing to file") > tts.say("A.B.B. robot; go to my directed position") > f = open(x) > r0 = f.read(500) > tts.say(r0) > nao = r0 > f.close() And now you re-read the same file 'x', reading a single '1' (because that is all that is inside the file). r0 and nao will always be '1'. > import time Another copy-and-paste error introducing spurious indentation. > time.sleep(5) # delays for 5 seconds What is the point of this sleep? This has nothing to do with your problem. Take it out. > ##f1 = open(x,'w') > ##f1.write("0") > ##f1.close() > y = "C:/FTP_Folder/test3.txt" > f2 = open(y) > r1 = f2.read(500) > abb = r1 Now you read from a completely different file. > if nao == abb: > print("Positions are same") > print r, r1 > else: > print("not same") > print nao, abb Why do you expect them to be the same? You are reading from two completely different files, one has been overwritten by the digit '1' each time. The only possible way for these to be the same will be: (1) Start file x as anything. (2) Start file y with *exactly* a single digit '1' and nothing else. Be careful because some text editors will automatically add a newline to the end of your file when they save. (3) You read file x. (4) Then you over-write x with a single digit '1'. (5) Then you re-read x, reading the single digit '1' again. (6) Now you read y, getting '1'. If you get *anything* else, even a single space or newline, they won't match. (7) If they do match, your code prints the ORIGINAL (now over-written) contents of x, and the contents of y, which has to be '1' or else it will never match the NEW (over-written) contents of x. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with 'IF' condition
a.ajm...@incycleautomation.com wrote: > I am trying to compare two different values using "IF" condition in my > program. Everything is working fine except this. I copied my program as > plain text below Your code has indentation errors, my analysis assumes # the following was added to make it runnable class TTS: def say(self, message): print "TTS says:", message tts = TTS() # end of my addition tts.say("Hi") x = "C:/FTP_Folder/test1.txt" f = open(x) r = f.read(500) tts.say("Current position number in the text file is") tts.say(r) f.close() f1 = open(x,'w') z = f1.write("1") f1.close() tts.say("writing to file") tts.say("A.B.B. robot; go to my directed position") f = open(x) r0 = f.read(500) tts.say(r0) nao = r0 f.close() import time time.sleep(5) # delays for 5 seconds ##f1 = open(x,'w') ##f1.write("0") ##f1.close() y = "C:/FTP_Folder/test3.txt" f2 = open(y) r1 = f2.read(500) abb = r1 if nao == abb: print("Positions are same") print r, r1 else: print("not same") print nao, abb > , in the last section I used "IF" condition. - If you see > in my code, I'm writing to "test1.txt" and saving that value in "nao" as > well. On the other side, I'm reading from "test3.txt" and saving that > value in "abb" just like above. > > Now, my goal is to compare these two variables nao & abb. As it written > below. However, result never gives me "true" when actually this both > variable contain same values. No, they don't. nao will contain the first 500 bytes of the file "C:/FTP_Folder/test1.txt" while abb will contain the first 500 bytes of the file "C:/FTP_Folder/test3.txt". But even if you start with two files with the same contents -- with the following lines > f1 = open(x,'w') > z = f1.write("1") > f1.close() you overwrite "C:/FTP_Folder/test1.txt" which now contains only a single "1". Thus the nao == abb test will only compare equal if "C:/FTP_Folder/test3.txt" contains a single "1", too. > This code always giving me "not same" > result. > > I'm not sure why this is not working. > I would greatly appreciate your help. > > Please let me know for any question. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with 'IF' condition
On 01/12/17 14:02, a.ajm...@incycleautomation.com wrote: > - If you see in my code, I'm writing to "test1.txt" and saving that value in > "nao" as well. > On the other side, I'm reading from "test3.txt" and saving that value in > "abb" just like above. > > Now, my goal is to compare these two variables nao & abb. As it written below. > However, result never gives me "true" when actually this both variable > contain same values. Are you absolutely sure they are the same? Have you checked, for example for newline characters or other whitespeace? You could do that by printing the repr() of the values: print repr(nao), repr(abb) Now simplifying your code slightly: > x = "C:/FTP_Folder/test1.txt" > f = open(x) > r = f.read(500) > f.close() > f1 = open(x,'w') > z = f1.write("1") > f1.close() Note that this will have deleted everything in x and replaced it with "1" > f = open(x) > r0 = f.read(500) r0 will now contain '1' > nao = r0 As will nao > f.close() > y = "C:/FTP_Folder/test3.txt" > f2 = open(y) > r1 = f2.read(500) > abb = r1 abb now contains whatever was in test3.txt. Did it contain '1'? > if nao == abb: > print("Positions are same") > print r, r1 r contains the original contents of test1 r1 contains the content of test3 > else: > print("not same") > print nao, abb whereas nao contains '1' and abb contains the same as r1 Is my interpretation what you see, and is it what you expect? In future with thee kinds of issues its good to include an actual cut n paste of the program output. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Problem with 'IF' condition
Hi, I am trying to compare two different values using "IF" condition in my program. Everything is working fine except this. I copied my program as plain text below, in the last section I used "IF" condition. - If you see in my code, I'm writing to "test1.txt" and saving that value in "nao" as well. On the other side, I'm reading from "test3.txt" and saving that value in "abb" just like above. Now, my goal is to compare these two variables nao & abb. As it written below. However, result never gives me "true" when actually this both variable contain same values. This code always giving me "not same" result. I'm not sure why this is not working. I would greatly appreciate your help. Please let me know for any question. tts.say("Hi") x = "C:/FTP_Folder/test1.txt" f = open(x) r = f.read(500) tts.say("Current position number in the text file is") tts.say(r) f.close() f1 = open(x,'w') z = f1.write("1") f1.close() tts.say("writing to file") tts.say("A.B.B. robot; go to my directed position") f = open(x) r0 = f.read(500) tts.say(r0) nao = r0 f.close() import time time.sleep(5) # delays for 5 seconds ##f1 = open(x,'w') ##f1.write("0") ##f1.close() y = "C:/FTP_Folder/test3.txt" f2 = open(y) r1 = f2.read(500) abb = r1 if nao == abb: print("Positions are same") print r, r1 else: print("not same") print nao, abb Best Regards, Achyut Ajmera ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor