Python: variable is undefined when calling function containing the variable -


i started first programming class few weeks ago , i'm embarrassed i'm stuck. had create program (in professor's words):

simulate roll of 2 dice. use randomly generated integer represent roll of each die in function named point. return combined value of roll. use loop in main roll dice 5 times , report each result.

so, did best , keep getting same issue telling me variable total not defined, though i'm calling function contains variable.

i submitted below code professor, in turn responded:

the dice program close. return total of roll. call point in main , capture returned value printing.

so saying call function point in main function (which, @ least think, am) still won't read vital variable finishing this.

import random  min=1 max=6    def main():   roll in range(5):     point()     print(total)   def point():    roll=random.randint(min, max)   roll2=random.randint(min, max)   total=roll+roll2   return total  main() 

inside main function, line:

point() 

does not make total available in current scope. instead, calls function point , discards return value.

you need capture return value assigning variable named total:

def main():   roll in range(5):     ################     total = point()     ################     print(total) 

now, when print(total), total defined , equal value of point().


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -