python - NameError -- not definied -
main program
# import statements import random import winning # set constants win = 0 lose = 0 tie = 0 rock = 1 paper = 2 scissor = 3 # main program rock paper scissor game. def main(): # set variable loop control again = 'y' while again == 'y': # display menu display_menu() # prompt user input userselection = input('which play (1, 2, 3)?: ') computerselection = random.randint(1, 3) # call winner module decide winner! print(winning.winner(userselection, computerselection)) # ask play again , make selection again = input('would play again (y/n)?') def display_menu(): print('please make selection: ') print(' 1) play rock') print(' 2) play paper') print(' 3) play scissor') # call main main()
second file: winning.py:
# module decide on won based on input main def winner(userinput, computerinput): if userinput == rock , computerinput == scissor: print('you win! rock crushes scissor!') win += 1 elif userinput == scissor , computerinput == paper: print('you win! scissor cuts paper!') win += 1 elif userinput == paper , computerinput == rock: print('you win! paper covers rock!') win += 1 elif userinput == computerinput: print('you tied computer! please try again!') tie += 1 else: print('you lost! please try again!') lose += 1
error
traceback (most recent call last): file "c:/python32/rps_project/main.py", line 14, in <module> rock = r nameerror: name 'r' not defined
i have tried quotation marks , all, , cannot figure out!!! this? please , thank you!
don't take negative comments wrong way. sure mark homework homework , sure paste actual error generated code you've posted. error doesn't match code posted.
you might ask question in calmer-seeming way :)
the problem simple. define globals in main program not in winning.py
, lines
if userinput == rock , computerinput == scissor: print('you win! rock crushes scissor!') win += 1
are going cause nameerrors, because rock
, scissor
, win
not defined. in every module, must either define or import names want use; names not shared between modules automatically -- reason!
i'll save trouble telling you must return
value winning.winner
-- otherwise, won't output expect.
Comments
Post a Comment