-5

My problem is quite easy i think...

I need to go through an excel list and just want to print out certain fields (to begin with just the first 10 rows (so low_run = 0, high_run=10, and run=10 too).

Now i wanna get the next 10 so i need:

def get_input(high_run, low_run, run):
    pressed_key = input('')

    if pressed_key == "n":  # Next Page
        set_high_run_np(high_run, run)
        set_low_run_np(low_run, run)

def set_high_run_np(high_run, run):
    if high_run != len(ldata):
        high_run = high_run + run

    return high_run

def set_low_run_np(low_run, run):
    if low_run != len(ldata) - run:
        low_run = low_run + run

    return low_run

The functions do work and would put out low_run = 10 and high_run = 20. It just won't return those numbers so they can be used... any idea why?

Thanks in advance and sorry for any grammar mistakes. No native speaker

Cya!

1 Answers1

0

I think you miss a return in the first function

def get_input(high_run, low_run, run):
    pressed_key = input('')

    if pressed_key == "n":  # Next Page
        return set_high_run_np(high_run, run), set_low_run_np(low_run, run)

def set_high_run_np(high_run, run):
    if high_run != len(ldata):
        high_run = high_run + run

    return high_run

def set_low_run_np(low_run, run):
    if low_run != len(ldata) - run:
        low_run = low_run + run

    return low_run

I added the return. I chose to return a tuple of values which in python is done with a comma. But there are a lot of other ways you can choose to return the values, you just need to return them.

Luca Angioloni
  • 2,243
  • 2
  • 19
  • 28
  • still does not work... when i go into debugging while im in set_high_run_np (so after he calculated but before it returns) high_numb is 20 but after the return high_numb is still 10... might be explaining the problem not correct but thats what im seeing – MisterMojo Aug 18 '17 at 10:50
  • So if I'm getting it right, the other functions (set_high_run_np and set_low_run_np) are not returning? Do they return None or a wrong value? Maybe provide a little context code to see where theese functions are used. What's ldata? – Luca Angioloni Aug 18 '17 at 11:57
  • And also, maybe you think you are passing high_run and low_run as a reference when instead they are not because they are integers. Do you update your external values. And if you have them as global variables, why pass them to the functions? You can use them the same way you use ldata which from what I can see is a global like variable. – Luca Angioloni Aug 18 '17 at 12:03
  • high_run and low_run are global variables yes. they are used for most things (why I would need to update them like this since they like "move" me in the whole table) – MisterMojo Aug 18 '17 at 12:08
  • I wrote the whole thing in a loop and e.g they are used to delete from my table `del used_data[0: low_run]` (used_data here is the same like ldata just it gets shortend here – MisterMojo Aug 18 '17 at 12:10
  • those functions seems to do what they are supposed to (as they calculate the high_run and so on right) but they just dont seem to return it (so its more likely they just return nothing). len(ldata) is the length of the whole list (200) and this statement just stands there so I won't get a list out of range error. I would like to provide some more thing but im quite new to this website so I really know what could help more than those functions (since the rest is functioning). you may have some website where I could show the whole code? – MisterMojo Aug 18 '17 at 12:14
  • Using a high_run as a parameter in the function you are shadowing the global variable, so the high_run you are using in the function is a new one that has for scope only the function. Returning it doesn't help if then you don't update the value of the global one with it. If i was you I would just use the global variable (so no high_run as a parameter of the functions). Use the global keyword to update the value of the global variable – Luca Angioloni Aug 18 '17 at 12:31
  • [Global variables are evil](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil). **EVIL**, I say! – Matthias Aug 18 '17 at 13:45
  • @Matthias you are right, but this is not the purpose of the question. Of course the coding stile of the asker can be much improved but he will learn with time (I hope) – Luca Angioloni Aug 18 '17 at 14:08
  • Hey! I'm back from weekend. I now tried this global solution and now it works! I'm quiet happy to have this finished... and yes you can hope so :) i'm like completly new to all this (started beginning this month.) How can make this question closed? – MisterMojo Aug 21 '17 at 06:40
  • @MisterMojo you can give the correct answer if what I said helped you, I can update my answer including this last thing we discussed about. – Luca Angioloni Aug 21 '17 at 09:08