python 3.4 - How do you set a variable from an input in a module -


so, i'm trying set variable in main program integer input function in module. can't work out how this.

this main program. menu name of module, i'm using display menu. 'menulist' want menu display. part works ok.

import time, sys menu import *  menulist = ['say hi', 'say bye', 'say yes', 'say no', 'exit'] choice = int(0) menu(menulist)  choosing(menulist, choice)  print(choice) ##this can see if choice == 1:     print('say hi') elif choice == 2:     print('say bye') elif choice == 3:     print('say yes') elif choice == 4:     print('say no') elif choice == 5:     sys.exit() else: ##this print if choice doesn't equal want     print('error') 

this module.

import time  def menu(menulist):     print('menu:')     time.sleep(1)     x = 0     while x < len(menulist):         y = x + 1         printout = '     ' + str(y) + '. ' + menulist[x]         print(printout)         x = x + 1         time.sleep(0.3)     time.sleep(0.7)     print('')   def choosing(menulist, choice):     flag = true     while flag:         try:             choice = int(input('enter number of choice: '))             time.sleep(0.8)             if choice < 1 or choice > len(menulist):                 print('that wasn\'t option sorry')                 time.sleep(1)             else:                 flag = false         except valueerror:             print('that wasn\'t option sorry')             time.sleep(1) 

the menu function works fine, , choosing function want to, won't set 'choice' in main program input when call module. sorry if it's blatantly obvious, i'm pretty new programming. thanks

your module doesn't recognise choice global variable. inside def choosing(...), choice local variable gets assigned value of input (converted int).
pass choice choosing, since you've set choice 0 before that, immutable variable, , python has (behind scenes) created local copy. matters, choice local variable inside choosing.

once program leaves function choosing, variable , value disappear (for practical purposes).

to solve this, shouldn't attempt make choice global: generally, bad design (plenty of exceptions, still, don't).

instead, can return choice function, , assign in main program. relevant part of module:

def choosing(menulist):     while true:         try:             choice = int(input('enter number of choice: '))             time.sleep(0.8)             if choice < 1 or choice > len(menulist):                 print('that wasn\'t option sorry')                 time.sleep(1)             else:                 break         except valueerror:             print('that wasn\'t option sorry')             time.sleep(1)     return choice 

(i made slight alteration here: can use break statement break out of continous while loop, instead of flag variable.) note: there no need assign initial value choice: function structured has pass through line choice = int(.... that, or exist exception other valueerror.

the relevant part of main program:

import time, sys menu import *  menulist = ['say hi', 'say bye', 'say yes', 'say no', 'exit'] menu(menulist)  choice = choosing(menulist) 

together above note: no need initial value of choice.

finally: see how choice has disappeared parameters in function, both in call , definition of choosing.

that last point suggest me might coming different programming language. in python, rare pass parameter function have altered. return it, because easy , clearer. in have multiple variables alter, can example return tuple: return a, b, c. or dict, or whatever fancy (but tuple starting point multiple return values).


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -