Matt - 10 months ago 21

Python Question

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

Answer

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()
```