I'm trying to write a Rock, Paper Scissors program with Python but I keep getting this error and I'm not sure how to fix it.
File "C:\Python33\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 140, in <module>
File "C:\Python33\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 34, in main
builtins.TypeError: 'tuple' object is not callable
This is my program:
import random
def main():
win = 0
lose = 0
tie = 0
ROCK_CHOICE = '1'
PAPER_CHOICE = '2'
SCISSORS_CHOICE = '3'
QUIT_CHOICE = '4'
play_again = 'y'
while play_again == 'y':
print('Welcome to the game of paper, rock, scissors!')
print('Please input the correct number according')
print('to the choices given.')
computer_choice = get_computer_choice()
player_choice = get_player_choice()
determine_winner = (computer_choice, player_choice)
print('Computer choose', computer_choice, '.')
print('You choose', player_choice, '.')
determine_winner(computer_choice, player_choice)
if result == -1:
lose += 1
elif result == 0:
tie += 1
else:
win += 1
play_again = input('Play again? Enter y for yes')
def get_computer_choice():
choice = random.randint(1,4)
if choice == 1:
choice = 'ROCK'
elif choice == 2:
choice = 'PAPER'
elif choice == 3:
choice = 'SCISSORS'
else:
choice = 4
return choice
def get_player_choice():
choice = int(input('Select rock(1), paper(2), or scissors(3): '))
while choice != 1 and choice != 2 and choice != 3:
print('The valid numbers are rock choice 1), paper choice 2),')
print('or scissors choice 3).')
choice = int(input('Please a valid number: '))
if choice == 1:
choice = 'ROCK'
elif choice == 2:
choice = 'PAPER'
elif choice == 3:
choice = 'SCISSORS'
else:
choice = 4
return choice
def determine_winner(computer_choice, player_choice):
if player_choice == ROCK_CHOICE and computer_choice == ROCK_CHOICE:
print('Its a tie.')
return 0
elif player_choice == PAPER_CHOICE and computer_choice == PAPER_CHOICE:
print('Its a tie.')
return 0
elif player_choice == SCISSORS_CHOICE and computer_choice == SCISSORS_CHOICE:
print('Its a tie.')
return 0
elif player_choice == ROCK_CHOICE and computer_choice == PAPER_CHOICE:
print('You lose, Rock covers Paper.')
return -1
elif player_choice == ROCK_CHOICE and computer_choice == SCISSORS_CHOICE:
print('You WIN!!! Rock smashes Scissors.')
return 1
elif player_choice == PAPER_CHOICE and computer_choice == SCISSORS_CHOICE:
print('You lose, Scissors cuts Paper.')
return -1
elif player_choice == SCISSORS_CHOICE and computer_choice == ROCK_CHOICE:
print('You lose, Rock smashes Paper.')
return -1
elif player_choice == SCISSORS_CHOICE and computer_choice == PAPER_CHOICE:
print('You WIN!!! Scissors cuts Paper.')
return 1
elif player_choice == PAPER_CHOICE and computer_choice == ROCK_CHOICE:
print('You WIN!!! Paper covers Rock.')
return 1
else:
player_choice == QUIT_CHOICE
print('Exiting the program...')
return
def display_results():
print()
print(' MENU')
print('1) Rock')
print('2) Paper')
print('3) Scissors')
print('4) Quit')
player_choice = input('Enter your choice:')
while player_choice != '1' and player_choice != '2' and \
player_choice != '3' and player_choice != '4':
print()
print('Error: invalid selection.')
player_choice = input('Please re-enter your choice:')
print('Your total wins are', win, '.')
print('Your total losses are', lose, '.')
print('Your total ties are', tie, '.')
main()
input('\nPress ENTER to continue...')
error 1:
fix line27 : result = determine_winner(computer_choice, player_choice
error 2:
if player_choice == ROCK_CHOICE and computer_choice == ROCK_CHOICE:
NameError: global name 'ROCK_CHOICE' is not defined
move:
ROCK_CHOICE = '1'
PAPER_CHOICE = '2'
SCISSORS_CHOICE = '3'
QUIT_CHOICE = '4'
outside main (to line 3)
error 3:
File "main.py", line 41, in main
play_again = input('Play again? Enter y for yes')
File "<string>", line 1, in <module>
NameError: name 'y' is not defined
input should be raw_input line 41:
play_again = raw_input('Play again? Enter y for yes')
NB. and the input('\nPress ENTER to continue...') also needs to be a raw_input
NNBB. and if you plan to use display_results; there is errors in there too.
from random import randint
class RPS:
def __init__(self, pCh):
self.playChoice = pCh
self.compChoice = ""
self.choice = randint(0, 3)
self.winner = ""
self.uCompChoice = ""
self.uPlayChoice = ""
if self.choice == 0:
self.compChoice = "R"
elif self.choice == 1:
self.compChoice = "P"
else:
self.compChoice = "S"
if self.compChoice == "R":
self.uCompChoice = "Rock"
if self.compChoice == "P":
self.uCompChoice = "Paper"
if self.compChoice == "S":
self.uCompChoice = "Scissors"
if self.playChoice == "P" or self.playChoice == "p" or self.playChoice == "Paper" or self.playChoice == "paper" or self.playChoice == "PAPER" or self.playChoice == "2":
self.uPlayChoice = "Paper"
if self.playChoice == "S" or self.playChoice == "s" or self.playChoice == "Scissors" or self.playChoice == "scissors" or self.playChoice == "SCISSORS" or self.playChoice == "3":
self.uPlayChoice = "Scissors"
if self.playChoice == "R" or self.playChoice == "r" or self.playChoice == "Rock" or self.playChoice == "rock" or self.playChoice == "ROCK" or self.playChoice == "1":
self.uPlayChoice = "Rock"
def determineWinner(self):
if self.uCompChoice == self.uPlayChoice:
return "Draw Game!"
elif self.uCompChoice == "Rock":
if self.uPlayChoice == "Paper":
return "You won because Paper covers Rock!!"
else:
return "Computer wins because Rock breaks Scissors."
elif self.uCompChoice == "Paper":
if self.uPlayChoice == "Scissors":
return "You won because Rock cuts Paper!!"
else:
return "Computer wins because Paper covers Rock."
else:
if self.uPlayChoice == "Rock":
return "You won because Rock breaks Scissors!!"
else:
return "Computer wins because Scissors cuts Paper."
def __str__(self):
return "Your weapon: " + self.uPlayChoice + "\n" + "Computer's weapon " + self.uCompChoice + "\n" + \
self.determineWinner()
def main():
print("Welcome to RockPaperScissors.")
print("Pick your weapon: ")
print("1. Rock")
print("2. Paper")
print("3. Scissors")
print("Note: If you don't pick a valid weapon the computer will pick a random weapon for you")
# input from user (weapon)
weapon = input("")
# Creating a new object
attack = RPS(weapon)
# printing the result
print(attack.__str__())
# Asking the user if he wants to play again
playagain = input("Would you like to play again: \n")
if playagain == "Yes" or playagain == "yes" or playagain == "YES" or playagain == "y" or playagain == "Y":
main()
else:
print("Thank you for playing.")
main()
don't you think, this code will be shorter this way:
print("R for rock")
print("P for paper")
print("S for scissors")
choices = ["rock", "paper", "scissor"]
while True:
choice = random.choice(choices)
player_choice = input("choose> ")
if player_choice.lower() == "r":
if choice == "rock":
print("i picked rock too!")
elif choice == 'paper':
print("YOU LOST! i picked paper")
else:
print("fine, you win")
elif player_choice.lower() == "p":
if choice == "rock":
print("You win.... Whatever!")
elif choice == 'paper':
print("haha same decisions")
else:
print("I won! You should be ashamed of losing against a bot")
elif player_choice.lower() == "s":
if choice == "rock":
print("I won! Humanity will soon be controlled by bots")
elif choice == 'paper':
print("Alright, you win")
else:
print("Draw")
else:
print("YOU WANNA PLAY GAMES WITH ME!??!?!?")
Related
This is my first little program I have created. Can anyone help me with making it so it works with the first letter being capitalized?
# Rock Paper Scissors
import time
import random
user_wins = 0
computer_wins = 0
def RockPaperScissors():
user_choice = input("Enter your choice in a game of Rock, Paper, Scissors: ")
if user_choice == "rock" or user_choice == "paper" or user_choice == "scissors":
print("Good choice, lets see who wins.")
time.sleep(1.5)
else:
print('Please enter either "rock", "paper", or "scissors"')
computer_choice = ["rock", "paper", "scissors"]
rand_choice = random.choice(computer_choice)
print("The computer chose", rand_choice)
if user_choice == "rock" and rand_choice == "scissors":
print("You won!")
elif user_choice == "scissors" and rand_choice == "rock":
print("The computer won, try again.")
elif user_choice == "paper" and rand_choice == "rock":
print("You won!")
elif user_choice == "rock" and rand_choice == "paper":
print("The computer won, try again")
elif user_choice == "scissors" and rand_choice == "paper":
print("You won!")
elif user_choice == "paper" and rand_choice == "scissors":
print("The computer won, try again")
RockPaperScissors()
I also have a slight issue with if you capitalize the first letter, the program does not run as intended. Appreciate the help!
The function is not working and therefore my score is not adding. I would like to run this just like a normal rock paper scissors game. It would be highly appreciated if you are able to solve this problem.
import random
user_score = 0
computer_score = 0
def Choose_Option():
user_choice = input("Choose Rock, Paper, Scissors: ")
if user_choice in ["Rock", "rock", "r", "R"]:
user_choice = "r"
elif user_choice in ["Paper", "paper", "p", "P"]:
user_choice = "p"
elif user_choice in ["Scissors", "scissors", "s", "S"]:
user_choice = "s"
else:
print("I don't understand. Try Again")
Choose_option()
return user_choice
def Computer_Option():
computer_choice = random.randint(1,3)
if computer_choice == 1:
computer_choice == "r"
elif computer_choice == 2:
computer_choice == "p"
else:
computer_choice == "s"
return computer_choice
while True:
print("")
user_choice = Choose_Option()
computer_choice = Computer_Option()
print("")
if user_choice == "r":
if computer_choice == "r":
print("You choose rock. Computer choose rock. Tied")
elif computer_choice == "p":
print("You choose rock. Computer choose paper. You Lose")
computer_score += 1
elif computer_choice == "s":
print("You choose rock. Computer choose scissors. You Win")
user_score += 1
elif user_choice == "p":
if computer_choice == "p":
print("You choose paper. Computer choose paper. Tied")
elif computer_choice == "r":
print("You choose paper. Computer choose rock. You Win")
user_score += 1
elif computer_choice == "s":
print("You choose paper. Computer choose scissors. You lose")
computer_score += 1
elif user_choice == "s":
if computer_choice == "s":
print("You choose scissors. Computer choose scissors. Tied")
elif computer_choice == "r":
print("You choose scissors. Computer choose rock. You lose")
computer_score += 1
elif computer_choice == "p":
print("You choose scissors. Computer choose paper. You win")
user_score += 1
print("")
print("Player wins: " + str(user_score))
print("Computer wins: " + str(computer_score))
print("")
user_choice = input("Do you want to play again? (y/n)")
if user_choice in ["Y", "y", "yes", "Yes", "YES"]:
pass
elif user_choice in ["N", "NO", "no", "No", "n"]:
break
else:
break
I liked this problem. Try posting it in github as well.
Here are some improvements that I made it.
import random
user_score = 0
computer_score = 0
def Choose_Option():
user_choice = input("Choose Rock, Paper, Scissors: ")
user_choice=user_choice.lower()
if user_choice in ["rock", "r"]:
user_choice = "r"
elif user_choice in ["paper", "p"]:
user_choice = "p"
elif user_choice in ["scissors", "s"]:
user_choice = "s"
else:
print("I don't understand. Try Again")
Choose_Option()
return user_choice
def Computer_Option():
computer_choice = random.randint(1,3)
dict_option = {
1:'r'
,2:'p'
,3:'s'
}
computer_choice=dict_option.get(computer_choice)
return computer_choice
while True:
print("")
user_choice = Choose_Option()
computer_choice = Computer_Option()
print("")
dict_rule={
'p':'r',
's':'p',
'r':'s'
}
dict_name={
'p':'Paper',
's':'Scissor',
'r':'Rock'
}
if user_choice==computer_choice:
print(f"You choose {dict_name[user_choice]}. Computer choose {dict_name[computer_choice]}. Tied")
elif dict_rule[user_choice] == computer_choice:
print(f"You choose {dict_name[user_choice]}. Computer choose {dict_name[computer_choice]}. You Win")
user_score += 1
else:
print(f"You choose {dict_name[user_choice]}. Computer choose {dict_name[computer_choice]}. You Lose")
computer_score += 1
print("")
print("Player wins: " + str(user_score))
print("Computer wins: " + str(computer_score))
print("")
user_choice = input("Do you want to play again? (y/n)")
user_choice=user_choice.lower()
if user_choice in ["y", "yes"]:
pass
elif user_choice in ["no","n"]:
break
else:
break
hi i am practicing python and doing rps game.
This is the code:
class Choices:
rock = "Rock"
paper = 'Paper'
scissors = 'Scissors'
def rps_game():
# Getting the name of the player.
name = raw_input("Welcome to Rock Paper Scissors Game!\n Enter your name:")
# The Players Choice.
while True:
player_choice = raw_input("\n1-Rock\n2-Paper\n3-Scissors\n{} choose a number:".format(name))
int(player_choice)
if player_choice == 1:
player_choice = Choices.rock
if player_choice == 2:
player_choice = Choices.paper
if player_choice == 3:
player_choice = Choices.scissors
# Getting the cpu choice.
cpu_choice = random.randint(1, 3)
if cpu_choice == 1:
cpu_choice = Choices.rock
if cpu_choice == 2:
cpu_choice = Choices.paper
if cpu_choice == 3:
cpu_choice = Choices.scissors
if player_choice == cpu_choice:
print"\n Its a Tie!/n{} you!".format(name)
if player_choice == Choices.paper and cpu_choice == Choices.rock:
print"\n Congratulations!\n{} you won!".format(name)
if player_choice == Choices.scissors and cpu_choice == Choices.paper:
print"\n Congratulations!\n{} you won!".format(name)
if player_choice == Choices.rock and cpu_choice == Choices.scissors:
print"\n Congratulations!\n{} you won!".format(name)
else:
print"\n Too Bad You Lost!".format(name)
print "\nCPU Choice: {}\n Your Choice: {}".format(cpu_choice, player_choice)
play_again = raw_input("Want to Play Again? y/n")
if play_again == 'y':
continue
else:
break
I defined only when the player is winning or its a tie and for the else i did else statement. But and a big But it will always will output always the lose output for some reason and when its print the choices for the CPU it prints a string the describes the choice(Paper Scissors ...) But for the players choice it prints the number
So Id be happy to get Your opinion what am i doing wrong and ALSO Id be happy to get some tips what are your thoughts and tips to get my code more efficient and professional
player_choice = raw_input("\n1-Rock\n2-Paper\n3-Scissors\n{} choose a number:".format(name))
int(player_choice)
This code doesn't work. player_choice is set as a string from raw_input but int(player_choice) doesn't do anything. It creates an integer and then sends it into the void. Instead, you need to reassign it to player_choice like so:
player_choice = int(player_choice)
if playerChoice == 1 and compChoice == 3:
print("You Win - Rock crushes Scissors.")
if playerChoice == 2 and compChoice == 1:
print("You Win - Paper covers Rock.")
if playerChoice == 3 and compChoice == 2:
print("You Win - Scissors cut Paper.")
if compChoice == 1 and playerChoice == 3:
print("You Lose - Rock crushes Scissors.")
if compChoice == 2 and playerChoice == 1:
print("You Lose - Paper covers Rock.")
if compChoice == 3 and playerChoice == 2:
print("You Lose - Scissors cut Paper.")
In your existing code. The else only applies to the final if statement. So if the player choice is not Rock and the cpu choice is not Scissors then the else is printed.
To resolve this use elif to make a chain of if statements. Then the else will apply only if none of the if conditions are met e.g.
if player_choice == cpu_choice:
print"\n Its a Tie!/n{} you!".format(name)
elif player_choice == Choices.paper and cpu_choice == Choices.rock:
print"\n Congratulations!\n{} you won!".format(name)
elif player_choice == Choices.scissors and cpu_choice == Choices.paper:
print"\n Congratulations!\n{} you won!".format(name)
elif player_choice == Choices.rock and cpu_choice == Choices.scissors:
print"\n Congratulations!\n{} you won!".format(name)
else:
print"\n Too Bad You Lost!".format(name)
You also need to convert the return value from raw_input to an int. Otherwise its a string and will never be equal to any of the integer values representing the choices. So use int(raw_input()) instead.
I see you tried to do this by using int(player_choice). The int constructor does not work 'in place'. i.e. it doesnt convert the variable itself to an int. It returns an integer value leaving player_choice as a string. You can see when we wrap input in the call to int we are capturing the return value and so player_choice is then an int (assuming your input was convertible to int).
Here's a that will work on python 2 and python 3.
import random
from sys import version_info
if version_info.major == 2:
try:
input = raw_input
except NameError:
pass
class Choices:
rock = "Rock"
paper = 'Paper'
scissors = 'Scissors'
def rps_game():
# Getting the name of the player.
name = input("Welcome to Rock Paper Scissors Game!\n Enter your name:")
# The Players Choice.
while True:
player_choice = int(input("\n1-Rock\n2-Paper\n3-Scissors\n{} choose a number:".format(name)))
if player_choice == 1:
player_choice = Choices.rock
elif player_choice == 2:
player_choice = Choices.paper
elif player_choice == 3:
player_choice = Choices.scissors
# Getting the cpu choice.
cpu_choice = random.randint(1, 3)
if cpu_choice == 1:
cpu_choice = Choices.rock
elif cpu_choice == 2:
cpu_choice = Choices.paper
elif cpu_choice == 3:
cpu_choice = Choices.scissors
if player_choice == cpu_choice:
print("\n Its a Tie!/n{} you!".format(name))
elif player_choice == Choices.paper and cpu_choice == Choices.rock:
print("\n Congratulations!\n{} you won!".format(name))
elif player_choice == Choices.scissors and cpu_choice == Choices.paper:
print("\n Congratulations!\n{} you won!".format(name))
elif player_choice == Choices.rock and cpu_choice == Choices.scissors:
print("\n Congratulations!\n{} you won!".format(name))
else:
print("\n Too Bad You Lost!".format(name))
print ("\nCPU Choice: {}\n Your Choice: {}".format(cpu_choice, player_choice))
play_again = input("Want to Play Again? y/n")
if play_again != 'y':
break
rps_game()
Also here's an attempt to implement it in less code.
from __future__ import print_function
import random
from sys import version_info
if version_info.major == 2:
input = raw_input
choices = ('Rock', 'Paper', 'Scissors')
winlogic = {
'Rock': 'Scissors',
'Paper': 'Rock',
'Scissors': 'Paper'
}
def calcwinner(choice1, choice2):
if choice1 == choice2: return 0
return 1 if winlogic[choice1] == choice2 else -1
name = input("Welcome to Rock Paper Scissors Game!\nEnter your name: ")
while True:
index = int(input("1-Rock\n2-Paper\n3-Scissors\n{} choose a number: ".format(name)))
try:
player_choice = choices[index - 1]
except IndexError:
print('please make a valid choice')
continue
cpu_choice = random.choice(choices)
winner = calcwinner(player_choice, cpu_choice)
print('You chose {} and computer chose {}. '.format(player_choice, cpu_choice), end='')
if winner == 0:
print('Tie')
elif winner < 0:
print('Cpu won')
else:
print('You won!')
play_again = input("Want to Play Again? y/n: ")
if play_again not in ('y', 'Y', 'yes'):
break
It's my first time here and I'm kinda panicking. I have an assignment to code Rock, Paper, Scissors in Python. (I use Python 3.1) and for some reason the function is not running...?
Here's the code:
hscore = 0
cscore = 0
tries = 0
#computer choice
rock = ("rock")
paper = ("paper")
scissors = ("scissors")
rps = (rock, paper, scissors)
cchoice = random.choice(rps)
choice = input("\nWhat do you choose? <rock, paper, scissors>: ")
tries +=1
def humanfunction():
if choice == "rock":
if cchoice == scissors:
print("Human wins this round.")
hscore +=1
if choice == "scissors":
if cchoice == paper:
print("Human wins this round.")
hscore +=1
if choice == "paper":
if cchoice == rock:
print("Human wins this round.")
hscore +=1
if cchoice == choice:
print("Tie.")
def compfunction():
if cchoice == scissors:
if choice == "paper":
print("Computer wins this round.")
cscore +=1
if cchoice == rock:
if choice == "scissors":
print("Computer wins this round.")
cscore +=1
if cchoice == paper:
if choice == "rock":
print("Computer wins this rounud.")
cscore +=1
if cchoice == choice:
print("Tie.")
def choose():
tries = 0
while 0 == 0:
choice = input("\nWhat do you choose? <rock, paper, scissors>: ")
tries +=1
print("\nHuman choice: ", choice)
print("Computer choice: ", cchoice)
print("Finished game number", tries)
if tries == 10:
print("Limit reached!")
break
humanfunction()
compfunction()
choose()
I've been trying to solve this for days now and for some reason, when I run the code, it doesn't show up who won. Help would be appreciated <3
EDIT:
here's what i get when i run the code:
output
the program is actually supposed to show this:
output2
Here is my take on your code.
The main reason it wasn't running is that your cscore variable was being referenced before it was initialized, because you had setup cscore as a global variable, but didn't declare it as a global variable in your function.
You also needed to import the random library
Also instead of doing 4 if/then statements I combined them into 1 if/then statement
EDIT: cleaned up the code a little more
EDIT 2: no more globals, avoid globals if possible
import random
def get_human_choice():
valid_choice = False
while not valid_choice:
choice = input("\nWhat do you choose? <rock, paper, scissors>: ")
if choice == 'rock' or 'paper' or 'scissors':
valid_choice = True
return choice
def get_comp_choice():
rps = ('rock', 'paper', 'scissors')
comp_choice = random.choice(rps)
return comp_choice
def human_winner(comp_choice):
print("The computer chooses: %s" % comp_choice)
print("Human wins this round.")
def comp_winner(comp_choice):
print("The computer chooses: %s" % comp_choice)
print("Computer wins this round.")
def stats(attempts, human_score, comp_scored, tie_score):
print("Finished game number: %s" % attempts)
print('Human Score: %s' % human_score)
print('Computer Score: %s' % comp_scored)
print('Ties: %s' % tie_score)
def play_game(human_score, comp_score, tie_score):
choice = get_human_choice()
comp_choice = get_comp_choice()
if choice == 'rock':
if comp_choice == 'scissors':
human_winner(comp_choice)
human_score += 1
else:
comp_winner(comp_choice)
comp_score += 1
elif choice == 'scissors':
if comp_choice == 'paper':
human_winner(comp_choice)
human_score += 1
else:
comp_winner(comp_choice)
comp_score += 1
elif choice == 'paper':
if comp_choice == 'rock':
human_winner(comp_choice)
human_score += 1
else:
comp_winner(comp_choice)
comp_score += 1
elif choice == comp_choice:
print("Tie.")
tie_score += 1
return human_score, comp_score, tie_score
if __name__ == '__main__':
tries = 1
h_score, c_score, t_score = 0, 0, 0
while tries <= 10:
h_score, c_score, t_score = play_game(h_score, c_score, t_score)
if tries == 10:
print("\nLimit reached!\n")
stats(tries, h_score, c_score, t_score)
break
else:
tries += 1
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am using Python. Currently new to it. I'm creating a rock paper scissors game with win counter. I've look up some solution online but it doesn't work for me.I'm close to completing it.But there's just an error to it.I can't get the tally counter working.It doesn't shows any win count at the end of the program
from random import randint
print ("Rock ,Paper,Scissors game.")
#Function to get computer input
def generate():
comlist = ["rock","paper","scissors"]
comans = comlist[randint(-1,2)]
if comans == "rock":
print ("Computer choose rock.")
elif comans == "paper" :
print ("Computer choose paper.")
elif comans == "scissors":
print ("Computer choose scissors.")
return comans
#Function to get user input
def user():
userchoice = input ("Choose rock, paper , or scissors.")
while userchoice != 'rock' and userchoice != 'paper' and userchoice != 'scissors':
print ("Invalid input. Please enter again")
userchoice = input ("Choose rock, paper , or scissors.")
if userchoice == "rock":
print ("You choose rock.")
choice = userchoice
elif userchoice == "paper" :
print ("You choose paper.")
choice = userchoice
else:
userchoice == "scissors"
print ("You choose scissors.")
choice = userchoice
return choice
#Function to determine winner
def result(comans ,choice):
global result_set
if choice == comans:
print ("Tie")
elif choice == "rock":
if computer == "paper":
print ("You lose")
else :
print("You win")
result_set ='win'
elif choice == "paper":
if computer == "scissors":
print("You lose")
else:
print("You win")
result_set ='win'
elif choice == "scissors":
if computer == "rock":
print("You lose")
else:
print("You win")
result_set ='win'
#Function to get win taly
def wincounter (result,guess,computer):
if result_set == 'win':
win += 1
else:
pass
print (win)
#Main program
counter = 0
win = 0
diffulty = input(' Please enter diffulty.( "1" for easy , "2" for medium, "3" for hard)')
while diffulty != '1' and diffulty != '2' and diffulty != '3':
print ('Invalid input')
diffulty = input(' Please enter diffulty.( "1" for easy , "2" for medium, "3" for hard)')
if diffulty == '1':
print ("You have choose easy")
counter = 1
guess = user()
computer = generate()
result (computer, guess)
while counter < 3:
guess = user()
computer = generate()
result (computer, guess)
counter +=1
if diffulty == '2':
print ("You have choose medium")
counter = 1
guess = user()
computer = generate()
result (computer, guess)
while counter < 5:
guess = user()
computer = generate()
result (computer, guess)
counter +=1
if diffulty == '3':
print ("You have choose hard")
counter = 1
guess = user()
computer = generate()
result (computer, guess)
while counter < 10:
guess = user()
computer = generate()
result (computer, guess)
counter +=1
Here is my code. It is slightly messy. Sorry for that since than I am still new in python. Thanks for the help.
After running.
Choose rock, paper , or scissors.rock
You choose rock.
Computer choose scissors.
You win
Choose rock, paper , or scissors.rock
You choose rock.
Computer choose rock.
Tie
Choose rock, paper , or scissors.rock
You choose rock.
Computer choose rock.
Tie
It is suppose to show the win results after the end of the last round.
I have fixed your code . . .
from random import randint
print ("Rock ,Paper,Scissors game.")
#Function to get computer input
def generate():
comlist = ["rock","paper","scissors"]
comans = comlist[randint(-1,2)]
if comans == "rock":
print ("Computer choose rock.")
elif comans == "paper" :
print ("Computer choose paper.")
elif comans == "scissors":
print ("Computer choose scissors.")
return comans
#Function to get user input
def user():
userchoice = input ("Choose rock, paper , or scissors.")
while userchoice != 'rock' and userchoice != 'paper' and userchoice != 'scissors':
print ("Invalid input. Please enter again")
userchoice = input ("Choose rock, paper , or scissors.")
if userchoice == "rock":
print ("You choose rock.")
choice = userchoice
elif userchoice == "paper" :
print ("You choose paper.")
choice = userchoice
else:
userchoice == "scissors"
print ("You choose scissors.")
choice = userchoice
return choice
#Function to determine winner
def result(comans ,choice):
result_set = ''
if choice == comans:
print ("Tie")
elif choice == "rock":
if computer == "paper":
print ("You lose")
else :
print("You win")
result_set ='win'
elif choice == "paper":
if computer == "scissors":
print("You lose")
else:
print("You win")
result_set ='win'
elif choice == "scissors":
if computer == "rock":
print("You lose")
else:
print("You win")
result_set ='win'
wincounter(result_set)
#Function to get win taly
def wincounter (result):
global win
if result == 'win':
win += 1
else:
pass
#print (win)
def print_win_count():
global win
print ('you have win '+ str(win) + ' times')
#Main program
counter = 0
win = 0
diffulty = input(' Please enter diffulty.( "1" for easy , "2" for medium, "3" for hard)')
while diffulty != '1' and diffulty != '2' and diffulty != '3':
print ('Invalid input')
diffulty = input(' Please enter diffulty.( "1" for easy , "2" for medium, "3" for hard)')
if diffulty == '1':
print ("You have choose easy")
counter = 1
guess = user()
computer = generate()
result (computer, guess)
while counter < 3:
guess = user()
computer = generate()
result (computer, guess)
counter +=1
print_win_count()
if diffulty == '2':
print ("You have choose medium")
counter = 1
guess = user()
computer = generate()
result (computer, guess)
while counter < 5:
guess = user()
computer = generate()
result (computer, guess)
counter +=1
print_win_count()
if diffulty == '3':
print ("You have choose hard")
counter = 1
guess = user()
computer = generate()
result (computer, guess)
while counter < 10:
guess = user()
computer = generate()
result (computer, guess)
counter +=1
print_win_count(0)