Rock,paper,scissors Tally counter python [closed] - python

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)

Related

only first if statement run, and unless it comes back true it will not continue. new to code

import random
user_wins = 0
computer_wins = 0
options = ["Rock", "Paper", "Scissors"]
while True:
user_input = input("Type Rock/Paper/Scissors or Q to quit: ")
if user_input == "q":
break
if user_input not in [options]:
continue
random_number = random.randint(0, 2)
# rock: 0, paper: 1, scissors: 2
computer_pick = options[random_number]
print("computer picked", computer_pick + ".")
if user_input == "rock" and computer_pick == "scissors":
print("You won!")
user_wins += 1
elif user_input == "scissors" and computer_pick == "paper":
print("You won!")
user_wins += 1
elif user_input == "paper" and computer_pick == "rock":
print("You won!")
user_wins += 1
else:
print("You lost!")
computer_wins += 1
print("You won", user_wins, "times.")
print("The cpu won", computer_wins, "times.")
print("Goodbye!")
I'm sorry if im not using this site the correct way but I've been following along with Tech With Tim on youtube trying to write 5 mini python games to just practice. I expect if i put q it will break, but now that i'm typing this i'm realizing that if it were to break i shouldn't get the print statements on line 37,38, and 39. either way, when i input rock , paper, or scissors it comes back as "Type Rock/Paper/Scissors or Q to quit:". I'm having a hard time understanding why my code doesnt work, while Tim has the exact same code, line for line, and his works fine. Any and all help would be appreciated.. even if its directing me to slow my roll
if user_input not in [options] isn't correct. [options] is a list containing one element: all of options. A string can never be all of options, so user_input not in [options] will always be true.
You want
if user_input not in options:

Rock, paper, scissors game in Python - some clarification needed

I am writing a simple game of rock, paper and scissors, where the user competes with a computer. There are 5 tries.
The problems I ran into are :
If I enter something apart from three options, it should return "Invalid Entry". Instead, the program stops
The program never prints "You won the game" or "You lost the game", it finishes after 5 attempts
Other feedback would also be appreciated
import random
def game():
win_count = 0
loose_count = 0
tries = 0
while tries < 5:
chosen = input("Make your choice: ")
if chosen == "scissors" or chosen == "Scissors":
element = "scissors"
elif chosen == "paper" or chosen == "Paper":
element = "paper"
elif chosen == "rock" or chosen == "Rock":
element = "rock"
else:
return "Invalid Entry"
computer_choices = ["scissors", "paper", "rock"]
computer_choice = random.choice(computer_choices)
if element == "scissors" and computer_choice == "paper":
print("Computer chose paper, you chose scissors, you win !")
win_count += 1
tries += 1
elif element == "paper" and computer_choice == "scissors":
print("Computer chose scissors, you chose paper, you loose !")
loose_count += 1
tries += 1
elif element == "paper" and computer_choice == "rock":
print("Computer chose rock, you chose paper, you win !")
win_count += 1
tries += 1
elif element == "rock" and computer_choice == "paper":
print("Computer chose paper, you chose rock, you loose !")
loose_count += 1
tries += 1
else:
print("Whoops, that's a draw, try again")
tries+=1
print("Your Wins: "+ str(win_count))
print("Computer Wins: "+str(loose_count))
if win_count > loose_count:
return "Congrats, you won the game!"
else:
return "Sorry, you lost"
game()
return statement does not print anything but they return the value from a function and as soon as a return statement executes the function ends up executing that's why whenever the user inputs something invalid the program stop.
Also not use the if statement from if chosen = paper or chosen = Paper instead use .lower() to lower the string.
I have made some changes to your code.
Try this code
import random
def game():
win_count = 0
loose_count = 0
tries = 0
while tries < 5:
element = input("Make your choice: ").lower()
computer_choices = ["scissors", "paper", "rock"]
if element not in computer_choices:
print("Invalid choice")
continue
computer_choice = random.choice(computer_choices)
if element == "scissors" and computer_choice == "paper":
print("Computer chose paper, you chose scissors, you win !")
win_count += 1
tries += 1
elif element == "paper" and computer_choice == "scissors":
print("Computer chose scissors, you chose paper, you loose !")
loose_count += 1
tries += 1
elif element == "paper" and computer_choice == "rock":
print("Computer chose rock, you chose paper, you win !")
win_count += 1
tries += 1
elif element == "rock" and computer_choice == "paper":
print("Computer chose paper, you chose rock, you loose !")
loose_count += 1
tries += 1
else:
print("Whoops, that's a draw, try again")
tries+=1
print("Your Wins: "+ str(win_count))
print("Computer Wins: "+str(loose_count))
if win_count > loose_count:
print("Congrats, you won the game!")
else:
print("Sorry, you lost")
game()

How do I make my basic Rock/Paper/Scissors game with fewer lines of code?

I built this simple rock/paper/scissor game by watching some tutorials. Everything is working fine. The issue is that I want to implement something where if the user and the computer choose the same word, then it should say something like "draw."
I could go ahead and add a bunch of "if" and "else" statements, but I don't want that. Can you guys think of any other way to implement that with fewer lines of code?
#Simple rock, paper and scissor game
import random
user_wins = 0
computer_wins = 0
options = ["rock", "paper", "scissors"]
while True:
user_pick = input("Please choose Rock/Paper/Scissors Or Press Q to quit: ")
if user_pick.lower() == "q":
print("You quit.")
break
elif user_pick not in options:
print("Please enter a valid input.")
continue
random_number = random.randint(0, 2)
computer_pick = options[random_number]
print("The computer picked", computer_pick)
if computer_pick == "rock" and user_pick == "scissors":
print("You lost!")
computer_wins += 1
elif computer_pick == "paper" and user_pick == "rock":
print("You lost!")
computer_wins += 1
elif computer_pick == "scissors" and user_pick == "paper":
print("You lost!")
computer_wins += 1
else:
print('You win!')
user_wins += 1
You could use a dictionary to map which choices beat each other. This will enable to you make the conditional part of the code, which determines who wins, more concise. It is also more easily expanded to rock-paper-scissors variants with more options, without the need for many more conditional statements.
#Simple rock, paper and scissor game
import random
user_wins = 0
computer_wins = 0
options = ["rock", "paper", "scissors"]
beaten_by = {
"rock": "scissors",
"scissors": "paper",
"paper": "rock"
}
while True:
user_pick = input("Please choose Rock/Paper/Scissors Or Press Q to quit: ")
if user_pick.lower() == "q":
print("You quit.")
break
elif user_pick not in options:
print("Please enter a valid input.")
continue
random_number = random.randint(0, 2)
computer_pick = options[random_number]
print("The computer picked", computer_pick)
if user_pick == computer_pick:
print("Draw!")
elif user_pick == beaten_by[computer_pick]:
print("You lost!")
computer_wins += 1
else:
print("You win!")
user_wins += 1

Variable not registering change in value

I wanted to make a simple Rock, Paper, Scissor game in Python. It goes well with the game, but the final scores are always being showed as a 0.
I wanted to show a smaller section of the code but, I don't know where the problem lies, so I am sorry for the length of the code.
I am still a novice learner, so please pardon me if the question is too silly or the code is not well formatted.
#Rock-Paper-Scissor Game
import random
print("Please enter your name:")
userName = input()
print("Welcome " + userName)
print("The following are the rules of the game:")
print("Press 'R' for Rock")
print("Press 'P' for Paper")
print("Press 'S' for Scissor")
print("This will be a 10 point match")
userTally = 0
compTally = 0
def gameProcess(userTally, compTally): #The process of the game. It increments or decrements the value depending on the result
print("Your turn:")
userInput = input()
computerChoice = random.choice(["R","P","S"])
if userInput == "R": #User Inputs R for Rock
if computerChoice == "R":
print("The computer chose Rock")
print("It's a Tie")
elif computerChoice == "P":
print("The computer chose Paper")
print("Computer Won")
compTally = compTally + 1
elif computerChoice == "S":
print("The computer chose Scissor")
print("You Won")
userTally = userTally + 1
elif userInput == "P": #User Inputs P for Paper
if computerChoice == "R":
print("The computer chose Rock")
print("You Won")
userTally = userTally + 1
elif computerChoice == "P":
print("The computer chose Paper")
print("It's a Tie")
elif computerChoice == "S":
print("The computer chose Scissor")
print("Computer Won")
compTally = compTally + 1
elif userInput == "S": #User Inputs S for Scissor
if computerChoice == "R":
print("The computer chose Rock")
print("Computer Won")
compTally = compTally + 1
elif computerChoice == "P":
print("The computer chose Paper")
print("You Won")
userTally = userTally + 1
elif computerChoice == "S":
print("The computer chose Scissor")
print("It's a Tie")
return(userTally,compTally)
def tryCount(): #The number of tries....
tryNum = 1
while tryNum < 11:
gameProcess(0, 0)
tryNum = tryNum + 1
tryCount()
print("You scored " + str(userTally))
print("The computer scored " + str(compTally))
if userTally > compTally:
print("CONGRATULATIONS, YOU WON.")
elif userTally < compTally:
print("Sorry, better luck next time.")
close = input()
if close == "Random Input.":
exit()
else:
exit()
You pass 0, 0 to gameProcess, which you treat as the scores within the function, and then return them modified, but you do not actually use the return value in the only place you call gameProcess (in tryCount), so the global userTally, compTally variables remain unchanged.
This is how you should change tryCount:
def tryCount(): #The number of tries....
global userTally, compTally
tryNum = 1
while tryNum < 11:
userTally,compTally=gameProcess(userTally,compTally)
tryNum = tryNum + 1

Making a Rock, Paper, Scissors game in Python. Just curious on how to make multiple rounds in it

I've asked the user to input how many rounds they want to play, but I don't actually know how to make the program play for another round. near the end of the code I'm trying to make the score add up to the rounds as well. Any help?
import time
import random
print ("Welcome to the Rock, Paper, Scissors Game!")
time.sleep(1)
rounds = int(raw_input("How many rounds would you like to play?"))
while rounds not in range(3,11):
try:
rounds = int(raw_input("Sorry! I can only play between 3 and 10 rounds. Try again:"))
except:
print ("ERROR invalid input. Out of range or wrong type of data.")
if rounds in range(3,11):
time.sleep(1)
print ("Challenge accepted! I will play you for", rounds,"rounds! Let's begin!!!")
options = ("rock","paper","scissors")
userChoice = raw_input("Rock, Paper or Scissors, peasant?")
while userChoice not in options:
try:
userChoice = raw_input("Not an option lad. Rock, paper or scissors")
except:
print ("ERROR invalid input. Out of range or wrong type of data.")
cpuoption1 = "rock"
cpuoption2 = "paper"
cpuoption3 = "scissors"
cpuChoice = random.randint(1,3)
answer = ""
if cpuChoice == 1:
answer = cpuoption1
elif cpuChoice == 2:
answer = cpuoption2
elif cpuChoice == 3:
answer = cpuoption3
print ("I choose", answer,"!")
user= 0
cpu = 0
if userChoice == "rock" and answer == cpuoption1:
user += 1
cpu += 1
print ("Draw!")
elif userChoice == "paper" and answer == cpuoption2:
user+=1
cpu+=1
print("Draw!")
elif userChoice == "scissors" and answer == cpuoption3:
user+=1
cpu+=1
print ("Draw!")
elif userChoice == "rock" and answer == cpuoption2:
cpu+=1
print ("CPU wins the round!")
elif userChoice == "rock" and answer == cpuoption3:
user+=1
print ("Player wins the round!")
elif userChoice == "paper" and answer == cpuoption1:
user+=1
print ("Player wins the round!")
elif userChoice == "paper" and answer == cpuoption3:
cpu+=1
print ("CPU wins the round!")
elif userChoice == "scissors" and answer == cpuoption1:
cpu+=1
print ("CPU wins the round!")
elif userChoice == "scissors" and answer == cpuoption2:
user+=1
print ("Player wins the round!")
if cpu==rounds:
print("CPU beat the player!")
elif user==rounds:
print("Player beat the CPU!")
elif cpu==rounds and user==rounds:
print("Looks like that was a draw! Good game!")
You can do it with a counter.
counter = 0
while counter <= rounds:
#code here
counter += 1 #when the game is over
Edit:
import time
import random
print ("Welcome to the Rock, Paper, Scissors Game!")
time.sleep(1)
rounds = int(raw_input("How many rounds would you like to play?"))
counter = 0
while counter <= rounds-1:
while rounds not in range(3,11):
try:
rounds = int(raw_input("Sorry! I can only play between 3 and 10 rounds. Try again:"))
except:
print ("ERROR invalid input. Out of range or wrong type of data.")
if rounds in range(3,11):
time.sleep(1)
print ("Challenge accepted! I will play you for", rounds,"rounds! Let's begin!!!")
options = ("rock","paper","scissors")
userChoice = raw_input("Rock, Paper or Scissors, peasant?")
while userChoice not in options:
try:
userChoice = raw_input("Not an option lad. Rock, paper or scissors")
except:
print ("ERROR invalid input. Out of range or wrong type of data.")
cpuoption1 = "rock"
cpuoption2 = "paper"
cpuoption3 = "scissors"
cpuChoice = random.randint(1,3)
answer = ""
if cpuChoice == 1:
answer = cpuoption1
elif cpuChoice == 2:
answer = cpuoption2
elif cpuChoice == 3:
answer = cpuoption3
print ("I choose", answer,"!")
user= 0
cpu = 0
if userChoice == "rock" and answer == cpuoption1:
user += 1
cpu += 1
print ("Draw!")
counter += 1
elif userChoice == "paper" and answer == cpuoption2:
user+=1
cpu+=1
print("Draw!")
counter += 1
elif userChoice == "scissors" and answer == cpuoption3:
user+=1
cpu+=1
print ("Draw!")
counter += 1
elif userChoice == "rock" and answer == cpuoption2:
cpu+=1
print ("CPU wins the round!")
counter += 1
elif userChoice == "rock" and answer == cpuoption3:
user+=1
print ("Player wins the round!")
counter += 1
elif userChoice == "paper" and answer == cpuoption1:
user+=1
print ("Player wins the round!")
counter += 1
elif userChoice == "paper" and answer == cpuoption3:
cpu+=1
print ("CPU wins the round!")
counter += 1
elif userChoice == "scissors" and answer == cpuoption1:
cpu+=1
print ("CPU wins the round!")
counter += 1
elif userChoice == "scissors" and answer == cpuoption2:
user+=1
print ("Player wins the round!")
counter += 1
if cpu==rounds:
print("CPU beat the player!")
elif user==rounds:
print("Player beat the CPU!")
counter += 1
elif cpu==rounds and user==rounds:
print("Looks like that was a draw! Good game!")
counter += 1
LOOP to play another game
keep_going = True
while keep_going:
# Here, insert all of your code to play one game
response = raw_input("Do you want to play another game? (Y or N)"
keep_going = (response == 'Y')
I see now; you know ahead of time how many rounds you want to play? In that case, use a for loop:
for i in range(rounds):
# Here, insert all of your code to play one game
When you're done with all the games (rounds), you'll want something more like:
if cpu > user:
print("CPU wins the match!")
elif user > cpu:
print("Player wins the match!")
else:
print("Looks like that was a draw! Good match!")
CODING to more easily determine the winner:
choice_list = { "rock":1, "paper":2, "scissors":3}
# input user choice; generate cpu choice as 1, 2, or 3
result_diff = (choice_list[userChoice] - cpuChoice) % 3
if result_diff == 0:
print ("Draw")
elif result_diff == 1:
print ("User wins")
user += 1
else:
print ("cpu wins")
cpu += 1
That's the logic; I'll leave the accounting details to you.

Categories

Resources