Flamonga
Flamonga

Reputation: 37

Properly Passing Variables to Functions

I have created a rock paper scissors application that allows the user to specify the number of rounds they want to play. After those specified rounds the application will display the score. After ensuring the application was functional, I attempted to create functions for specific parts of the application. I tried to return variables to pass around to other functions but the application is not functional. I think I'm missing some key things here.

I've attempted to mess with the tabs and change certain variables but I have had no luck.

Current Error Message:

Traceback (most recent call last):

  File "C:/Users/iamep/Desktop/Python Projects/RockPaperScissors.py", line 96, in <module>
        main()
      File "C:/Users/iamep/Desktop/Python Projects/RockPaperScissors.py", line 90, in main
        runGame(rounds)
    NameError: name 'rounds' is not defined

Code:

#display Welcome Message
def welcomeMsg():
    print ("Welcome to Rock Paper Scissors")

#User Inputs Number of Rounds
def roundChoice():
    user_input = input('Enter Number of Rounds: ')          
    try:
            rounds = int(user_input)
    except ValueError:
            print('Not a number')
    return rounds
def runGame(rounds):

    import random                         #imports a random module for the computer.
    game = ["ROCK", "PAPER", "SCISSORS"]  #sets the game answers.
    count = 0                             #game count is set to zero.
    score = 0                             #player score is set to zero.
    computerscore =0                      #computers score is set to zero.

    while count == 0:                                       #starts game if count is zero.
      for i in range (rounds):                              #repeats the turns to the user specified rounds
        answer = input ("Pick rock, paper or scissors: ")   #users input their choice

        print (answer.upper())                               #prints the users answer
        computer= random.choice(game)                        #computer picks randomly

        print ("Computer picks",computer)                            #Display Computer Option
        if answer.upper() == "ROCK" and computer == "ROCK":          #This whole set of code sets the game that what beats what.
          print ("Its a tie!")                                       # prints after each response that who won.
          count +=1                                                  #the count variable is increased.

        elif answer.upper() == "PAPER" and computer == "PAPER":
          print ("Its a tie!")
          count +=1

        elif answer.upper() == "SCISSORS" and computer == "SCISSORS":
          print ("Its a tie!")
          count +=1

        elif answer.upper() == "PAPER" and computer == "ROCK":
          print ("You win!")
          count +=1
          score +=1                                                 #If User Wins Score is Added

        elif answer.upper() == "PAPER" and computer == "SCISSORS":
          print ("You lose!")
          count +=1
          computerscore +=1                                         #IF Computer Wins computerscore is added
        elif answer.upper() == "ROCK" and computer == "PAPER":
          print ("You lose!")
          count +=1
          computerscore +=1

        elif answer.upper() == "ROCK" and computer == "SCISSORS":
          print ("You win!")
          count +=1
          score +=1

        elif answer.upper() == "SCISSORS" and computer == "ROCK":
          print ("lose!")
          count +=1
          computerscore +=1

        elif answer.upper() == "SCISSORS" and computer == "PAPER":
          print ("You win!")
          count +=1
          score +=1
        return score, computerscore

def gameResults(score, computerscore):
    if score < computerscore:                      #Display round results
      print ("Your score is", score)
      print ("Computers score is",computerscore)
      print ("Computer wins!.")

    if score > computerscore:
      print ("Your score is", score)
      print ("Computers socre is",computerscore)
      print ("You win!.")

    if score == computerscore:
      print ("Your score is", score)
      print ("Computers socre is",computerscore)
      print ("Its a tie!!.")
def main():
    welcomeMsg()
    roundChoice()
    runGame(rounds)
    gameResults(score, computerscore)

Upvotes: 2

Views: 47

Answers (1)

Mert K&#246;kl&#252;
Mert K&#246;kl&#252;

Reputation: 2231

You don't have rounds in your main so it causes error. You are returning rounds from roundChoice() but not assigning it any variable. I have assigned it and passed runGame() for expected behaviour. Your main implementation needs to be:

def main():
    welcomeMsg()
    rounds=roundChoice()
    runGame(rounds)
    gameResults(score, computerscore) 

Upvotes: 2

Related Questions