I am trying to create a program that lets the user choose to play rock, paper or scissors. Once they choose what they want to play and the amount of games that they want to play, they will be told if they won or lost.
I want to tally up the amount of times they won or lost, but instead it just prints after each round. Is there a way that I can make it print after the game is completed? I want to show the user the outcome overall at the end
from random import randint
def main():
games = int(input("How many games would you like to play?"))
while games > 0:
games -= 1
comp = computer()
choice = user()
print("You played", choice, "and the computer played", comp)
winner(comp, choice)
def computer():
comp = randint in range (0,3)
if comp == 0:
comp = 'rock'
elif comp == 1:
comp = 'paper'
elif comp == 2:
comp = 'scissors'
return comp
def user():
choice = int(input("choose 0 for rock, 1 for paper, or 2 for scissors: "))
if choice == 0:
choice = 'rock'
elif choice == 1:
choice = 'paper'
elif choice == 2:
choice = 'scissors'
else:
print("invalid input")
return choice
def winner(comp, choice):
tie = 0
win = 0
lose = 0
while True:
if choice == "rock" and comp == "rock":
result = 'tie'
tie += 1
break
elif choice == 'rock'and comp == 'scissors':
result = "you win"
win += 1
break
elif choice == 'rock' and comp == 'paper':
result = "you lose"
lose += 1
break
elif choice == 'paper' and comp == 'paper':
result = 'tie'
tie += 1
break
elif choice == 'paper' and comp == 'scissors':
result = 'you lose'
lose += 1
break
elif choice == 'paper' and comp == 'rock':
result = 'you win'
win =+ 1
break
elif choice == 'scissors' and comp == 'scissors':
result = 'tie'
tie += 1
break
elif choice == 'scissors' and comp == 'paper':
result = 'you win'
win += 1
break
elif choice == 'scissors' and comp == 'rock':
result = 'you lose'
lose +=1
break
else:
print("error")
break
print(result)
print("you won", win,"times, and lost", lose,"times, and tied", tie,"times.")
main()
Declare globals for the win, lose and tie counters
randint(0, 2) is how you get random integer
It's += and not =+
There are couple of mistakes. I added a comment # CORRECTION at the respective lines.
from random import randint
tie = 0
win = 0
lose = 0
def main():
games = int(input("How many games would you like to play?"))
while games > 0:
games -= 1
comp = computer()
choice = user()
print("You played", choice, "and the computer played", comp)
winner(comp, choice)
def computer():
# CORRECTION: randint in range (0,3) always return FALSE. BELOW IS THE CORRECT WAY
comp = randint(0, 2)
if comp == 0:
comp = 'rock'
elif comp == 1:
comp = 'paper'
elif comp == 2:
comp = 'scissors'
return comp
def user():
choice = int(input("choose 0 for rock, 1 for paper, or 2 for scissors: "))
if choice == 0:
choice = 'rock'
elif choice == 1:
choice = 'paper'
elif choice == 2:
choice = 'scissors'
else:
print("invalid input")
return choice
def winner(comp, choice):
# CORRECTION: MAKE ALL THE COUNTERS GLOBAL
global tie
global win
global lose
while True:
if choice == "rock" and comp == "rock":
result = 'tie'
tie += 1
break
elif choice == 'rock'and comp == 'scissors':
result = "you win"
win += 1
break
elif choice == 'rock' and comp == 'paper':
result = "you lose"
lose += 1
break
elif choice == 'paper' and comp == 'paper':
result = 'tie'
tie += 1
break
elif choice == 'paper' and comp == 'scissors':
result = 'you lose'
lose += 1
break
elif choice == 'paper' and comp == 'rock':
result = 'you win'
# CORRECTION: ITS NOT =+ ITS +=
# win =+ 1
win += 1
break
elif choice == 'scissors' and comp == 'scissors':
result = 'tie'
tie += 1
break
elif choice == 'scissors' and comp == 'paper':
result = 'you win'
win += 1
break
elif choice == 'scissors' and comp == 'rock':
result = 'you lose'
lose +=1
break
else:
print("error")
break
print(result)
main()
print("you won", win,"times, and lost", lose,"times, and tied", tie,"times.")
Related
when input is asked, I type Rock it loops back as though i did not type the right input.
I've tried retyping the error checking but nothing is working. The only input it will accept is 'q'. Here is the code:
import random
user_wins = 0 #setting default score
computer_wins = 0
options = ["rock", "paper", "scissors"]
while True:
user_input = input('Type Rock/Paper/Scissors or Q to quit: ').lower()
if user_input == "q":
break
if user_input not in options:
continue
random_number = random.randint(0, 2) #rock: 0 , paper: 1, scissors: 2
computer_pick = options[random_number] #using computer's pick from a random from the list to be stored.
print('Computer picked:', computer_pick + ".") #printing the result of computer's pick
if user_input == "rock" and computer_pick == "scissors": #setting win or lose conditions
print('Rock Crushes Scissors...You Win!')
user_wins += 1
elif user_input == "paper" and computer_pick == "rock":
print('Paper Covers Rock...You Win!')
user_wins += 1
elif user_input == "scissors" and computer_pick == "paper":
print('Scissors Cuts Paper...You Win!')
user_wins += 1
else:
print("You Lose")
computer_wins += 1
print("You won ", user_wins, "times.")
print("Computer won ", computer_wins, "times.")
print('Goodbye')
It's because you need to break the loop when input is in options, so the loop could be
while True:
user_input = input('Type Rock/Paper/Scissors or Q to quit: ').lower()
if user_input == "q":
break
if user_input not in options:
continue
else:
break
Anyways, a better implementation could be creating the empty option and assign it if matches, so
answer = ''
while not answer:
user_input = input('Type Rock/Paper/Scissors or Q to quit: ').lower()
if user_input == "q":
break
elif user_input in options:
answer = user_input
In you case try this code. You will need to have two while loops: one is for checking the input, the other is for continuing the game.
exit = False #this helps you to exit from the main while loop.
while not exit:
options = ['rock', 'scissors', 'paper']
user_input = ""
while True:
user_input = input('pls write paper, rock, scissors, or
q(quit) ').lower()
if user_input in options:
break
elif user_input == 'q':
exit = True
break
#do some calculations
# do some summary calculations and print them
I hope you will find it helpful)
I am trying to make it so that when the user or the bot wins the round they get a point but at the end of the game, it seems to not add when one or the other gets it right, i tried the other way ('x = x + 1'), also please rate my code and tell me what i could've done better.
import random
print('Lets play Rock Paper Scissors')
for tries in range (1,4):
try:
user_guess = input('Rock Paper Scissors? ')
choices = ['Rock','Paper','Scissors']
user_point = 0 #To keep track of the points
bot_point = 0
bot_guess = random.choice(choices) #Will randomly pick from the list 'choices'
while user_guess not in choices:#if the user tries to put in anything other then the choices given
print('Please enter the choices above!')
user_guess = input('Rock Paper Scissors? ')
except ValueError:
print('Please choose from the choices above ') #Just in case user tries to put a different value type
user_guess = input('Rock Paper Scissors? ')
DEBUG = "The bot did " + bot_guess
print(DEBUG)
if user_guess == bot_guess:
print('Tie!')
elif user_guess == "Rock" and bot_guess == "Paper":
print('The bot earns a point!')
bot_point += 1
elif user_guess == 'Paper' and bot_guess == "Rock":
print('The user earns a point!')
user_point += 1
elif user_guess == 'Paper' and bot_guess == 'Scissors':
print('The bot earns a point')
bot_point += 1
elif user_guess == 'Scissors' and bot_guess == 'Paper':
print('The user earns a point')
user_point += 1
elif user_guess == 'Rock' and bot_guess == 'Scissors':
print('The user earns a point')
user_point += 1
elif user_guess == 'Scissors' and bot_guess == 'Rock':
print('The bot earns a point')
bot_point += 1
print('After ' + str(tries) + ' tries. ' + ' The score is')
print('The User: ' + str(user_point))
print('The Bot: ' + str(bot_point))
if user_point > bot_point:
print('THE USER IS THE WINNER!!!')
else:
print('THE BOT IS THE WINNER!!!')
You need to initialize user_point and bot_point before you start the for loop. The way it is, those are reset to zero each time through the loop.
Its showing"line 42, in
if input_ !='no':
NameError: name 'input_' is not defined" error when i input 'no'
The code is :
def rock_paper_scissors():
comp_score = your_score = 0
y,z="This is a rockpaper scissors game. ","Do you wish to play"
x= ["rock","paper","scissors"]
input_=input(y+z+"?")
if input_ == "no":
return
rock_paper_scissors()
if input_ !='no':
a=input("Do you wanna play again?")
How can i rectify it? (this is just a small part of the entire program but i think this should do...)
Variable input_ is initialised inside function rock_paper_scissors() which means outside of it (function scope) is undefined. So we need to put it inside function or make input_ global.
def rock_paper_scissors():
comp_score = your_score = 0
y, z = "This is a rock paper scissors game. ", "Do you wish to play"
x= ["rock", "paper", "scissors"]
input_=input(y + z + "?")
if input_ == "no":
return
else: # no need for: if input_ != "no"
a = input("Do you wanna play again?")
rock_paper_scissors()
Hope this helps.
Here is my take on it, still needs some work, it functional...
# Tested via Python 3.7.4 - 64bit, Windows 10
# Author: Dean Van Greunen
# License: I dont care, do what you want
####################################
# Some Defines
####################################
# imports
import random
# global vars
comp_score = 0
your_score = 0
input_ = ''
ai_input = ''
# strings
string_1 = 'This is a rockpaper scissors game.'
question_1 = 'Do you wish to play? '
question_2 = 'Enter your move: '
question_3 = 'Do you wanna play again? '
string_4 = 'Valid Moves: '
string_5 = 'Thank you for playing!'
string_6 = 'Scores: Player {0} V.S. AI {1}'
yes = 'yes'
no = 'no'
# vaild moves
moves = ["rock","paper","scissors"]
####################################
# Code Setup
####################################
def displayWinner(player_move_str, ai_move_str):
# Vars
winner = ''
tie = False
# Winner Logic
if player_move_str == ai_move_str:
tie = True
elif player_move_str == 'paper' and ai_move_str == 'rock':
winner = 'Player'
elif player_move_str == 'scissor' and ai_move_str == 'rock':
winner = 'AI'
elif player_move_str == 'rock' and ai_move_str == 'paper':
winner = 'AI'
elif player_move_str == 'scissor' and ai_move_str == 'paper':
winner = 'Player'
elif player_move_str == 'rock' and ai_move_str == 'scissor':
winner = 'Player'
elif player_move_str == 'paper' and ai_move_str == 'scissor':
winner = 'AI'
# display Logic
if tie:
print('It Was A Tie!')
else:
global your_score
global comp_score
if winner == 'AI':
comp_score = comp_score + 1
elif winner == 'Player':
your_score = your_score + 1
print(winner + ' Won!')
def start():
global your_score
global comp_score
print(string_1)
print(string_6.format(your_score, comp_score))
input_ = input(question_1)
if input_ == yes:
print(string_4)
[print(x) for x in moves]# make sure input is valid.
input_ = input(question_2)
ai_input = random.choice(moves) # let AI pick move
print('AI Picked: ' + ai_input)
displayWinner(input_, ai_input)
input_ = input(question_3) # Play Again?
if input_ == yes:
start() # Restart Game (Recursive Call)
else:
print(string_5) # Thank you Message
####################################
# Game/Script Entry Point/Function
####################################
start()
Thanks yall. Fixed my program. Now it goes like:
import time
import random
input_ =""
def rock_paper_scissors():
comp_score = your_score = 0
y,z="This is a rockpaper scissors game. ","Do you wish to play"
x= ["rock","paper","scissors"]
global input_
input_=input(y+z+"?")
if input_ == "no":
return
max_score=("Enter max score : ")
while True:
input_=input("Enter your choice among rock, paper and scissors ('stop' to
quit):")
if input_ not in x and input_!='stop'and input_ != 'enough':
print("Invalid answer. Are you blind? Pick one from the 3.")
continue
n = random.randint(0,2)
k=x[n]
if n<2:
if input_==x[n+1]:
your_score+=1
elif input_==k:
pass
else:
comp_score+=1
else:
if input_=="paper":
comp_score+=1
elif input_=="rock":
your_score+=1
elif input_=="scissors":
pass
else:
pass
if input_!="stop" and input_ != "enough":
print(3)
time.sleep(1.5)
print(2)
time.sleep(1.5)
print(1)
time.sleep(1.5)
print("Your choice : "+input_ +"\nComputers move : "+k)
elif input_=="stop" and input_=="enough":
input_=input("Play again?")
if (your_score == max_score or comp_score==max_score) or (input_=="stop"
or input_=="enough"):
print("Your score is : ",your_score)
print("AI's score is : ",comp_score)
break
rock_paper_scissors()
if input_ !='no':
a=input("Do you wanna play again?")
if a =="yes":
rock_paper_scissors()
print("k.bye! \n :(")
import random
dict = {0:'rock',1:'Spock',2:'paper',3:'lizard',4:'scissors'}
def name_to_number(name):
if name == "rock":
return 0
elif name == "spock":
return 1
elif name == "paper":
return 2
elif name == "lizard":
return 3
elif name == "csissors":
return 4
else:
print("error:wrong name!")
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: wrong number!")
return
def rpsls(palyer_choice):
print(" ")
print("players chooses %s",'player_choice')
player_number = name_to_number(player_choice)
comp_number = random.randrange(0, 5)
print ("Computer chooses %s",' comp_number')
difference = player_number - comp_number
if difference == 0:
print("Player and computer tie!")
elif difference == 3 or difference ==3 or difference == -1 or difference == -2:
print ("computer wins!")
else:
print("Players wins!")
you have to call rpsls function
so add this code in the last of your code
rpsls()
First of im sorry about bombarding my post with a bunch of code, but im putting it here hoping that it will help you guys understanding my question better.
I am trying to figure out why my IDE is unwilling to execute the first function of my code. Obviously im doing something wrong, but im to unexperienced to see it for myself. All help would be greatly appriciated.
If you have any further comments or tips on my code feel free contribute.
import random
print("Press '1' for New Single Player Game - Play against computer component: ")
print("Press '2' for New Two Player Game - Play against another person, using same computer: ")
print("Press '3' for Bonus Feature - A bonus feature of choice: ")
print("Press '4' for User Guide - Display full instructions for using your application: ")
print("Press '5' for Quit - Exit the program: ")
def choose_program():
what_program = int(input("What program would you like to initiate?? Type in the 'number-value' from the chart above: "))
if what_program == 1 or 2 or 3 or 4 or 5:
program_choice = int(input("Make a choice: "))
if (program_choice > 5) or (program_choice < 1):
print("Please choose a number between 1 through 5 ")
choose_program()
elif program_choice == 1:
print("You picked a New Single Player Game - Play against computer component")
def single_player():
start_game = input("Would you like to play 'ROCK PAPER SCISSORS LIZARD SPOCK'?? Type 'y' for YES, and 'n' for NO): ").lower()
if start_game == "y":
print("Press '1' for Rock: ")
print("Press '2' for Paper: ")
print("Press '3' for Scissors: ")
print("Press '4' for Lizard: ")
print("Press '5' for Spock: ")
elif start_game != "n":
print("Please type either 'y' for YES or 'n' for NO: ")
single_player()
def player_one():
human_one = int(input("Make a choice: "))
if (human_one > 5) or (human_one < 1):
print("Please choose a number between 1 through 5 ")
player_one()
elif human_one == 1:
print("You picked ROCK!")
elif human_one == 2:
print("You picked PAPER!")
elif human_one == 3:
print("You picked SCISSORS!")
elif human_one == 4:
print("You picked LIZARD!")
elif human_one == 5:
print("You picked SPOCK!")
return human_one
def computers_brain():
cpu_one = random.randint(1, 5)
if cpu_one == 1:
print("Computers choice iiiiiis ROCK!")
elif cpu_one == 2:
print("Computers choice iiiiiis PAPER!")
elif cpu_one == 3:
print("Computers choice iiiiiis SCISSORS!")
elif cpu_one == 4:
print("Computers choice iiiiiis LIZARD!")
elif cpu_one == 5:
print("Computers choice iiiiiis SPOCK!")
return cpu_one
def who_won(human, cpu, human_wins, cpu_wins, draw):
if human == 1 and cpu == 3 or cpu == 4:
print("Human is invincible!!")
human_wins = human_wins.append(1)
elif human == 2 and cpu == 1 or cpu == 5:
print("Human is invincible!!")
human_wins = human_wins.append(1)
elif human == 3 and cpu == 2 or cpu == 4:
print("Human is invincible!!")
human_wins = human_wins.append(1)
elif human == 4 and cpu == 2 or cpu == 5:
print("Human is invincible!!")
human_wins = human_wins.append(1)
elif human == 5 and cpu == 1 or cpu == 3:
print("Human is invincible!!")
human_wins = human_wins.append(1)
elif human == cpu:
print("Human & computer think alike, such technology")
draw = draw.append(1)
else:
print("Computer have beaten it's master, incredible..")
cpu_wins = cpu_wins.append(1)
return
def end_score(human_wins, cpu_wins, draw):
human_wins = sum(human_wins)
cpu_wins = sum(cpu_wins)
draw = sum(draw)
print("Humans final score is: ", human_wins)
print("Computers final score is: ", cpu_wins)
print("Total draws: ", draw)
def main():
human = 0
human_wins = []
cpu = 0
cpu_wins = []
draw = []
finalhuman_wins = 0
finalcpu_wins = 0
finaldraw = 0
Continue = 'y'
single_player()
while Continue == 'y':
human = player_one()
cpu = computers_brain()
who_won(human, cpu, human_wins, cpu_wins, draw)
Continue = input("Would you like to play again (y/n):").lower()
if Continue == 'n':
print("Printing final scores.")
break
end_score(human_wins, cpu_wins, draw)
main()
elif program_choice == 2:
print("You picked a New Two Player Game - Play against another person, using same computer")
elif program_choice == 3:
print("You picked the Bonus Feature - A bonus feature of choice")
elif program_choice == 4:
print("You picked the User Guide - Display full instructions for using your application")
elif program_choice == 5:
print("You picked to Quit - Exit the program")
return program_choice
elif what_program != 1 or 2 or 3 or 4 or 5:
print("To initiate a program you need to type in the appropriate number to initiate this program, either 1, 2, 3, 4 & 5 ")
choose_program()
Outout in IDE:
Press '1' for New Single Player Game - Play against computer component:
Press '2' for New Two Player Game - Play against another person, using same computer:
Press '3' for Bonus Feature - A bonus feature of choice:
Press '4' for User Guide - Display full instructions for using your application:
Press '5' for Quit - Exit the program:
Process finished with exit code 0
Thank you in advance for your help :)