[Tutor] How to change the values in python code?

Alan Gauld alan.gauld at yahoo.co.uk
Mon Nov 7 18:17:58 EST 2022


On 07/11/2022 20:43, samira wrote:
> Hi Alan
> 
> Thanks for the suggestions. I tried to address what you mentioned but I am
> not sure if I understood it correctly as the codes are still not outputting
> what I am looking for. 

I'm still not clear what exactly you are looking for.
However, to your data and code...

> Below is the example of data you have requested. As you can see there are
> changes in numbers that are less than previous in multiple locations 

It might help if you point those out because I'mnot clear which
ones you are looking for.

For example:

> Line1: -1.75, 1.08, 10.35, -0.10, -0.01, -0.01, 23.19, 488

We have 8 Fields, indexed 0-7. (based on splitting by a comma)


> Line2: -1.75, 1.12, 10.39, -0.10, -0.01, -0.01, 23.20, 521
> Line4: -1.75, 1.10, 10.40, -0.10, -0.00, -0.01, 23.30, 967

Here index 1 and 4 are both less than previous.
Which if any is significant?

> Line5: -1.77, 1.09, 10.40, -0.10, -0.01, -0.01, 23.28, 1000

And here index 1 and 6 are less.

> Line6: -1.72, 1.08, 10.40, -0.10, -0.00, -0.01, 23.31, 1032

Again it is 1 and 4

> Line1340: -1.97, 0.84, 10.35, -0.10, -0.01, -0.01, 24.16, 48040
> Line1341: -1.97, 0.86, 10.35, -0.10, -0.01, -0.01, 24.16, 48075
> 
> ****this is where the split needs to happen where Last<previous
> 
> Line1342: -2.16, 0.54, 10.25, -0.10, -0.00, -0.01, 24.18, 493

And here it is 1, 2, 4 and 7.

Again, what makes this row different to the others?

> previous=0
> 
> with open ('test-copy.txt', mode="r") as timestamp:
>     for line in timestamp:
>           x=line.split(", ")

Again you loop though all tyhe lines in the file  splitting
them then throwing the data away leaving only the last line
in x.

>     last=int(x[7])

Now you assign index 7, field 8, to last.
You never change previous so it is always zero.

>     print("last = ", last)
>     print("previous = ", previous)
> 
>     if last < previous :
>         print("split here")

Here you compare last to 0. It will only be less if x[7] is negative.
But the sample data suggests it is always positive.

I suspect you need more code inside the initial for loop,
something like this(untested)

previous = 0
with open ('test-copy.txt', mode="r") as timestamp:
    for line in timestamp:
          x=line.split(", ")
          last=int(x[7])
          print("last = ", last)
          print("previous = ", previous)
          if last < previous :
             print("split here - last = ", last)
          previous = last


-- 
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





More information about the Tutor mailing list