1
def set_values():
    cycle_num = input("Cycle Amount: ")
    probability = input("Probability: ")
    main_menu()
    return cycle_num, probability
def display_values(cycle_num, probability):
    print(cycle_num)
    print(probability)
def main_menu():
    user_choice = input("Choose option 1 or 2")
    if user_choice == "1":
        set_values()
    else:
        display_values(cycle_num, probability)
if __name__ == main_menu():
    main_menu()

I am struggling to use variables set in one function in another. I must specify that I am new to python. How could I make this work. Thanks in advance.

Matt
  • 482
  • 1
  • 6
  • 15
  • 3
    Think you might want to read about variable scope. http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules -- Wouldn't hurt to read about returning variables either. – Christopher Schneider Apr 25 '16 at 20:56
  • You're calling set_values(), which returns two values, but you're not storing those values. They are discarded. I second @ChristopherSchneider's suggestion that you do some reading. – Surreal Dreams Apr 25 '16 at 20:57
  • Thanks for the comment- I have spent some time previously looking over similar questions but cannot seem to get them working in my actual code(a lot more complicated than the example I provided) – Matt Apr 25 '16 at 21:01
  • @SurrealDreams So what would you recommend I did instead to store the variables rather than discard them? – Matt Apr 25 '16 at 21:02
  • `cycle_num, probability = set_values()` - All it requires is assigning the returned values to some variables. However, you'll need more than that to make this work. See the answers below. – Surreal Dreams Apr 25 '16 at 21:15

2 Answers2

3

There a some errors:

  • It should be if __name__=='__main__'
  • You make a recursion when calling main_menu() in set_values
  • As mentioned in the comments you return two values in set_values but don't use them
  • You would have to use the global keyword to get this to work, which is in most times a bad idea
  • Maybe you need a proper way to exit your program (like choosing "3")

Try something like this:

def set_values():
    cycle_num = input("Cycle Amount: ")
    probability = input("Probability: ")
    return cycle_num, probability

def display_values(cycle_num, probability):
    print("Cycle Amount: ", cycle_num)
    print("Probability: ", probability)

def main_menu():
  cycle_num=0
  probability=0

  while True:
    user_choice = input("Choose option 1, 2 or 3")
    if user_choice == "1":
        cycle_num, probability=set_values()
    elif user_choice == "2":
        display_values(cycle_num, probability)
    else:
      break

if __name__ == '__main__':
  main_menu()
Günther Jena
  • 3,706
  • 3
  • 34
  • 49
  • 1
    There's no `raw_input` in Python 3. – Blender Apr 25 '16 at 21:09
  • @Blender: Thanks, forget what I said. Edited my answer – Günther Jena Apr 25 '16 at 21:09
  • 1
    @Matt: It "works" because `main_menu()` will be called when this line is evaluated. – Günther Jena Apr 25 '16 at 21:10
  • I am not sure which python I am on-using visual studio community- but it will not accept raw_input – Matt Apr 25 '16 at 21:12
  • @Matt updated the answer, should work now with Python3 (what is probably what you are using) – Günther Jena Apr 25 '16 at 21:14
  • I have a question though- originally I called main_menu() in set_values(). You removed that because it is "recursive" but what I want to know is why it still works and returns to main_menu() with user_choice prompt – Matt Apr 25 '16 at 21:16
  • @Matt: When calling `main_menu()` from `set_values()` it simple get called from there. When choosing "2" `main_menu` returns and `set_values` will be finished – Günther Jena Apr 25 '16 at 21:25
  • I used this method in my other code and initialised the values to "No selection" rather than 0. This returns No Selection even after adding values in the set_values() equivalent function. – Matt Apr 26 '16 at 17:18
2

You're having issues with the concept of scope. When you call the function set_values(), you're assigning two variables and are returning them to the original function they were called from. This part is correct.

Where you're running into problems is that your main_menu() function isn't doing anything with those values. Since you don't store them, they disappear. A simple solution would be:

def set_values():
    cycle_num   = input("Cycle Amount: ")
    probability = input("Probability: ")
    return cycle_num, probability

def display_values(cycle_num, probability):
    print(cycle_num)
    print(probability)

def main_menu():
    while True:
        user_choice = input("Choose option 1 or 2 (ctrl-c exits): ")

        if user_choice == "1":
            cycle_num, probability = set_values()
        elif user_choice == "2":
            display_values(cycle_num, probability)

if __name__ == '__main__':
    main_menu()
Jacobm001
  • 4,431
  • 4
  • 30
  • 51