Reputation: 13
I need to write rock, paper, scissors only using functions.
I have not done this before so my try at it is below. I am not able to get it to run fully through. Any pointers in the right direction would be very helpful!
Code below:
import random
def user_choice():
user_choice = input("Choose rock, paper, or scissors: ")
if user_choice in ["rock", "Rock"]:
user_choice = "r"
elif user_choice in ["paper", "Paper"]:
user_choice = "p"
elif user_choice in ["scissors", "Scissors"]:
user_choice = "s"
else: ("Try again.")
user_choice
return user_choice
def computer_choice():
computer_choice = random.randint(1,3)
if computer_choice == 1:
computer_choice = "r"
if computer_choice == 2:
computer_choice = "p"
if computer_choice == 3:
computer_choice = "s"
return computer_choice
def get_winner():
#User choice = rock
if user_choice == "r":
if computer_choice == "r":
print ("You and computer chose rock. It's a tie!")
elif user_choice == "r":
if computer_choice == "p":
print ("You chose rock and computer chose paper. Computer wins!")
elif user_choice == "r":
if computer_choice == "s":
print ("You chose rock and computer chose scissors. You win!")
#User choice = scissors
if user_choice == "s":
if computer_choice == "s":
print ("You and computer chose scissors. It's a tie!")
elif user_choice == "s":
if computer_choice == "p":
print ("You chose scissors and computer chose paper. You win!")
elif user_choice == "s":
if computer_choice == "r":
print ("You chose scissors and computer chose rock. Computer wins!")
#User choice = paper
if user_choice == "p":
if computer_choice == "p":
print ("You and computer chose paper. It's a tie!")
elif user_choice == "p":
if computer_choice == "r":
print ("You chose paper and computer chose rock. You win!")
elif user_choice == "p":
if computer_choice == "s":
print ("You chose paper and computer chose scissors. Computer wins!")
else:
print("Error")
user_choice()
computer_choice()
get_winner()
I tried writing a function for the user input, a random choice for the computer, and one that compares the user and computer choice to get the winner. I have tried writing and calling the functions but it is not working.
Upvotes: 0
Views: 410
Reputation: 3363
You need to pass results of "input" functions to "get_winner" function as arguments.
It should be defined like that:
def get_winner(user_choice, computer_choice):
Then in your code:
uc = user_choice()
cc = computer_choice()
get_winner(uc, cc)
Upvotes: 1