Way to keep track of rounds during a while loop - python

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"

Related

Making a leaderboard which resorts itself everytime someone plays

I am learning python coming from C++ and HTML, after studying I decided a good way to exercise was making a simple game then slowly developing. I made it this far, but got stuck on a few things: 1. storing the "player" class and its atributes in a txt file 2. adding the new players everytime someone else with a different name plays 3. sorting the list everytime someone new is added 4. displaying the list when a keyword is input from the keyboard ( split it in small steps as i always do when coding. )
Please help and explain the steps, here is the base code:
input('Welcome to rock, paper, scissors version 1.3!(contact me for updates) Press any key to continue.')
w = 0
l = 0
t = 0
class player:
def __init__(self, name, wins, losses, ties):
self.name = name
self.wins = wins
self.losses = losses
self.ties = ties
def func1():
global w, l, t
x=input('Choose: rock, paper or scissors?')
from numpy import random
y = (random.randint(1, 3))
if (x == "scissors") or (x == "rock") or (x == "paper"):
if y == 1:
y = "scissors"
if y == 2:
y = "rock"
if y == 3:
y = "paper"
else: print("Error: you did not type your answer correctly: check so it is lowercase and spells the word correctly(as described in the question) and try again.")
if x == "rock" :
if y == "paper" :
print("computer chose " + y + " so you lost!")
l = l + 1
else:
if y == "rock" :
print("computer chose " + y + " so it is a tie!")
t = t + 1
else:
print("computer chose " + y + " so you win!")
w = w + 1
if x == "paper" :
if y == "scissors" :
print("computer chose " + y + " so you lost!")
l = l + 1
else:
if y=="paper" :
print("computer chose " + y + " so it is a tie!")
t = t + 1
else:
print("computer chose " + y + " so you win!")
w = w + 1
if x == "scissors" :
if y == "rock" :
print("computer chose " + y + " so you lost!")
l = l + 1
else:
if y == "scissors" :
print("computer chose " + y + " so it is a tie!")
t = t + 1
else:
print("computer chose " + y + " so you win!")
w = w + 1
n=input('Play again?(y/n)')
if n == "y":
n = 0
func1()
if n == "n":
print("You won " + str(w) + " times, lost " + str(l) + " times and it was a tie " + str(t) + " times.")
p1 = player(input("Please type your name:"),w,l,t)
input('Thanks for playing! Press any key to exit.')
func1()

How do i properly call this function in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm a beginner in python and having some difficulties on how to call a function for a rock, paper and scissor game. The code is as below:
def game:
your = input("Rock, Paper or Scissor? ")
your.lower()
list = ["rock", "paper", "scissor"]
import random
a = random.randint(0, 2)
weapon = list[a]
if your == weapon:
print("You chose " + your + " and I chose " + weapon + " so it's a draw.")
elif your == "rock" and weapon == "paper":
print("You chose " + your + " and I chose " + weapon + " so I won.")
elif your == "paper" and weapon == "scissor":
print("You chose " + your + " and I chose " + weapon + " so I won.")
elif your == "scissor" and weapon == "rock":
print("You chose " + your + " and I chose " + weapon + " so I won.")
else:
print("You chose " + your + " and I chose " + weapon + " so you won.")
choice = input("Would you like to play again?(y/n) ")
if choice == "y":
game()
else:
print("Thank you for playing.")
You need brackets, like this:
def game():
to define a function. And .lower() only returns the string in lower case, it doesn't override.
your = input("Rock, Paper or Scissor? ").lower()
This is how function calls work in Python:
#Defining the function
def myFunction():
print("Hello world")
#Calling the function. Yes, it is this simple.
myFunction()
For creating functions with parameters
def myFuncWithParams(Word):
print("Hello" + Word)
myFuncWithParams("World")
Output:
Hello World
In your case:
def game():
your = input("Rock, Paper or Scissor? ")
your.lower()
list = ["rock", "paper", "scissor"]
import random
a = random.randint(0, 2)
weapon = list[a]
if your == weapon:
print("You chose " + your + " and I chose " + weapon + " so it's a draw.")
elif your == "rock" and weapon == "paper":
print("You chose " + your + " and I chose " + weapon + " so I won.")
elif your == "paper" and weapon == "scissor":
print("You chose " + your + " and I chose " + weapon + " so I won.")
elif your == "scissor" and weapon == "rock":
print("You chose " + your + " and I chose " + weapon + " so I won.")
else:
print("You chose " + your + " and I chose " + weapon + " so you won.")
choice = input("Would you like to play again?(y/n) ")
if choice == "y":
game()
else:
print("Thank you for playing.")
#Calling the method and staring the game
game()
All you have to do to call a function is type its name and include arguments if required.
In your case, what you have to do is
def game:
your = input("Rock, Paper or Scissor? ")
your.lower()
list = ["rock", "paper", "scissor"]
import random
a = random.randint(0, 2)
weapon = list[a]
if your == weapon:
print("You chose " + your + " and I chose " + weapon + " so it's a draw.")
elif your == "rock" and weapon == "paper":
print("You chose " + your + " and I chose " + weapon + " so I won.")
elif your == "paper" and weapon == "scissor":
print("You chose " + your + " and I chose " + weapon + " so I won.")
elif your == "scissor" and weapon == "rock":
print("You chose " + your + " and I chose " + weapon + " so I won.")
else:
print("You chose " + your + " and I chose " + weapon + " so you won.")
choice = input("Would you like to play again?(y/n) ")
if choice == "y":
game()
else:
print("Thank you for playing.")
# this is how you call the function
game()

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

Why does my code not end even when directly prompted to?

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

python problems can't seem to check some values

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)

Categories

Resources