I have made a rock paper scissors program as I am just beginning but I have discovered that the 2 positions I tried putting a while True: command have completely opposite effects. I can't understand the logic/reasoning behind it and was hoping someone would be able to explain it in simple terms.
the first copy of the code is the one that functions properly with the while True: on the 3rd line.
the second version of the code is what I had done first with the while True: on the 6th line.
the second version results in the result of the game being printed endlessly so E.G. "You win" a million times whereas when the while True: is on the 3rd line it runs as intended with the win/lose or tie message only printing once.
Can someone explain why that is just so I understand for future reference.
import random
moves = "rock", "paper", "scissors"
while True:
computer = moves[random.randint(0,2)]
player = input("Choose rock, paper or scissors... Or 'end' to end the game: ")
if player == "end":
print("Game over!")
break
elif player == "rock" and computer == "scissors" or player == "scissors" and computer == "paper" or player == "paper" and computer == "rock":
print("You win", player, "beats", computer)
elif player == computer:
print("Tie!")
else:
print("You lose!", computer, "beats", player)
import random
moves = "rock", "paper", "scissors"
computer = moves[random.randint(0,2)]
player = input("Choose rock, paper or scissors... Or 'end' to end the game: ")
while True:
if player == "end":
print("Game over!")
break
elif player == "rock" and computer == "scissors" or player == "scissors" and computer == "paper" or player == "paper" and computer == "rock":
print("You win", player, "beats", computer)
elif player == computer:
print("Tie!")
else:
print("You lose!", computer, "beats", player)
Related
I'm really new to coding and Python and have beent trying the "rock, paper, scissor" game. Everything seems to work except the looping and i'm really struggling to understand why, I thought that setting the player to False would reloop the code?
from random import randint
t = ["rock", "paper", "scissors"]
computer = t[randint(0, 2)]
player = False
while player == False:
player = input("rock, paper or scissors?")
if player == computer:
print("Tie!")
elif player == "rock":
if computer == "paper":
print ("sorry, you lose!", computer, "beats", player)
else:
print("Great, you win!", player, "destroys", computer)
elif player == "paper":
if computer == "scissors":
print("sorry, you lose", computer, "beats", player)
else:
print("Great, you win!", player, "destroys", computer)
elif player == "scissors":
if computer == "rock":
print("sorry, you lose!", computer, "beats", player)
else:
print("Great, you win!", player, "destorys", computer)
else:
print("not a valid input, try again")
player = False
computer = t[randint(0, 2)]
Any help is welcome!
You are overwriting player with an string in the line with input(). Then player is never equal to False. If you want to loop endless you can simply do it with a while True:. Then it is even better readable.
In Python, strings (text) are True as long as they are not empty.
Similarly, lists, sets and other collections are True unless empty.
It has a great advantage once you are aware of it.
Also, your indentation was wrong. You need to move the computer=t[randint(0,2)] into the loop to pick a new choice each time. Setting player=False at the end does not make sense.
from random import randint
t = ["rock", "paper", "scissors"]
player = None
while player != "quit":
computer = t[randint(0, 2)]
player = input("rock, paper or scissors?")
if player == computer:
print("Tie!")
elif player == "rock":
if computer == "paper":
print ("sorry, you lose!", computer, "beats", player)
else:
print("Great, you win!", player, "destroys", computer)
elif player == "paper":
if computer == "scissors":
print("sorry, you lose", computer, "beats", player)
else:
print("Great, you win!", player, "destroys", computer)
elif player == "scissors":
if computer == "rock":
print("sorry, you lose!", computer, "beats", player)
else:
print("Great, you win!", player, "destorys", computer)
elif player != "quit":
print("not a valid input, try again")
python creates code blocks based on indentation.
Look at the indentation of your second player = False statement, that should help :)
Indent these lines so they are in the while loop
player = False
computer = t[randint(0, 2)]
Your Player variable is a boolean and a string
The string is "rock", "paper" or "scissors" so it is bool(Player) = True.
You need 2 variables, game_loop (bool) and player_response (string)
The computer choice have to be in the loop (computer = t[randint(0, 2)])
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 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.
Keep getting this syntax error
TypeError: input expected at most 1 arguments, got 3
Does anyone know how to fix this?
from random import randint
from tkinter import *
po = ["Rock", "Paper", "Scissors"]
player = False
cpu = po[randint(0, 2)]
while player == False:
player = input("Rock", "Paper", "Scissors?")
if player == computer:
print("Tie")
elif player == "Rock":
if computer == "Paper":
print("You lose!", computer, "covers", player)
else:
print("You win!", player, "smashes", computer)
elif player == "Paper":
if computer == "Scissors":
print("You lose!", computer, "cuts", player)
else:
print("You win!", player, "covers", computer)
elif player == "Scissors":
if computer == "Rock":
print("You lose!", computer, "smashes", player)
else:
print("You win!", player, "cut", computer)
else:
print("That's not a valid play. Check your spelling!")
player = False
computer = po[randint(0,2)]
You're misusing input. The argument passed to it is merely the prompt presented, and thus can only be a single string.
player = input("Rock, Paper, Scissors?")
Is probably more like what you want.
It seems that you never really give the person a chance to input wether rock, paper, or scissors, you should probably do something along the lines of
player = str(input("Rock, Paper, or Scissors?"))
That way player is assigned to whichever the player chooses
I am creating a Rock, Paper, Scissors game. The game has a main menu which I need to be able to return to from each sub menu. I've tried a few different method I could think of as well as looked here and elsewhere online to determine a method of solving my problem.
I want the user to be able to select an option from the main menu, go to the selected sub menu, then be prompted with an option to return to the main menu. For example, Select the rules sub menu, then return to the main menu. Or, select to play a round of Rock, Paper, Scissors, then select to play again or return back to the main menu.
# random integer
from random import randint
# list for weapon
WEAPON = ["Rock", "Paper", "Scissors"]
# module to run the program
#def main():
# menu()
def main():
menuSelect = ""
print("\tRock, Paper, Scissors!")
# main menu
print("\n\t\tMain Menu")
print("\t1. See the rules")
print("\t2. Play against the computer")
print("\t3. Play a two player game")
print("\t4. Quit")
menuSelect = int(input("\nPlease select one of the four options "))
while menuSelect < 1 or menuSelect > 4:
print("The selection provided is invalid.")
menuSelect = int(input("\nPlease select one of the four options "))
if menuSelect == 1:
rules()
elif menuSelect == 2:
onePlayer()
elif menuSelect == 3:
twoPlayer()
elif menuSelect == 4:
endGame()
# display the rules to the user
def rules():
print("\n\t\tRules")
print("\tThe game is simple:")
print("\tPaper Covers Rock")
print("\tRock Smashes Scissors")
print("\tScissors Cut Paper")
print("")
# one player mode
def onePlayer():
again = ""
player = False
print("\n\tPlayer VS Computer")
while player == False:
player = input("\nSelect your weapon: Rock, Paper, or Scissors\n")
player = player.lower()
computer = WEAPON[randint(0,2)]
computer = computer.lower()
if player == computer:
print(player," vs ",computer)
print("It's a tie!\n")
elif player == "rock":
if computer == "paper":
print(player," vs ",computer)
print("Paper covers rock! You lose!\n")
else:
print("Rock smashes",computer,". You win!\n")
elif player == "paper":
if computer == "scissors":
print(player," vs ",computer)
print("Scissors cut paper! You lose!\n")
else:
print("Paper covers",computer,". You win!\n")
elif player == "scissors":
if computer == "rock":
print(player," vs ",computer)
print("Rock smashes scissors! You lose!\n")
else:
print("Scissors cut",computer,". You win!\n")
else:
print("invalid input")
again = input("Would you like to play again? Yes or no\n")
again = again.lower()
if again == "yes" or "y":
player = False
elif again == "no" or "n":
main()
# two player mode
def twoPlayer():
fight = False
player1 = ""
player2 = ""
print("\n\tPlayer VS Player")
while fight == False:
player1 = input("\nSelect your weapon: Rock, Paper, or Scissors\n")
player1 = player1.lower()
player2 = input("\nSelect your weapon: Rock, Paper, or Scissors\n")
player2 = player2.lower()
if player1 == player2:
print(player1," vs ",player2)
print("It's a tie!\n")
elif player1 == "rock":
if player2 == "paper":
print(player1," vs ",player2)
print("Paper covers rock! Player 2 wins!\n")
else:
print("Rock smashes",player2,". Player 1 wins!\n")
elif player1 == "paper":
if player2 == "scissors":
print(player1," vs ",player2)
print("Scissors cut paper! Player 2 wins!\n")
else:
print("Paper covers",player2,". Player 1 wins!\n")
elif player1 == "scissors":
if player2 == "rock":
print(player1," vs ",player2)
print("Rock smashes scissors! Player 2 wins!\n")
else:
print("Scissors cut",player2,". Player 1 wins!\n")
else:
print("invalid input")
again = input("Would you like to play again? Yes or no\n")
again = again.lower()
if again == "yes" or "y":
player = False
elif again == "no" or "n":
main()
def endGame():
print("Thank you for playing!")
main()
Currently my only test is within the onePlayer() module. The idea behind my code is to ask the user if they want to continue playing. If they don't want to continue playing, then I want the program to bring them back to the main menu.
You are using player variable for two works, instead of that you can use another variable to just check the condition and another to take user input.
Also you can check the condition like : if again in ["yes","y"]
def main():
menuSelect = ""
print("\tRock, Paper, Scissors!")
# main menu
print("\n\t\tMain Menu")
print("\t1. See the rules")
print("\t2. Play against the computer")
print("\t3. Play a two player game")
print("\t4. Quit")
menuSelect = int(input("\nPlease select one of the four options "))
while menuSelect < 1 or menuSelect > 4:
print("The selection provided is invalid.")
menuSelect = int(input("\nPlease select one of the four options "))
if menuSelect == 1:
rules()
elif menuSelect == 2:
onePlayer()
elif menuSelect == 3:
twoPlayer()
elif menuSelect == 4:
endGame()
# display the rules to the user
def rules():
print("\n\t\tRules")
print("\tThe game is simple:")
print("\tPaper Covers Rock")
print("\tRock Smashes Scissors")
print("\tScissors Cut Paper")
print("")
# one player mode
def onePlayer():
again = ""
player = False
print("\n\tPlayer VS Computer")
while player == False:
player = input("\nSelect your weapon: Rock, Paper, or Scissors\n")
player = player.lower()
#computer = WEAPON[randint(0,2)]
#temporary
computer = "paper"
computer = computer.lower()
if player == computer:
print(player," vs ",computer)
print("It's a tie!\n")
elif player == "rock":
if computer == "paper":
print(player," vs ",computer)
print("Paper covers rock! You lose!\n")
else:
print("Rock smashes",computer,". You win!\n")
elif player == "paper":
if computer == "scissors":
print(player," vs ",computer)
print("Scissors cut paper! You lose!\n")
else:
print("Paper covers",computer,". You win!\n")
elif player == "scissors":
if computer == "rock":
print(player," vs ",computer)
print("Rock smashes scissors! You lose!\n")
else:
print("Scissors cut",computer,". You win!\n")
else:
print("invalid input")
again = input("Would you like to play again? Yes or no\n")
again = again.lower()
if again=="yes" or again=="y":
player = False
else:
player = True
main()
main()
Do a try and except command. If they say no your code should be quit(). If they say yes put a continue command and it will restart the whole thing.
put your main method in a while (True): loop, and if option 4 is called, use break like this:
elif menuSelect == 4:
break
add an indent to
again = input("Would you like to play again? Yes or no\n")
again = again.lower()
if again == "yes" or "y":
player = False
else:
main()
and rather than calling main() just set player = True also, your Weapon array has not been defined. easy fix, just add WEAPON = ["rock", "paper", "scissors"] to the beginning of your onePlayer(): method. I can see another problem, change
if again == "yes" or "y":
to
if again == "yes" or again == "y":
one last thing, don't forget your imports! (put it at the top of your code.)
from random import randint
BTW, the break statement just tells python to leave whatever for loop or while loop it's in.