Python Forum
py4e book exercise not working when compiled - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: py4e book exercise not working when compiled (/thread-10845.html)

Pages: 1 2


py4e book exercise not working when compiled - adriand - Jun-09-2018

Hi,

this exercise in the Py4E course should print: 21, 31, uct.ac.za.
When I run the program (python3 filename), my results are: 21, -1, uct.ac.za Sat Jan  5 09:14:16 2008.
Can someone tell me why is this happening?
Thanks.

data = 'From [email protected] Sat Jan  5 09:14:16 2008'
atpos = data.find('@') 
# the atpos variable will have the position of the @ sign

print (atpos)

spos = data.find(' ',atpos) 
# look for a space starting from the position of the @ sign
print (spos)

host = data[atpos+1 : spos]
print (host)



RE: py4e book exercise not working when compiled - Larz60+ - Jun-09-2018

That's what I get:
λ python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> data = 'From [email protected] Sat Jan  5 09:14:16 2008'
>>> atpos = data.find('@')
>>> print (atpos)
21
>>> spos = data.find(' ',atpos)
>>> print (spos)
31
>>> host = data[atpos+1 : spos]
>>> print (host)
uct.ac.za
>>>



RE: py4e book exercise not working when compiled - adriand - Jun-09-2018

thanks!
this is why it's confusing and I'd like to understand why the different results before I move on with the course.
this is what I get
Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> data = 'From [email protected] Sat Jan  5 09:14:16 2008'
>>> atpos = data.find('@') 
>>> print (atpos)
21
>>> spos = data.find(' ',atpos) 
>>> print (spos)
-1
>>> host = data[atpos+1 : spos]
>>> print (host)
uct.ac.za Sat Jan  5 09:14:16 200
>>> 



RE: py4e book exercise not working when compiled - Larz60+ - Jun-09-2018

I just copied from your post above:
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on wi
n32                                                                                      
Type "help", "copyright", "credits" or "license" for more information.                   
>>> data = 'From [email protected] Sat Jan  5 09:14:16 2008'                    
>>> atpos = data.find('@')                                                               
>>> print (atpos)                                                                        
21                                                                                       
>>> spos = data.find(' ',atpos)                                                          
>>> spos                                                                                 
31                                                                                       
>>>                                                                                      
I'm using windows but that shouldn't be an issue.
how did you install python?
You should update using version 3.6.5 follow instructions here: https://python-forum.io/Thread-Basic-Part-1-Linux-Python-3-environment?highlight=install

Are you running on an obscure machine or obscure python?
I can't see any reason for you to get such results.

Try install as above and see if that has an effect.


RE: py4e book exercise not working when compiled - adriand - Jun-09-2018

(Jun-09-2018, 01:09 PM)Larz60+ Wrote: I'm using windows but that shouldn't be an issue.
how did you install python?
You should update using version 3.6.5 follow instructions here: https://python-forum.io/Thread-Basic-Part-1-Linux-Python-3-environment?highlight=install

Are you running on an obscure machine or obscure python?

I'm using debian and I've installed python from the repositories. This is weird...


RE: py4e book exercise not working when compiled - buran - Jun-09-2018

I'm on Linux Mint I've got same result as Larz60+


RE: py4e book exercise not working when compiled - adriand - Jun-09-2018

I see... How common is to run the same code and encounter different results?
I'm searching for a solution on the debian forums as well, maybe it's a package issue


RE: py4e book exercise not working when compiled - buran - Jun-09-2018

sorry for asking, but are you definitely sure you run exactly the same code?
for some reason it doesn't find the char(s) you search for on line#8


RE: py4e book exercise not working when compiled - adriand - Jun-09-2018

Yes, the first time I executed the code from a file.
Then tried the interpreter and posted it here - reply #3
Same results in both cases...
Thanks for being so responsive and helping me figure this out!
Ah, just a curiosity, what would position -1 be in the string?


RE: py4e book exercise not working when compiled - buran - Jun-09-2018

str.find() (line#8) will return -1 if it doesn't find what you search for
however in slicing (line#11) -1 is the last element (negative index are allowed in slicing)
try this
>>> my_string='abcd'
>>> my_string.find('e')
-1
>>> my_string[-1]
'd'
>>> my_string.find('b')
1
>>> my_string.find('b', 2) # here we search for 'b', but starting from index 2
-1
>>> my_string[-3]
'b'
>>> my_string[1:-1]
'bc'