So I have the rest of the code correct, but it doesn't end and I'm unsure why. It shouldn't be infinite because it is prompted when either variable is over 5 to end yet it continues. Any answers?
import random
Round = 0
Player_Score = 0
Computer_Score = 0
while Player_Score < 5 or Computer_Score < 5:
Player_object = input("Would you like to choose R, P, or S?")
Computer_object = random.sample("RPS", 1)[0]
if Player_object == "R" or Player_object == "r":
if Computer_object == "R":
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have tied with the Computer and neither of you have scored a point.")
elif Computer_object == "P":
Computer_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have been beaten by the Computer and it has scored a point.")
else:
Player_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have beaten the Computer and you have scored a point.")
if Player_object == "P" or Player_object == "p":
if str(Computer_object) == "R":
Player_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have beaten the Computer and you have scored a point.")
elif str(Computer_object) == "P":
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have tied with the Computer and neither of you have scored a point.")
else:
Computer_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have been beaten by the Computer and it has scored a point.")
if Player_object == "S" or Player_object == "s":
if str(Computer_object) == "R":
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have been beaten by the Computer and it has scored a point.")
elif str(Computer_object) == "P":
Computer_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have beaten the Computer and you have scored a point.")
else:
Player_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have tied with the Computer and neither of you have scored a point.")
if Computer_Score == 5 and Player_Score != 5:
print("The Computer has won!")
if Player_Score == 5 and Computer_Score != 5:
print("You have won and beaten the computer")
Your loop ends when both are higher than 5, if you want it to stop when either are higher then 5 you need to have the following in your main for loop:
while Player_Score < 5 and Computer_Score < 5
Related
So i tried making a rock paper scissors game but some if statements are not working. code was written in python.
Is there something preventing the if statements ffrom running? or s there another problem
I tried a bunch of little changes but none of them work
code:
import random
moves = ('rock', 'paper', 'scissors')
while True:
print("rock, paper, scissors. Go! ")
userInput = input("Choose your move: ")
botInput = random.choice(moves)
if userInput == botInput:
print(userInput + " VS " + botInput)
print("DRAW")
if userInput == "paper" and botInput == "rock":
print(userInput + " VS " + botInput)
print("Player Wins!")
if userInput == "scissors" and botInput == "paper":
print(userInput + " VS " + botInput)
print("Player Wins!")
if userInput == "rock" and botInput == "scissors":
print(userInput + " VS " + botInput)
print("Player Wins!")
if userInput == "rock" and botInput == "paper":
print(userInput + " VS " + botInput)
print("Bot Wins!")
if userInput == "paper" and botInput == "scissors":
print(userInput + " VS " + botInput)
print("Bot Wins!")
if userInput == "scissors" and botInput == "rock":
print(userInput + " VS " + botInput)
print("Bot Wins!")
print("Wanna Rematch?")
decision = input("Yes or No? ")
if decision == "Yes":
pass
elif decision == "No":
break
Here's my solution to this. I cleaned up the amount of if-else statements in your code as well with a variable outcome_map. I also removed a lot of the typing rarities that come with inputs by introducing your choice to be "(1, 2, or 3)":
import random
moves = ('rock', 'paper', 'scissors')
while True:
print("Rock, paper, scissors. Go! ")
user_input = int(input("Choose your move (1:rock, 2:paper, 3:scissors): "))
if user_input not in [1, 2, 3]:
print("Invalid input. Try again...")
continue
bot_move = random.choice(moves)
user_move = moves[user_input-1]
outcome_map = { # item A vs item B -> Outcome for A
"rock":{
"rock":"Draw",
"paper":"Lose",
"scissors":"Win",
},
"paper":{
"rock":"Win",
"paper":"Draw",
"scissors":"Lose",
},
"scissors":{
"rock":"Lose",
"paper":"Win",
"scissors":"Draw",
}
}
outcome = outcome_map[user_move][bot_move]
print("Player: ", outcome, "!")
decision = input("Wanna Rematch? (y/n) ")
if decision in ["y", "yes", "Y", "YES"]:
pass
else:
print("Exiting... ")
break
Hope this helps!
I think there's nothing wrong with your code... however, it does not handle specific inputs, maybe that is your problem, here is my solution:
import random
moves = ('rock', 'paper', 'scissors')
while True:
print("rock, paper, scissors. Go! ")
userInput = input("Choose your move: ").lower()
botInput = random.choice(moves)
#check if input is valid
if userInput not in moves:
print("Choose a valid move! Wanna try again?")
decision = input("Yes or No? ").lower()
if decision == "yes" or decision == 'y': continue
# skips the remaining body of the loop
else: break
elif userInput == botInput:
print(userInput + " VS " + botInput)
print("DRAW")
elif userInput == "paper" and botInput == "rock":
print(userInput + " VS " + botInput)
print("Player Wins!")
elif userInput == "scissors" and botInput == "paper":
print(userInput + " VS " + botInput)
print("Player Wins!")
elif userInput == "rock" and botInput == "scissors":
print(userInput + " VS " + botInput)
print("Player Wins!")
elif userInput == "rock" and botInput == "paper":
print(userInput + " VS " + botInput)
print("Bot Wins!")
elif userInput == "paper" and botInput == "scissors":
print(userInput + " VS " + botInput)
print("Bot Wins!")
elif userInput == "scissors" and botInput == "rock":
print(userInput + " VS " + botInput)
print("Bot Wins!")
print("Wanna Rematch?")
decision = input("Yes or No? ").lower()
if decision == "yes" or decision == 'y': continue
else: break
I hope you understand what I did here:
moves = ('rock', 'paper', 'scissors')
decision = "Nothing"
while decision != "No":
decision = "Nothing"
print("rock, paper, scissors. Go! ")
userInput = input("Choose your move: ")
botInput = random.choice(moves)
if userInput == "paper":
if botInput == "rock":
print(userInput + " VS " + botInput)
print("Player Wins!")
elif botInput == "paper":
print(userInput + " VS " + botInput)
print("DRAW")
elif botInput == "scissors":
print(userInput + " VS " + botInput)
print("Bot Wins!")
else:
print("Invalid choice.")
if userInput == "scissors":
if botInput == "rock":
print(userInput + " VS " + botInput)
print("Bot Wins!")
elif botInput == "paper":
print(userInput + " VS " + botInput)
print("Player Wins!")
elif botInput == "scissors":
print(userInput + " VS " + botInput)
print("DRAW")
if userInput == "rock":
if botInput == "rock":
print(userInput + " VS " + botInput)
print("DRAW")
elif botInput == "paper":
print(userInput + " VS " + botInput)
print("Bot Wins!")
elif botInput == "scissors":
print(userInput + " VS " + botInput)
print("Player Wins!")
while decision != "Yes" and decision != "No":
print("Wanna Rematch?")
decision = input("Yes or No? ")
if decision == "Yes":
pass
elif decision == "No":
break
else:
print("Invalid answer.")
I am creating a rock,paper,scissors game but sometimes my output shows and sometimes it doesn't what's seems to be the problem here?
So far this is my code:
import random
round = 1
win = 0
lose = 0
tie = 0
while True:
choices = ["rock", "paper", "scissors"]
computer = random.choice(choices)
player = None
while player not in choices:
player = input("Rock, paper, or scissors??: ").lower()
if player == computer:
print("Player : ", player)
print("Computer: ", computer)
print("Tie!")
tie+=1
elif player == "rock":
if computer == "paper":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose+=1
elif player == "paper":
if computer == "rock":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win+=1
elif player == "scissors":
if computer == "rock":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose+=1
elif player == "rock":
if computer == "scissors":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win+=1
elif player == "paper":
if computer == "scissors":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose+=1
elif player == "scissors":
if computer == "paper":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win+=1
play_again = input("Would you like to play again? (yes/no): ").lower()
if play_again != "yes":
print("ROUNDS PLAYED: ",round)
print("TOTAL WINS:",win)
print("TOTAL LOSES: ",lose)
print("TOTAL TIME PLAYER TIED WITH COMPUTER: ",tie)
break
elif play_again == "yes":
rounds = round + 1
print("NEW ROUND, ROUND: ",rounds)
print("Bye, Thanks for playing this program")
now if I were to choose paper it won't work it will show me something like this:
(also I have realized that my rounds doesn't count.)
Rock, paper, or scissors??: paper
Would you like to play again? (yes/no): yes
NEW ROUND, ROUND: 2
Rock, paper, or scissors??: paper
Would you like to play again? (yes/no): yes
NEW ROUND, ROUND: 2
Rock, paper, or scissors??: scissors
Player : scissors
Computer: scissors
Tie!
Would you like to play again? (yes/no): yes
NEW ROUND, ROUND: 2
Rock, paper, or scissors??: rock
Player : rock
Computer: rock
Tie!
Would you like to play again? (yes/no): yes
NEW ROUND, ROUND: 2
Rock, paper, or scissors??: paper
Would you like to play again? (yes/no): no
ROUNDS PLAYED: 1
TOTAL WINS: 0
TOTAL LOSES: 0
TOTAL TIME PLAYER TIED WITH COMPUTER: 2
Bye, Thanks for playing this program
I am new to programming any help would be highly appreciated.
You have two different elif block with same conditions. For Example:
elif player == "scissors":
if computer == "rock":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose += 1
elif player == "scissors":
if computer == "paper":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win += 1
In this case, if player choose scissors what is gonna happen?
Python starts to search for this condition but only the first one will be valid.
Thus, you should construct your elif block like this:
elif player == "scissors":
if computer == "paper":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win += 1
elif computer == "rock":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose += 1
Let's look at your second question.
You have a variable named round and rounds. These are same I guess but you are incrementing rounds by the line rounds = round+1.
Instead, try
round = round+1
or
round+=1
Hope this helps.
Your code is not working sometimes because you have not applied all the condition for example what if player choses paper and computer choses rock.
Also the number of round is not increasing because you are saving it in new variable every time, hence not increasing .
rounds = round + 1
above line of code should be
round = round + 1
If you put the same condition more than once in any if-elif structure, only the first conditional if/elif branch will satisfy and the latter will never be executed. For example, in your code in the if-else statement, you have written the same condition elif player == "rock" twice.
elif player == "rock":
if computer == "paper":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose+=1
elif player == "paper":
if computer == "rock":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win+=1
elif player == "scissors":
if computer == "rock":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose+=1
elif player == "rock":
if computer == "scissors":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win+=1
Say, your player has entered rock and computer has chosen scissors. When the code executes, only the first elif player == "rock" will be evaluated, inside this statement, you only checked if the computer has chosen paper or not. But computer has chosen scissors which you did not check in this statement. So, the interpreter will do nothing and skip the rest of the if-else condition.
To make it work, you need to use only one else statement for the same condition, like this:
elif player == "rock":
if computer == "paper":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose+=1
elif computer == "scissors":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win+=1
You need to follow this for all the possible outcomes.
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
So I'm trying to keep track of what is played in a round by each player and who wins each round in my R/P/S program. Is there a way to do that in a, possibly, infinitely repeating loop? Here's the code
import random
Round = 0
Player_Score = 0
Computer_Score = 0
while Player_Score < 5 and Computer_Score < 5:
Player_object = input("Would you like to choose R, P, or S?")
Computer_object = random.sample("RPS", 1)[0]
if Player_object == "R" or Player_object == "r":
if Computer_object == "R":
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have tied with the Computer and neither of you have scored a point.")
elif Computer_object == "P":
Computer_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have been beaten by the Computer and it has scored a point.")
else:
Player_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have beaten the Computer and you have scored a point.")
if Player_object == "P" or Player_object == "p":
if str(Computer_object) == "R":
Player_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have beaten the Computer and you have scored a point.")
elif str(Computer_object) == "P":
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have tied with the Computer and neither of you have scored a point.")
else:
Computer_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have been beaten by the Computer and it has scored a point.")
if Player_object == "S" or Player_object == "s":
if str(Computer_object) == "R":
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have been beaten by the Computer and it has scored a point.")
elif str(Computer_object) == "P":
Computer_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have beaten the Computer and you have scored a point.")
else:
Player_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have tied with the Computer and neither of you have scored a point.")
if Computer_Score == 5 and Player_Score != 5:
print("The Computer has won!")
if Player_Score == 5 and Computer_Score != 5:
print("You have won and beaten the computer")
R = "Rock"
r = "Rock"
P = "Paper"
p = "Paper"
S = "Scissors"
s = "Scissors"
The simplest way to handle something like this is to use a list where you would store the information you want in each round.
Before your loop, create an empty list
Rounds = []
and at the end of your loop, append the relevant information.
You can use a tuple to add multiple pieces of information as a single entry in the list. For example, to add what is played by each player, you could write
Rounds.append((Player_object, Computer_object))
If you want to keep track of who won the round, add this information in a variable in your if/else and add it to the tuple.
Once your loop is over, you can access the information of each round by accessing this list. List indexes start at 0, so to access the information of the first round, you would write Rounds[0]. Each element in a tuple can also be accessed by index, so the Player_object of round 1 can be accessed using Rounds[0][0].
If you need more information, try to search for lists and tuples in Python.
For each round, you want to store two things, right? The players and their moves, and their winner. In a round, a player moves only once, so we would want a tuple of (player, move) in our data structure. There could be many players participating in one round, so using a list would be most suitable. Therefore in each round, you can have a list of tuples of (player, move).
Since with each round, you want to keep track of the winner also, you can make a tuple to represent a round: ([list of players and moves], winner).
Finally, you want a list of these! Define this list outside the game loop, but append the data of a round at the end of each loop to this list. H
Hope this helps :)
infinity loop......here you go
import random
Round = {}
while True:
cnt = 1
ans = raw_input("Wanna Play RPS (y/n) ? ")
if ans == 'y' or ans == 'Y':
Player_Score = 0
Computer_Score = 0
while Player_Score < 5 and Computer_Score < 5:
Player_object = raw_input("Would you like to choose R, P, or S? ")
Computer_object = random.sample("RPS", 1)[0]
if Player_object == "R" or Player_object == "r":
if Computer_object == "R":
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ".You have tied with the Computer and neither of you have scored a point.")
elif Computer_object == "P":
Computer_Score += 1
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ". You have been beaten by the Computer and it has scored a point.")
else:
Player_Score += 1
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ".You have beaten the Computer and you have scored a point.")
if Player_object == "P" or Player_object == "p":
if str(Computer_object) == "R":
Player_Score += 1
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ".You have beaten the Computer and you have scored a point.")
elif str(Computer_object) == "P":
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ". You have tied with the Computer and neither of you have scored a point.")
else:
Computer_Score += 1
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ".You have been beaten by the Computer and it has scored a point.")
if Player_object == "S" or Player_object == "s":
if str(Computer_object) == "R":
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ".You have been beaten by the Computer and it has scored a point.")
elif str(Computer_object) == "P":
Computer_Score += 1
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ". You have beaten the Computer and you have scored a point.")
else:
Player_Score += 1
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ".You have tied with the Computer and neither of you have scored a point.")
if Computer_Score == 5 and Player_Score != 5:
Round[cnt] = "Computer"
print("The Computer has won!")
elif Player_Score == 5 and Computer_Score != 5:
Round[cnt] = "Player"
print("You have won and beaten the computer")
else:
break
cnt += 1
R = "Rock"
r = "Rock"
P = "Paper"
p = "Paper"
S = "Scissors"
s = "Scissors"
for i in Round:
print "Round ", i, "won by ", Round[i], "\n"
I have just started my first language: python. So I started practicing it and tried writing a rock, paper, scissors game. I know I'm not the best but can't seem to make the game end when the us_count and comp_count reached the max
import random
us_count = 0
comp_count = 0
gamelim = None
def GameConfig(gamelim,us_count,comp_count):
gamelim = input("How long do you want the game to be: \n")
Game(gamelim,us_count,comp_count)
def GameLoop(gamelim,us_count,comp_count):
if us_count > comp_count and us_count + comp_count == gamelim:
print("You have the best ",us_count," out of ",gamelim)
GameEnd()
elif us_count < comp_count and us_count + comp_count == gamelim:
print("Computer has bested ",comp_count," out of ",gamelim)
GameEnd()
elif us_count == comp_count and us_count + comp_count == gamelim:
print("Game tied.")
GameEnd()
else:
Game(gamelim,us_count,comp_count)
def GameEnd():
gamecont = print("Game has ended do you want to play again?")
end
def Game(gamelim,us_count,comp_count):
us_choice = str(input("Please select rock, paper or scissors: \n"))
choices = ["rock","paper","scissors"]
comp_choice = random.choice(choices)
if us_choice == comp_choice:
print("Computer choses " + comp_choice + "\nIt'a tie.")
elif us_choice == "rock" and comp_choice == "scissors":
print("Computer choses " + comp_choice + "\nYou won.")
us_count = us_count + 1
elif us_choice == "rock" and comp_choice == "paper":
print("Computer choses " + comp_choice + "\nYou lost.")
comp_count = comp_count + 1
elif us_choice == "paper" and comp_choice == "rock":
print("Computer choses " + comp_choice + "\nYou won.")
us_coun = us_count + 1
elif us_choice == "paper" and comp_choice == "scissors":
print("Computer choses " + comp_choice + "\nYou lost.")
comp_count = comp_count + 1
elif us_choice == "scissors" and comp_choice == "paper":
print("Computer choses " + comp_choice + "\nYou won.")
us_count = us_count + 1
elif us_choice == "scissors" and comp_choice == "rock":
print("Computer choses " + comp_choice + "\nYou lost.")
comp_count = comp_count + 1
else:
print("Please enter a corret value")
GameLoop(gamelim,us_count,comp_count)
GameConfig(gamelim,us_count,comp_count)