Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 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 stephen.marquard@uct.ac.za 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)
Quote
#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 stephen.marquard@uct.ac.za 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
>>>
Quote
#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 stephen.marquard@uct.ac.za 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
>>> 
Quote
#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 stephen.marquard@uct.ac.za 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.
Quote
#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...
Quote
#6
I'm on Linux Mint I've got same result as Larz60+
Quote
#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
Quote
#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
Quote
#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?
Quote
#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'
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Help with test exercise lm777 3 231 Feb-21-2018, 10:42 PM
Last Post: nilamo
  John Guttag Book - Finger Exercise 4 - need help to make the code better pritesh 9 438 Feb-13-2018, 10:31 PM
Last Post: pritesh
  This is an open book home assignment but i really need your help Samiha 3 506 Oct-04-2017, 05:39 PM
Last Post: nilamo
  Simple exercise - how to write in python tomtom55 6 930 Sep-28-2017, 07:18 PM
Last Post: nilamo
  Help with an exercise Cerobug 8 551 Sep-20-2017, 09:10 PM
Last Post: nilamo
  The Python Book altered rock paper scissors Python_Noob 0 384 Sep-18-2017, 06:13 AM
Last Post: Python_Noob
  Trouble with LPtHW exercise 21 pyth31 6 717 Sep-09-2017, 07:46 PM
Last Post: pyth31
  Python Exercise: Generate a two-dimensional array smbx33 4 1,352 Apr-22-2017, 11:51 PM
Last Post: smbx33

Forum Jump:


Users browsing this thread: 1 Guest(s)