Python Forum
py4e book exercise not working when compiled
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
py4e book exercise not working when compiled
#1
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)
Reply
#2
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
>>>
Reply
#3
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
>>> 
Reply
#4
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-Par...ht=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.
Reply
#5
(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-Par...ht=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...
Reply
#6
I'm on Linux Mint I've got same result as Larz60+
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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
Reply
#8
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
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?
Reply
#10
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'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  John Guttag Book - Finger Exercise 4 - need help to make the code better pritesh 12 10,523 May-06-2020, 05:10 PM
Last Post: riteshp
  Book exercise in lists sonedap 17 6,866 Feb-03-2019, 08:37 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020