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()
Related
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
main.py:
import functions
def main():
count = True
while count == True:
print("Enter R for Rock")
print("Enter P for Paper")
print("Enter S for Scissors")
value = input("Choose from R/P/S: ") # taking input from the user, they can tpye R/P/S or r/p/s to play a game. Rock Paper Scissors
if value == "R" or value =="r":
print()
print(functions.rock(value))
elif value == "P" or value =="p":
print()
print(functions.paper(value))
elif value == "S" or value =="s":
print()
print(functions.scissors(value))
#Using If statement so if the user input is "R" then it will call the rock fucntion, if its "P" it will call the paper function and if its "S" it will call the scissors function.
else:
print()
print("Invalid Input, Please enter R for Rock, P for Paper, S for Scissors")
# if there's an invalid input the program will stop and redirect them to the correct input process
break
print("------------")
main()
function.py:
import random
def computer_choice():
choice = random.randint(1, 3)
if choice == 1:
return "Rock"
elif choice == 2:
return "Paper"
elif choice == 3:
return "Scissors"
# getting a random string from a,b,c variable. The random variable will be the computers input
def rock(value):
print("Computer choice:", computer_choice())
print()
if computer_choice() == "Rock":
return("Its a draw")
elif computer_choice() == "Paper":
return("The computer wins")
else:
return("You Win")
def paper(value):
print("Computer choice:", computer_choice())
print()
if computer_choice() == "Rock":
return("You Win")
elif computer_choice() == "Paper" :
return("Its a draw")
else:
return("The computer wins")
def scissors(value):
print("Computer choice:", computer_choice())
print()
if computer_choice() == "Rock":
return("The computer wins")
elif computer_choice() == "Paper":
return("You win")
else:
return("Its a draw")
# defining all the functions and giving each function a decision structure and the pattern for rock paper scissors.
Why is a random result for the code being displayed? The only thing random was supposed to be the choice of the computer but the results are random too The code seems to send random results from the functions where I used the random function. The "function.py" returns a random result and not the one that was supposed to be send
Every time you use computer_choice() in an if statement it is generating a new random choice because you are calling the function. Instead you want to make a variable and assign computer_choice() to it, then refer to this variable in your if statements.
For example rock would become:
def rock(value):
comp = computer_choice()
print("Computer choice:", comp)
print()
if comp == "Rock":
return("Its a draw")
elif comp == "Paper":
return("The computer wins")
else:
return("You Win")
I have written logic in python for the game of rock, paper , scissor but having trouble to get it to run.
It wont run the through the entire game and just keeps asking me for my input. I have looked through it multiple times but cant see where it might be getting stuck.
Any idea what the issue may be?
Code is as below:
import random
comp_wins = 0
player_wins = 0
def choose_option ():
user_choice = input("choose rock, paper and scissors: ")
if user_choice in ["rock","Rock"]:
user_choice = "rock"
elif user_choice in ["paper","Paper"]:
user_choice = "paper"
elif user_choice in ["scissors","Scissor"]:
user_choice = "scissors"
else:
print ("error try again")
return user_choice
def computer_option ():
comp_choice = random.randint(1,3)
if comp_choice ==1:
comp_choice = "rock"
elif comp_choice ==2:
comp_choice = "paper"
elif comp_choice ==3:
comp_choice = "scissors"
return comp_choice
while True:
print("")
user_choice = choose_option ()
comp_choice = computer_option ()
print ("")
if user_choice ==["rock"]:
if comp_choice == "rock":
print("draw")
elif comp_choice == "paper":
print("computer wins")
comp_wins += 1
elif comp_choice == "scissors":
print("user wins")
player_wins += 1
elif user_choice == "paper":
if comp_choice == "rock":
print("user wins")
player_wins += 1
elif comp_choice == "paper":
print("draw")
elif comp_choice == "scissors":
print("computer wins")
comp_wins += 1
elif user_choice == "scissors":
if comp_choice == "rock":
print("computer wins")
comp_wins += 1
elif comp_choice == "paper":
print("user wins")
player_wins += 1
elif comp_choice == "scissors":
print("draw")
print("")
print("user_wins: " + str(player_wins))
print("comp_wins: " + str(comp_wins))
print("")
Youur issue is here
while True:
print("")
user_choice = choose_option ()
comp_choice = computer_option ()
print ("")
You never exit this while, you should put an exit condition or a flag when a choice has been made.
Also it would be better to clean up and use a Top-Down structure with a main function in which you control the file.
It looks that there is a wrong indent: the main while True loop never ends. Try to move all blocks after this loop to the right (i.e. add four spaces in the beginning of all next lines).
Your indentation levels aren't correct. The if user_choice ==["rock"]: part is the same indentation level as the while statement, so it isn't included in the loop. You need to indent it to let Python know it's part of the loop block. And since you don't have anything that ends the loop, it will go on forever. Also, using strings to represent choices is really inefficient. If you represent choices with numbers, you can use modular arithmetic.
I have created a rock, paper scissors game in Python, posted it on replit, and now the 'if statements' refuse to function.
print("Welcome to the official game of Rock, Paper, Scissors!")
option = input('''Pick an option: Rock, Paper, Scissors
>>> ''').lower()
import random
game_op =["rock", "paper", "scissors"]
computer_option = print(random.choice(game_op))
def options():
if option == "rock" and computer_option == "rock":
print("You and the Computer chose the same options.")
print('The end')
elif option == "rock" and computer_option == "paper":
print("The Computer wins!")
print('The end')
elif option == "rock" and computer_option == "scissors":
print("You win!")
print('The end')
elif option == "paper" and computer_option == "rock":
print("You win!")
print('The end')
elif option == "paper" and computer_option == "paper":
print("You and the Computer chose the same options.")
print('The end')
elif option == "paper" and computer_option == "scissors":
print("The Computer wins!")
print('The end')
elif option == "scissors" and computer_option == "rock":
print("The Computer wins!")
print('The end')
elif option == "scissors" and computer_option == "scissors":
print("You and the Computer chose the same options.")
print('The end')
elif option == "scissors" and computer_option == "paper":
print("You win!")
print('The end')`enter code here`
print(options())
I tried making a function to handle the if statements and when you and the computer have decided on your move, the function works, but all its printing is 'None'.
In your code, you have the line option.lower. This does not call any function nor does it reassign option to anything. Thus, if the user inputs "ROCK", all the if and elif clauses will be ignored. Rewrite the first few lines to:
print("Welcome to the official game of Rock, Paper, Scissors!")
option = input('''Pick an option: Rock, Paper, Scissors
>>> ''').lower()
Note that inputting something like "banana" will still cause all of the if and elif clauses to be ignored.
I am creating a python code for rock, paper, scissors but I can't seem to keep track of the score of wins, losses, and ties. I think there is something wrong with my "count" which i called "win += 0", "lose += 0" and "tie += 0." Could someone please tell me what I should do to increase the score by 1 every time there is a win, lose or tie?
Here is my code below:
from random import randint
t = ["rock", "paper", "scissors"]
computer = t[randint(0,2)]
player = False
lose = 0
win = 0
for i in range(0,10):
print("1... 2... 3... go!")
while player == False:
player = input("rock, paper, scissors: ")
print("Computer: ", computer)
print("User: ", player)
if player == computer:
tie = 0
tie +=1
print("Tie!")
elif player == "rock":
if computer == "paper":
lose +=0
print("You lose!")
else:
win +=0
print("You win!")
elif player == "paper":
if computer == "scissors":
lose = 0
lose +=0
print("You lose!")
else:
win +=0
print("You win!")
elif player == "scissors":
if computer == "rock":
lose +=0
print("You lose!")
else:
win +=0
print("You win!")
else:
print("That's not a valid play. Check your spelling!")
player = False
computer = t[randint(0,2)]
break
print("Final Tally")
print("************")
print("User Wins: ", win)
print("Computer Wins: ", lose)
print("Ties: ", tie)
if tie > win and tie > lose:
print("It's a tie!")
elif win > tie and win > lose:
print("You won!")
else:
print("The computer won!")
Here's the fixed version. I suggest you work on it some more :)
from random import choice
t = ["rock", "paper", "scissors"]
tie = 0
lose = 0
win = 0
for i in range(0, 10):
print("1... 2... 3... go!")
# you need to refresh these variables on every for iteration
computer = choice(t)
player = None
# if you're using while to make sure player inputs, that's the only thing that needs
# to be within the while loop
while not player:
player = input("rock, paper, scissors: ")
print("Computer: ", computer)
print("User: ", player)
# I would look for a way to simplify determining the winner
if player == computer:
# tie += 1 is the same as tie = tie + 1
tie +=1
print("Tie!")
elif player == "rock":
if computer == "paper":
lose += 1
print("You lose!")
else:
win += 1
print("You win!")
elif player == "paper":
if computer == "scissors":
lose += 1
print("You lose!")
else:
win += 1
print("You win!")
elif player == "scissors":
if computer == "rock":
lose += 1
print("You lose!")
else:
win += 1
print("You win!")
else:
print("That's not a valid play. Check your spelling!")
print("Final Tally")
print("************")
print("User Wins: ", win)
print("Computer Wins: ", lose)
print("Ties: ", tie)
if tie > win and tie > lose:
print("It's a tie!")
elif win > tie and win > lose:
print("You won!")
else:
print("The computer won!")
UPDATE: Apparently I have nothing to do. So ok, here's a straightforward way to simplify win conditioning.
win_condition_rock = player == 'rock' and computer == 'scissors'
win_condition_paper = player == 'paper' and computer == 'rock'
win_condition_scissors = player == 'scissors' and computer == 'paper'
if player == computer:
# tie += 1 is the same as tie = tie + 1
tie +=1
print("Tie!")
elif any([win_condition_paper, win_condition_scissors, win_condition_rock]):
win += 1
print('You win')
else:
lose += 1
print('You lose')
UPDATE 2: And here's a check for valid input
while player not in t:
player = input("rock, paper, scissors: ").lower()
if player not in t:
print('Invalid input')