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.

5
  • 3
    Think you might want to read about variable scope. stackoverflow.com/questions/291978/… -- Wouldn't hurt to read about returning variables either. Commented Apr 25, 2016 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. Commented Apr 25, 2016 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) Commented Apr 25, 2016 at 21:01
  • @SurrealDreams So what would you recommend I did instead to store the variables rather than discard them? Commented Apr 25, 2016 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. Commented Apr 25, 2016 at 21:15

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

8 Comments

There's no raw_input in Python 3.
@Blender: Thanks, forget what I said. Edited my answer
@Matt: It "works" because main_menu() will be called when this line is evaluated.
I am not sure which python I am on-using visual studio community- but it will not accept raw_input
@Matt updated the answer, should work now with Python3 (what is probably what you are using)
|
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()

6 Comments

I tried this before and it re-ran option 1 which I do not want it to do.
@Matt: my apologies, I hadn't noticed that you weren't looping in the code. Should work better now, it's fine on my machine.
ctrl-c does nothing btw
@Matt: exits on my machine running it as a script.
@Matt: that's logical... The values would be unset.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.