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")
Related
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()
I am very new to Python and coding in general. I tried to create my own Rock, Paper, Scissors game. The issue I'm getting can be tested by doing the following: typing "rock", then typing "yes" to play again and repeating those two steps. Eventually, it gives me an "Invalid Input." It is only supposed to print this if the user types something besides rock, paper, or scissors. Is there any way I can fix this? I'd really appreciate any input and advice. Thank you for your time.
import random
# to loop the program
def Repeat():
Answer = input ("Do you want to play again? (Y/N): ")
if Answer == ("Yes") or Answer == ("yes") or Answer == ("Y") or Answer == ("y"):
Game()
if Answer == ("No") or Answer == ("no") or Answer == ("N") or Answer == ("n"):
exit()
else:
print ("Invalid Input")
Repeat()
def Game():
# Variable Reset
Choice = None
Opponent_Choice = None
# Choices
Opponent_Choices = ["Rock", "Paper", "Scissors"]
Choice = input ("Type Rock, Paper, or Scissors: ")
Opponent_Choice = random.choice(Opponent_Choices)
# Outcomes
# if you choose rock
if Choice == "Rock" or Choice == "rock":
if Opponent_Choice == "Paper":
print ("your opponent chose Paper. You lose!")
Repeat()
elif Opponent_Choice == "Scissors":
print ("your opponent chose Scissors. You win!")
Repeat()
elif Opponent_Choice == "Paper":
print ("your opponnent chose Rock. It was a tie!")
Repeat()
# if you choose paper
elif Choice == "Paper" or Choice == "paper":
if Opponent_Choice == "Paper":
print ("your opponent chose Paper. It was a tie!")
Repeat()
elif Opponent_Choice == "Scissors":
print ("your opponent chose Scissors. You lose!")
Repeat()
elif Opponent_Choice == "Paper":
print ("your opponent chose Rock. You win!")
Repeat()
# if you choose scissors
elif Choice == "Scissors" or Choice == "scissors":
if Opponent_Choice == "Paper":
print ("your opponent chose Paper. You win!")
Repeat()
elif Opponent_Choice == "Scissors":
print ("your opponent chose Scissors. It was a tie!")
Repeat()
elif Opponent_Choice == "Paper":
print ("your opponet chose Rock. You lose!")
Repeat()
else:
print ("Invalid input")
Game()
Game()
p.s. Sorry for the messy code :P Still very new to this.
The actual issue:
The actual issue
The reason you seem to be getting the invalid input is due to your check for the Opponent_choice not checking for Rock but instead checking for paper twice. Therefore it jumps all the way to the else statement at the end of the function which contains the invalid input print statement if the opponent chooses Rock.
if Opponent_Choice == "Paper":
print ("your opponent chose Paper. You lose!")
Repeat()
elif Opponent_Choice == "Scissors":
print ("your opponent chose Sciccors. You win!")
Repeat()
elif Opponent_Choice == "Paper": #error here
print ("your opponnet chose Rock. It was a tie!")
Repeat()
The fix would be
if Opponent_Choice == "Paper":
print ("your opponent chose Paper. You lose!")
Repeat()
elif Opponent_Choice == "Scissors":
print ("your opponent chose Sciccors. You win!")
Repeat()
elif Opponent_Choice == "Rock": #change made here
print ("your opponnet chose Rock. It was a tie!")
Repeat()
One problem with the code is that you have functions calling themselves.
The function Game() and Repeat() in your code do this. This is called recursion and can be very useful. But it can also cause problems if used incorrectly. For one thing, let's consider the code above DOES work, if you played it for very long, you would get a stack overflow exception. I would suggest some research on recursion and "the stack" to understand this better.
If you take out the recursion like so for example:
it should work.
I also noticed you are checking user input on the confirmation question. Very nice. But why not for the Rock Paper Scissors selection?
Example refactor without recursion:
import random
# to loop the program
def Repeat():
while True:
result = Confirm()
if (result is True):
break
def Confirm():
Answer = input ("Do you want to play again? (Y/N): ")
if Answer == ("Yes") or Answer == ("yes") or Answer == ("Y") or Answer == ("y"):
return True
if Answer == ("No") or Answer == ("no") or Answer == ("N") or Answer == ("n"):
exit()
else:
return False
def Game():
# Choices
Opponent_Choices = ["Rock", "Paper", "Scissors"]
Choice = input("Type Rock, Paper, or Scissors: ")
Opponent_Choice = random.choice(Opponent_Choices)
# Outcomes
# if you choose rock
if Choice == "Rock" or Choice == "rock":
if Opponent_Choice == "Paper":
print ("your opponent chose Paper. You lose!")
Repeat()
elif Opponent_Choice == "Scissors":
print ("your opponent chose Sciccors. You win!")
Repeat()
elif Opponent_Choice == "Paper":
print ("your opponnet chose Rock. It was a tie!")
Repeat()
# if you choose paper
elif Choice == "Paper" or Choice == "paper":
if Opponent_Choice == "Paper":
print ("your opponent chose Paper. It was a tie!")
Repeat()
elif Opponent_Choice == "Scissors":
print ("your opponent chose Sciccors. You lose!")
Repeat()
elif Opponent_Choice == "Paper":
print ("your opponet chose Rock. You win!")
Repeat()
# if you choose scissors
elif Choice == "Scissors" or Choice == "scissors":
if Opponent_Choice == "Paper":
print ("your opponent chose Paper. You win!")
Repeat()
elif Opponent_Choice == "Scissors":
print ("your opponent chose Sciccors. It was a tie!")
Repeat()
elif Opponent_Choice == "Paper":
print ("your opponet chose Rock. You lose!")
Repeat()
else:
print ("Invalid input")
while True:
Game()
I can't give you an answer about why there's an error, but here is a clue: the 'invalid input' message you get was from line 11, in Repeat(). The line will only be run if the if statement above it evaluates to false, and the if statement will only be run if the other if statement above it exits, which means you'll only get the message if the Game()-function somehow exited, which shouldn't happen. You can add a print()-statement after line 7 to confirm that. The REAL reason was because you used recursion, like #dominic has said. And you should try their code.
PS: don't feel bad about messy code. Mine is worse than yours. But you really don't have to capitalize the variables and function. Good luck with Python!
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
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.