Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'd like some assistance on my rock paper scissors game code. I'd like to add a function where the player can choose a specific number of rounds, instead of having a preset number of rounds. I am quite stuck on how to do this and I've just started to pick up Python again, so i am a bit rusty. Thank you so much ! (Please ignore spelling ;D)
CODE:
import random #imports a random moduel for the computer.
game = ["ROCK", "PAPER", "SCISSORS"] #sets the game answers.
count = 0 #game count is set to zero.
score = 0 #player score is set to zero.
computerscore =0 #computers score is set to zero.
print ("Welcome")
while count == 0: #starts game if count is zero.
for i in range (3): #repeats the turns 3 times.
answer = input ("Pick rock, paper or scissors.") #users answer.
print (answer.upper()) # prints the users answer
computer= random.choice(game) #computer picks randomly
print ("Computer picks",computer) #prints the computers choice.
if answer.upper() == "ROCK" and computer == "ROCK": #This whole set of code sets the game that what beats what.
print ("Its a tie!") # prints after each response that who won.
count +=1 #the count variable is increased.
elif answer.upper() == "PAPER" and computer == "PAPER":
print ("Its a tie!")
count +=1
elif answer.upper() == "SCISSORS" and computer == "SCISSORS":
print ("Its a tie!")
count +=1
elif answer.upper() == "PAPER" and computer == "ROCK":
print ("You win!")
count +=1
score +=1 #user score is added.
elif answer.upper() == "PAPER" and computer == "SCISSORS":
print ("You lose!")
count +=1
computerscore +=1 #computers score is added.
elif answer.upper() == "ROCK" and computer == "PAPER":
print ("You lose!")
count +=1
computerscore +=1
elif answer.upper() == "ROCK" and computer == "SCISSORS":
print ("You win!")
count +=1
score +=1
elif answer.upper() == "SCISSORS" and computer == "ROCK":
print ("lose!")
count +=1
computerscore +=1
elif answer.upper() == "SCISSORS" and computer == "PAPER":
print ("You win!")
count +=1
score +=1
if score < computerscore: #prints out at the end who scored how much and who won.
print ("Your score is", score)
print ("Computers socre is",computerscore)
print ("Computer wins!.")
if score > computerscore:
print ("Your score is", score)
print ("Computers socre is",computerscore)
print ("You win!.")
if score == computerscore:
print ("Your score is", score)
print ("Computers socre is",computerscore)
print ("Its a tie!!.")
Change the while count == 0:
to while count < x:
where x is the number of games you want to play
also i suggest to split your code into more functions, it will be much cleaner
Create a rounds variable to store the user input:
rounds = 3
Add this just before the while-loop:
user_input = input('How many rounds do you want to play?')
try:
rounds = int(user_input)
except ValueError:
print('Not a number')
And change your loop's condition to for i in range(rounds):
Related
This is a rock,paper,scissors game.
import random
options = ("rock", "paper", "scissors") # three options
running = True
score = 0
while running: # continuing the game
player = "" # storing the player's choices
computer = random.choice(options) # computer will choose a random choice from the options
while player not in options: # the player has to choose one of the options, if not, it will still keep on looping
player = input("Enter a choice (rock, paper, scissors): ")
print("Player: " + player) # player(user) vs. computer
print("Computer: " + computer)
if player == computer:
score = 0
print("It's a tie!")
elif player == "rock" and computer == "scissors":
score += 1
print("You win!")
elif player == "paper" and computer == "rock":
score += 1
print("You win!")
elif player == "scissors" and computer == "paper":
score += 1
print("You win!")
else:
score += 1
print("You lose!")
print(score, "for computer")
print(score)
play_again = input("Play again? (y/n): ").lower()
if play_again == "y":
running = True
print("Yes sir!")
else:
running = False
print("Thanks for playing! ")
My goal was trying to keep score for player and computer. If the user wins, they get a point. If the computer wins, they get a point. If its a tie, neither of them gets a point.
The following code shows you three rounds of three games of a simulated user input game with its result. It is your code only slightly changed to count properly the results:
import random
lst_user_input = [
"list faking user input:"
,"scissors"
,"paper"
,"rock"
,"y"
,"scissors"
,"paper"
,"rock"
,"y"
,"scissors"
,"paper"
,"rock"
,"n"
]
options = ("rock", "paper", "scissors") # three options
running = True
computer_score = 0
player_score = 0
round_no = 0
while running: # continuing the game
#player = "" # storing the player's choices
round_no += 1
computer = random.choice(options) # computer will choose a random choice from the options
#while player not in options: # the player has to choose one of the options, if not, it will still keep on looping
# player = input("Enter a choice (rock, paper, scissors): ")
print(round_no)
player = lst_user_input[round_no]
print("Player : " + player) # player(user) vs. computer
print("Computer: " + computer)
if player == computer:
print("It's a tie!")
elif player == "rock" and computer == "scissors":
player_score += 1
print("You win!")
elif player == "paper" and computer == "rock":
player_score += 1
print("You win!")
elif player == "scissors" and computer == "paper":
player_score += 1
print("You win!")
else:
computer_score += 1
print("You lose!")
print("Player score:", player_score)
print("Computer score:", computer_score)
if not round_no % 3: # ask only each 3 rounds
#play_again = input("Play next 3-rounds? (y/n): ").lower()
round_no +=1
play_again = lst_user_input[round_no]
if play_again == "y":
running = True
print("Yes sir!")
else:
running = False
print("Thanks for playing! ")
Here an example of printed output:
1
Player : scissors
Computer: paper
You win!
Player score: 1
Computer score: 0
2
Player : paper
Computer: scissors
You lose!
Player score: 1
Computer score: 1
3
Player : rock
Computer: paper
You lose!
Player score: 1
Computer score: 2
Yes sir!
5
Player : scissors
Computer: paper
You win!
Player score: 2
Computer score: 2
6
Player : paper
Computer: rock
You win!
Player score: 3
Computer score: 2
Thanks for playing!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
The Break Command Doesnt work and when i try to run it, it gets stuck like it cant compile because its stuck in a loop
My code isn't running at all, I have reviewed my code but it doesn't seem to work. Everything is fine for me.
import random
# print("Winning Rules of the Rock paper scissor game as follows: \n"
# + "Rock vs paper->paper wins \n"
# + "Rock vs scissor->Rock wins \n"
# + "paper vs scissor->scissor wins \n")
while True:
def main():
print("Enter choice \n 1. Rock \n 2. paper \n 3. scissor \n")
try :
choice_name = (input("User turn: "))
if choice_name == 1:
choice_name = 'Rock'
if choice_name == 2:
choice_name = 'paper'
if choice_name == 3:
choice_name = 'scissor'
else:
choice_name = None
input("Atleast Enter a Valid Number like BRUHHHHHHHHH: ")
except ValueError:
print ("Try again")
main()
print("user choice is: " + choice_name)
print("\nNow its computer turn.......")
Computer_Choice = random.randint(1, 3)
while Computer_Choice == choice_name:
Computer_Choice = random.randint(1, 3)
if Computer_Choice == 1:
Computer_Choice_name = 'Rock'
elif Computer_Choice == 2:
Computer_Choice_name = 'paper'
else:
Computer_Choice_name = 'scissor'
print("Computer choice is: " + Computer_Choice_name)
print(choice_name + " V/s " + Computer_Choice_name)
if((choice_name == 1 and Computer_Choice == 2) or
(choice_name == 2 and Computer_Choice == 1)):
print("paper wins => ", end="")
result = "paper"
elif((choice_name == 1 and Computer_Choice == 3) or
(choice_name == 3 and Computer_Choice == 1)):
print("Rock wins =>", end="")
result = "Rock"
else:
print("scissor wins =>", end="")
result = "scissor"
if result == choice_name:
print("<== User wins ==>")
else:
print("<== Computer wins ==>")
print("Do you want to play again? (Y/N)")
ans = input()
if ans == 'n' or ans == 'N':
break
print("\nThanks for playing")
You are defining a function inside of a while loop. Currently the loop always evaluates to False.
What you want is:
def main():
while True:
# ...
Then you need to actually call main. At the end of the file:
if __name__ == '__main__':
main()
I just started to learn Python and tried to make this game by myself for self-training.
I have made the game taking 2 more hours.
But I'd like to make score result(either 3 win or 3 lose) with break.
I don't know how to use while statement with break on this situation.
Hope to help me please.
import random
user_choice = input("select one of rock, paper, scissors. ")
Rock_paper_scissors = ['rock', 'paper', 'scissors']
computer_choice = Rock_paper_scissors[random.randint(0,2)]
if user_choice == computer_choice:
print("Draw.")
elif user_choice == "rock":
if computer_choice == "paper":
computer_score += 1
print("lose.")
else:
user_score += 1
print("win.")
elif user_choice == "scissors":
if computer_choice == "rock":
computer_score += 1
print("lose.")
else:
user_score += 1
print("win.")
elif user_choice == "paper":
if computer_choice == "scissors":
computer_score += 1
print("lose")
else:
user_score += 1
print("win")
Well first youll want to add those score variables at the begging of your code.
computer_score=0
user_score=0
then you want to have a while statement that also encloses the user input
Rock_paper_scissors = ['rock', 'paper', 'scissors']
while True:
user_choice = input("select one of "rock, paper, scissors. ")
computer_choice = Rock_paper_scissors[random.randint(0,2)]
#Your if/elif statements go here
And at the end and an if statement to check if someone has a score of 3 or more
if user_score >= 3:
print('You win')
break
You can loop your procedure for 3 times, stop when one of 2 opponent reach score of 2.
It will look something like this
while (user_score < 3 and computer_score < 3):
<continue playing>
If you want to use break:
while True:
<continue playing>
if user_score == 3 or computer score == 3:
break
Hope this help
The while loop in python works like this:
while condition:
do something...
While the condition is true the loop will keep going, in this case you don't need a break statement, you could simply do:
user_score = 0
computer_score = 0
while (user_score < 3 and computer_score < 3):
game...
If you really want to use a break statement, you could do it like this:
user_score = 0
computer_score = 0
while True:
if (user_score >= 3 or computer_score >= 3):
break
game...
That way the loop will keep going forever, since the condition is True, but the if inside the loop will call a break when a player scores 3 points.
user_score and computer_score are initialized to zero, you always have to initialize your variables.
as my homework (using codeskulptor.org) I put together a simple Rock-Paper-Scissors-Lizard-Spock 'game' in Python, where hard coded player's guesses were running the programme. Translation from name to number and other way round, random computer choice and printing... everything worked fine.
Then I tried to introduce input so that player gets to type their guess. However, the console prints only the log about wrong input but doesn't launch the rest of the programme if the input is actually correct... Tried various modifications, but I'm stuck... am I missing something obvious? Thanks!
import simplegui
import random
def get_guess(guess):
if guess == "rock":
return 0
elif guess == "Spock":
return 1
elif guess == "paper":
return 2
elif guess == "lizard":
return 3
elif guess == "scissors":
return 4
else:
print "Error guess_to_number:", guess, "is not a rpsls-element"
return
def number_to_name(number):
if number == 0:
return "rock"
elif number == 1:
return "Spock"
elif number == 2:
return "paper"
elif number == 3:
return "lizard"
elif number == 4:
return "scissors"
else:
print "Error number_to_name:", number, "is not in [0, 4]"
return
def rpsls(guess):
print
print "Player chooses", guess
player_number = get_guess(guess)
computer_number = random.randrange(5)
computer_choice = number_to_name(computer_number)
print "Computer chooses", computer_choice
diff_mod = (player_number - computer_number) % 5
if diff_mod == 0:
print "Player and computer tie!"
elif diff_mod == 1 or diff_mod == 2:
print "Player wins!"
else:
print "Computer wins!"
frame = simplegui.create_frame("GUI-based RPSLS", 200, 200)
frame.add_input("Enter guess for RPSLS", get_guess, 200)
frame.start()
import random
def main():
playagain = 1
win=0
lose=0
tie=0
while playagain==1:
printCpu=cpuConverter()
printPlayer=playerConverter()
print("The computers choice is", printCpu)
print("Your choice is", printPlayer)
gameWinner(printCpu, printPlayer)
winner(gameWinner, printCpu, printPlayer)
playagain=int(input("Would you like to play again? Enter 1 for yes, any other number for no!"))
print("Your total wins are", win)
print("Your total losses are", lose)
print("Your total ties are", tie)
def cpuConverter():
cpuChoice=random.randrange(1,4)
if cpuChoice==1:
printCpu="Rock"
elif cpuChoice==2:
printCpu="Paper"
else:
printCpu="Scissors"
return printCpu
def playerConverter():
again=0
while again<1 or again>3:
printPlayer=int(input("Please choose a number to play against the computer. 1=Rock 2=Paper 3=Scissors "))
if printPlayer<1 or printPlayer>3:
print("Invalid choice for the game. Please choose another number inside the constraints.")
elif printPlayer==1:
printPlayer="Rock"
again=1
elif printPlayer==2:
printPlayer="Paper"
again=1
else:
printPlayer="Scissors"
again=1
return printPlayer
def gameWinner(printCpu, printPlayer):
if printCpu == printPlayer:
winner = "tie"
print("It's a tie")
elif printCpu == "Scissors" and printPlayer == "Rock":
winner = "win"
print("Rock beats Scissors! You win")
elif printCpu == "Paper" and printPlayer == "Scissors":
winner = "win"
print("Scissors cuts paper! You win")
elif printCpu == "Rock" and printPlayer == "Paper":
winner = "win"
print("Paper covers Rock! You win")
else:
winner = "lose"
print("You lose")
return winner
def winner(gameWinner, printCpu, printPlayer):
if winner == "win":
win += 1
elif winner == "lose":
lose += 1
elif winner == "tie":
tie += 1
return winner
main()
So when I try this code, everything works for the most part. The only thing I can't seem to get working is the actual counting part. When I try to print my results after playing multiple (or even one) times, the code still ends up with zero as the total amount of games. Can someone please show me where I'm messing up and hopefully help me fix this?
You have several errors in your code. The first is
winner(gameWinner, printCpu, printPlayer)
passes a function to the function winner. You should capture the return value of gameWinner and pass it to winner
result = gameWinner(printCpu, printPlayer)
winner(result, printCpu, printPlayer)
Next issue
def winner(gameWinner, printCpu, printPlayer):
if winner == "win":
You are ignoring your input parameters and comparing the function itself to the string "win". It will always be False. The upshot of this is that your win, lose, tie variables are never touched.
The final issue is that win, lose and tie are global. All experienced programmers frown upon the use of globals, but if you must use them you should firstly declare the variables in the global scope, not inside the function main. You should then should use the global keyword inside any function that references them. So inside winner we need
global win, lose, tie
The minimally corrected code looks like
import random
win=0
lose=0
tie=0
def main():
playagain = 1
while playagain==1:
printCpu=cpuConverter()
printPlayer=playerConverter()
print("The computers choice is", printCpu)
print("Your choice is", printPlayer)
result = gameWinner(printCpu, printPlayer)
winner(result, printCpu, printPlayer)
playagain=int(input("Would you like to play again? Enter 1 for yes, any other number for no!"))
print("Your total wins are", win)
print("Your total losses are", lose)
print("Your total ties are", tie)
def cpuConverter():
cpuChoice=random.randrange(1,4)
if cpuChoice==1:
printCpu="Rock"
elif cpuChoice==2:
printCpu="Paper"
else:
printCpu="Scissors"
return printCpu
def playerConverter():
again=0
while again<1 or again>3:
printPlayer=int(input("Please choose a number to play against the computer. 1=Rock 2=Paper 3=Scissors "))
if printPlayer<1 or printPlayer>3:
print("Invalid choice for the game. Please choose another number inside the constraints.")
elif printPlayer==1:
printPlayer="Rock"
again=1
elif printPlayer==2:
printPlayer="Paper"
again=1
else:
printPlayer="Scissors"
again=1
return printPlayer
def gameWinner(printCpu, printPlayer):
if printCpu == printPlayer:
winner = "tie"
print("It's a tie")
elif printCpu == "Scissors" and printPlayer == "Rock":
winner = "win"
print("Rock beats Scissors! You win")
elif printCpu == "Paper" and printPlayer == "Scissors":
winner = "win"
print("Scissors cuts paper! You win")
elif printCpu == "Rock" and printPlayer == "Paper":
winner = "win"
print("Paper covers Rock! You win")
else:
winner = "lose"
print("You lose")
return winner
def winner(gameWinner, printCpu, printPlayer):
global win, lose, tie
if gameWinner == "win":
win += 1
elif gameWinner == "lose":
lose += 1
elif gameWinner == "tie":
tie += 1
return winner
main()