I'm curious as to why this program is not looping. The program will run once but when another game is played it doesn't record the winner and quits.
Any assistance will be greatly appreciated!
Here is the Python program:
import random
random.seed()
print ("For each round please select from the following warriors: ")
print ("'C' or 'c' for Cowboy")
print ("'N' or 'n' for Ninja")
print ("'B' or 'b' for Bear")
print ("'Q' or 'q' for quit")
print()
gamesPlayed = 0
print ("Round", gamesPlayed +1, ":")
warriorStr = input("Please choose a warrior: ")
warriorStr = warriorStr.lower()
while not warriorStr.isalpha():
print()
print ("That's not a valid choice!")
warriorStr = input("Please enter a weapon: ")
computer = random.choice("cnb")
count = 0
winCount = 0
lossCount = 0
tieCount = 0
if warriorStr == "n" or "N" and computer == "c" or "C":
winCount = winCount + 1
print ("You win")
elif warriorStr == "c" or "C" and computer == "b" or "B":
winCount = winCount + 1
print ("You win")
elif warriorStr == "b" or "B" and computer == "n" or "N":
winCount = winCount + 1
print ("You win")
elif warriorStr == "c" and computer == "n":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "b" and computer == "c":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "n" and computer == "b":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "c" and computer == "c":
tieCount = tieCount + 1
print ("You tied")
elif warriorStr == "n" and computer == "n":
tieCount = tieCount + 1
print ("You tied")
elif warriorStr == "b" and computer == "b":
tieCount = tieCount + 1
print ("You tied")
gamesPlayed = gamesPlayed + 1
print()
print ("Round", gamesPlayed +1, ":")
warriorStr = input("Choose a warrior: ")
if warriorStr == "q" and "Q":
print("Game Over!")
print ("You have played a total of", gamesPlayed, "games.")
print ("You won", winCount, "times")
print ("The computer won", lossCount, "times")
print ("You tied", tieCount, "times")
Wrap the whole thing in a while loop.
Try something like this:
warriorStr = input("Please choose a warrior: ")
warriorStr = warriorStr.lower()
acceptables = ['a','c','n','q']
while warriorStr not 'q' and warriorStr in acceptables:
warriorStr = input("Please enter a weapon: ")
computer = random.choice("cnb")
count = 0
winCount = 0
lossCount = 0
tieCount = 0
if warriorStr == "n" or "N" and computer == "c" or "C":
winCount = winCount + 1
print ("You win")
elif warriorStr == "c" or "C" and computer == "b" or "B":
winCount = winCount + 1
print ("You win")
elif warriorStr == "b" or "B" and computer == "n" or "N":
winCount = winCount + 1
print ("You win")
elif warriorStr == "c" and computer == "n":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "b" and computer == "c":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "n" and computer == "b":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "c" and computer == "c":
tieCount = tieCount + 1
print ("You tied")
elif warriorStr == "n" and computer == "n":
tieCount = tieCount + 1
print ("You tied")
elif warriorStr == "b" and computer == "b":
tieCount = tieCount + 1
print ("You tied")
gamesPlayed = gamesPlayed + 1
print()
print ("Round", gamesPlayed +1, ":")
warriorStr = input("Choose a warrior: ")
print("Game Over!")
print ("You have played a total of", gamesPlayed, "games.")
print ("You won", winCount, "times")
print ("The computer won", lossCount, "times")
print ("You tied", tieCount, "times")
Related
I just started to learn python a few days ago. I made this for practice but the win counter does not increase after a win. I would also like to make the games run continually one after another. Any help would be appreciated.
print("Welcome to Rock | Paper | Scissors")
player1_wincount = 0
player2_wincount = 0
x = input("Would you like to play a game?\n")
if x == "Yes" or "yes" or "YES" or "Yeah" or "yeah" or "Yup" or "yup" or "Yessir" or "yessir":
x = 1
player1_name = input("Player 1, please enter your name:\n")
player1_age = input("Player 1, please enter your age:\n")
player2_name = input("Player 2, please enter your name:\n")
player2_age = input("Player 2, please enter your age:\n")
else: x = -1
while x > 0:
round = 1
print("Round", round)
player1_selection = input("Player 1, please select from the following options:\n1:Rock\n2:Paper\n3:Scissors\n4:EXIT")
player2_selection = input("Player 2, please select from the following options:\n1:Rock\n2:Paper\n3:Scissors\n4:EXIT")
round + 1
if player1_selection == "1" and player2_selection == 1:
print("Tie!")
elif player1_selection == "1" and player2_selection == 2:
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == "1" and player2_selection == 3:
print("Player 1 Wins")
player1_wincount += 1
elif player1_selection == "2" and player2_selection == 1:
print("Player 1 Wins!")
player1_wincount += 1
elif player1_selection == "2" and player2_selection == 2:
print("Tie!")
elif player1_selection == "2" and player2_selection == 3:
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == "3" and player2_selection == 1:
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == "3" and player2_selection == 2:
print("Player 1 Wins!")
player1_wincount += 1
elif player1_selection == "3" and player2_selection == 3:
print("Tie!")
else:
x = -1
break
print (player1_name,"has", player1_wincount, "wins!\n", player2_name, "has", player2_wincount, "wins!")
You made two small mistakes in your case, the indentation of your if-else statements, and placing quotes around player 2's selections.
print("Welcome to Rock | Paper | Scissors")
player1_wincount = 0
player2_wincount = 0
x = input("Would you like to play a game?\n")
if x == "Yes" or "yes" or "YES" or "Yeah" or "yeah" or "Yup" or "yup" or "Yessir" or "yessir":
x = 1
player1_name = input("Player 1, please enter your name:\n")
player1_age = input("Player 1, please enter your age:\n")
player2_name = input("Player 2, please enter your name:\n")
player2_age = input("Player 2, please enter your age:\n")
else: x = -1
while x > 0:
round = 1
print("Round", round)
player1_selection = input("Player 1, please select from the following options:\n1:Rock\n2:Paper\n3:Scissors\n4:EXIT")
player2_selection = input("Player 2, please select from the following options:\n1:Rock\n2:Paper\n3:Scissors\n4:EXIT")
round + 1
if player1_selection == "1" and player2_selection == "1":
print("Tie!")
elif player1_selection == "1" and player2_selection == "2":
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == "1" and player2_selection == "3":
print("Player 1 Wins")
player1_wincount += 1
elif player1_selection == "2" and player2_selection == "1":
print("Player 1 Wins!")
player1_wincount += 1
elif player1_selection == "2" and player2_selection == "2":
print("Tie!")
elif player1_selection == "2" and player2_selection == "3":
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == "3" and player2_selection == "1":
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == "3" and player2_selection == "2":
print("Player 1 Wins!")
player1_wincount += 1
elif player1_selection == "3" and player2_selection == "3":
print("Tie!")
else:
x = -1
break
print (player1_name,"has", player1_wincount, "wins!\n", player2_name, "has", player2_wincount, "wins!")
Additionally the code for this program can be more condensed using for loops.
print("Welcome to Rock | Paper | Scissors")
player1_wincount = 0
player2_wincount = 0
round = 1
x = input("Would you like to play a game?\n")
if x == "Yes" or "yes" or "YES" or "Yeah" or "yeah" or "Yup" or "yup" or "Yessir" or "yessir":
a = 1
player1_name = input("Player 1, please enter your name:\n")
# player1_age = input("Player 1, please enter your age:\n")
player2_name = input("Player 2, please enter your name:\n")
# player2_age = input("Player 2, please enter your age:\n")
else:
a = -1
while a > 0:
print("Round", round)
player1_selection = int(input("Player 1, please select from the following options:\n1:Rock\n2:Paper\n3:Scissors\n4:EXIT\n"))
player2_selection = int(input("Player 2, please select from the following options:\n1:Rock\n2:Paper\n3:Scissors\n4:EXIT\n"))
if player1_selection == 1 and player2_selection == 1:
print("Tie!")
elif player1_selection == 1 and player2_selection == 2:
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == 1 and player2_selection == 3:
print("Player 1 Wins")
player1_wincount += 1
elif player1_selection == 2 and player2_selection == 1:
print("Player 1 Wins!")
player1_wincount += 1
elif player1_selection == 2 and player2_selection == 2:
print("Tie!")
elif player1_selection == 2 and player2_selection == 3:
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == 3 and player2_selection == 1:
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == 3 and player2_selection == 2:
print("Player 1 Wins!")
player1_wincount += 1
elif player1_selection == 3 and player2_selection == 3:
print("Tie!")
else:
a = -1
break
round += 1
print (player1_name,"has", player1_wincount, "wins!\n", player2_name, "has", player2_wincount, "wins!")
There were a few mistakes in the code, well done for your project considering its your first few days of coding though!
First of all, all the if/elif/else conditions weren't indented into the while loop
The conditions of the if/elif statements weren´t of the same type. This means that the player 1 selection was always checking for the string "1" and player 2 selection was checking for a number/integer. I changed the input type to int, you can do it the other way around though if you wish
The round 1 had to be definded outside of the while loop as it got redefined as round 1 every time the loop restarted.
I want to code a rock paper scissors game with a scoring system that allows the user to wish to play again and the score adds on and not restarting to 0.
My code is here: https://pastebin.com/eRqEuwtY (It's also attached on this message)
Your help is appreciated.
import random
score1 = int(0)
score2 = int(0)
def main():
while True:
player = input('What do you choose to play (R, P, S)? ')
computer = random.choice(["R", "P", "S"])
print("You chose", (player), "and the computer chose", (computer))
if computer == player:
print("It's a tie")
print("Score: You =", score1, "Computer =", score2)
if computer == "R" and player == "S":
print("Computer wins")
print("Score: You =", score1, "Computer =", score2 + 1)
if computer == "P" and player == "R":
print("Computer wins")
print("Score: You =", score1, "Computer =", score2 + 1)
if computer == "S" and player == "P":
print("Computer wins")
print("Score: You =", score1, "Computer =", score2 + 1)
if computer == "S" and player == "R":
print("You won")
print("Score: You =", score1 + 1, "Computer =", score2)
if computer == "R" and player == "P":
print("You won")
print("Score: You =", score1 + 1, "Computer =", score2)
if computer == "P" and player == "S":
print("You won")
print("Score: You =", score1 + 1, "Computer =", score2)
play_again = input("Would you like to play again? Y/N ")
if play_again == "Y":
main()
else:
print("You scored", score1, "and the computer scored", score2)
exit()
main()
Your problem is that you print the score + 1 if you win. This does not save the new score to the variable!
Example:
print("You won")
score += 1
print("Score: You =", score1, "Computer =", score2)
Another problem in your code is that you call the main function again every time the user wants to play again. This is endless recursion and will result in an error if you hit the recursion limit. It's better to do it like this:
def main():
play_again = "Y"
while play_again == "Y":
#code...
play_again = input("Would you like to play again? Y/N ")
print("You scored", score1, "and the computer scored", score2)
main()
As Jasper pointed out, you need to save the new score to the respective variable. Here is a rework of your code, including some improvements:
import random
def main():
SYMBOLS = ["R", "P", "S"]
INPUTS_YES = ["Y", "YES", "I LOVE THIS GAME SO MUCH"]
INPUTS_NO = ["N", "NO", "PLZ STOP THIS"]
INPUTS_ALL = INPUTS_YES + INPUTS_NO
keep_playing = True
score_player = 0
score_computer = 0
while keep_playing:
symbol_player = input("What do you choose to play? (R, P, S)").upper()
while not symbol_player in SYMBOLS:
print("invalid input")
symbol_player = input("What do you choose to play? (R, P, S)").upper()
symbol_computer = random.choice(SYMBOLS)
print("You chose ", symbol_player, " and the computer chose ", symbol_computer)
difference = (SYMBOLS.index(symbol_player) - SYMBOLS.index(symbol_computer)) % 3
if difference == 0:
print("It's a tie")
if difference == 1:
score_player += 1
print("You won")
if difference == 2:
score_computer += 1
print("Computer won")
print("Score: You = ", score_player, ", Computer = ", score_computer)
play_again = input("Would you like to play again? Y/N").upper()
while not play_again in INPUTS_ALL:
print("invalid input")
play_again = input("Would you like to play again? Y/N").upper()
if play_again in INPUTS_NO:
keep_playing = False
print("You scored", score_player, "and the computer scored", score_computer)
if __name__ == "__main__":
main()
This question already has an answer here:
How can I concatenate str and int objects?
(1 answer)
Closed 6 years ago.
total_guess = 0
wins = 0
loss = 0
import random
characters = ["rock", "paper", "scissors", "lizard", "spock"]
computer = characters[random.randint(0,4)]
print(computer)
Subroutine- functions fine
def valid(text, flag):
error_message= ""
while True:
var = input(error_message + text)
if flag == "s":
if var.isalpha()==True:
break
else:
error_message = "This is not valid, "
elif flag =="i":
if var.isdigit()==True:
var = int(var)
break
else:
error_message = user_name + " this is not a number, "
elif flag == "g":
if var == "rock" or var == "paper" or var == "scissors" or var == "lizard" or var == "spock":
break
else:
error_message = user_name + " this is not valid! "
return(var)
user_name = valid("What is your name?", "s")
num_rounds = valid(user_name +" how many rounds do you want?", "i")
This code does not work either due to past changes initially worked, 'says can't convert 'int' object to str implicitly
while True:
player = valid(user_name + """ ,What do you want as your character:
Rock, paper, scissors, lizard or spock""", "g" )
while num_rounds > total_guess:
total_guess = total_guess + 1
if player == computer:
print("Draw!")
# --------------------------------------------
elif player == "Rock" or player == "rock":
if computer == "paper" or computer == "spock" :
loss = loss + 1
print("You lost ", computer, " beats ", player)
print( user_name + " you have won " + wins +" games")
if computer == "scissors" or computer == "lizard":
wins = wins + 1
print("You win", player, " beats ", computer)
# ---------------------------------------------
elif player == "Paper" or player == "paper":
if computer == "scissors" or computer == "lizard":
loss = loss + 1
print("You lost ", computer, " beats ", player)
if computer == "rock" or computer == "spock":
wins = wins + 1
print("You win", player, " beats ", computer)
# ---------------------------------------------
elif player == "Scissors" or player == "scissors":
if computer =="Spock" or computer == "rock":
loss = loss + 1
print("You lost ", computer, " beats ", player)
if computer =="paper" or computer == "lizard":
wins = wins + 1
print("You win", player, " beats ", computer)
# --------------------------------------------
elif player == "Lizard" or player =="lizard":
if computer =="scissors" or computer == "rock":
loss = loss + 1
print("You lost ", computer, " beats ", player)
if computer == "paper" or computer == "spock":
wins = wins + 1
print("You win", player, " beats ", computer)
# --------------------------------------------
elif player == "Spock" or player == "spock":
if computer == "lizard" or computer == "paper":
loss = loss + 1
print("You lost ", computer, " beats ", player)
if computer =="rock" or computer == "scissors":
wins = wins + 1
print("You win", player, " beats ", computer)
# -------------------------------------------
This block code to restart game does not work it's purpose is to have a try again feature
end_game = input("To exit enter N, to play again enter any key ")
if end_game == 'n' or end_game == 'N':
print("THANKS FOR PLAYING " + user_name + '!')
break
This could be shortened heavily but I don't feel like rewriting your entire program. This should work as expected.
total_guess = 0
wins = 0
loss = 0
import random
characters = ["rock", "paper", "scissors", "lizard", "spock"]
computer = characters[random.randint(0,4)]
print(computer)
def valid(text, flag):
error_message= ""
while True:
var = input(error_message + text)
if flag == "s":
if var.isalpha()==True:
break
else:
error_message = "This is not valid, "
elif flag =="i":
if var.isdigit()==True:
var = int(var)
break
else:
error_message = user_name + " this is not a number, "
elif flag == "g":
if var == "rock" or var == "paper" or var == "scissors" or var == "lizard" or var == "spock":
break
else:
error_message = user_name + " this is not valid! "
return(var)
user_name = valid("What is your name?", "s")
num_rounds = valid(user_name +" how many rounds do you want?", "i")
while True:
while num_rounds > total_guess:
player = valid(user_name + """ ,What do you want as your character:
Rock, paper, scissors, lizard or spock""", "g" )
total_guess = total_guess + 1
if player == computer:
print("Draw!")
# --------------------------------------------
elif player == "Rock" or player == "rock":
if computer == "paper" or computer == "spock" :
loss = loss + 1
print(' '.join(("You lost", computer, "beats", player)))
if computer == "scissors" or computer == "lizard":
wins = wins + 1
print(' '.join(("You win", player, "beats", computer)))
elif player == "Paper" or player == "paper":
if computer == "scissors" or computer == "lizard":
loss = loss + 1
print(' '.join(("You lost", computer, "beats", player)))
if computer == "rock" or computer == "spock":
wins = wins + 1
print(' '.join(("You win", player, "beats", computer)))
elif player == "Scissors" or player == "scissors":
if computer =="Spock" or computer == "rock":
loss = loss + 1
print(' '.join(("You lost", computer, " beats ", player)))
if computer =="paper" or computer == "lizard":
wins = wins + 1
print(' '.join(("You win", player, "beats", computer)))
elif player == "Lizard" or player =="lizard":
if computer =="scissors" or computer == "rock":
loss = loss + 1
print(' '.join(("You lost", computer, "beats", player)))
if computer == "paper" or computer == "spock":
wins = wins + 1
print(' '.join(("You win", player, "beats", computer)))
elif player == "Spock" or player == "spock":
if computer == "lizard" or computer == "paper":
loss = loss + 1
print(' '.join(("You lost", computer, "beats", player)))
if computer =="rock" or computer == "scissors":
wins = wins + 1
print(' '.join(("You win", player, "beats", computer)))
end_game = input("To exit enter N, to play again enter any key ")
if end_game == 'n' or end_game == 'N':
print("THANKS FOR PLAYING " + user_name + '!')
break
total_guess = 0
This code runs through properly once and then brings the error
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
paper
NameError: name 'paper' is not defined
I need the code to be able to run a game of rock, paper, lizard, spock to run 10 times showing a tally of the scores (as it goes and) at the end of the 10 (or the number of chosen) games.
Here is the code:
import random
numberofgames = raw_input("How many games do you want to play? ")
print "Please choose rock , paper , scissors , lizard, or spock (in lower case please)"
choice = raw_input("What do you choose? ")
player_choice = str(choice)
def name_to_number(name):
if name == "rock":
name = 0
return name
elif name == "spock":
name = 1
return name
elif name == "paper":
name = 2
return name
elif name == "lizard":
name = 3
return name
elif name == "scissors":
name = 4
return name
def number_to_name(number):
if number == 0:
number = "rock"
return number
elif number == 1:
number = "spock"
return number
elif number == 2:
number = "paper"
return number
elif number == 3:
number = "lizard"
return number
elif number == 4:
number = "scissors"
return number
try:
computer_choice = random.randrange(5)
player_number = name_to_number(player_choice)
print "Player choice is: " + player_choice
print "Computer choice is: " + number_to_name(computer_choice)
difference = (int(player_number) - computer_choice) % 5
draws = 0
playerwins = 0
computerwins = 0
if difference in [1, 2]:
print "Player wins!"
playerwins = playerwins + 1
elif difference == 0:
print "Player and computer tie!"
draws = draws + 1
else:
print "Computer wins!"
computerwins = computerwins + 1
print "Wins: " + str(playerwins) + "\n" + "Draws: " + str(draws) + "\n" + "Losses " + str(computerwins)
while playerwins + draws + computerwins <= numberofgames:
name_to_number()
except TypeError:
print "Sorry, please read the directions and type rock, paper, scissors, spock, or lizard in lowercase."
The code you posted raises TypeError: name_to_number() takes exactly 1 argument (0 given) because you call your name_to_number function without an argument.
Btw, here's a simpler version of that function:
name_to_number_dict = dict(rock=0, spock=1, paper=2,lizard=3,scissors=4)
def name_to_number(name):
if name not in name_to_number_dict:
raise ValueError("illegal name")
return name_to_number_dict[name]
Edit:
In fact you don't need such a function at all. A simpler approach might be the following:
import random
data = "rock", "spock", "paper", "lizard", "scissors"
def playgames():
tally = dict(win=0, draw=0, loss=0)
numberofgames = raw_input("How many games do you want to play? ")
numberofgames = int(numberofgames)
for _ in range(numberofgames):
outcome = playgame()
tally[outcome] += 1
print """
Wins: {win}
Draws: {draw}
Losses: {loss}
""".format(**tally)
def playgame():
choice = ""
while (choice not in data):
choice = raw_input("Enter choice (choose rock , paper , scissors , lizard, or spock):")
choice = choice.lower()
print "Player choice is:{}".format(choice)
player_number = data.index(choice)
computer_number = random.randrange(5)
print "Computer choice is: {}".format(data[computer_number])
difference = (player_number - computer_number) % 5
if difference in [1, 2]:
print "Player wins!"
outcome = "win"
elif difference == 0:
print "Player and computer tie!"
outcome = "draw"
else:
print "Computer wins!"
outcome = "win"
return outcome
playgames()
I need to calculate difference between 'player_number' and 'comp_number' but it says:
TypeError: unsupported operand type(s) for Sub: 'NoneType' and 'int'
I understand this error. My code generates player_number is None type that is why I can not subtract it.
How can I handle this problem? Any ideas?
Here is my code:
def name_to_number(name):
if name == "rock":
name = 0
elif name == "paper":
name = 1
elif name == "Spock":
name = 2
elif name == "lizard":
name = 3
elif name == "scissors":
name = 4
else:
print 'Name is not listed:',name
def number_to_name(number):
if number == 0:
print "rock"
elif number == 1:
print "Spock"
elif number == 2:
print "paper"
elif number == 3:
print "lizard"
elif number == 4:
print "scissors"
else:
print 'Your number is not valid:',number
def rpsls(player_choice):
if player_choice == "rock":
print 'Player choses', player_choice
player_number = name_to_number(player_choice)
elif player_choice == "Spock":
print 'Player choses',player_choice
player_number = name_to_number(player_choice)
elif player_choice == "paper":
print 'Player choses',player_choice
player_number = name_to_number(player_choice)
elif player_choice == "lizard":
print 'Player choses',player_choice
player_number = name_to_number(player_choice)
elif player_choice == "scissors":
print 'Player choses',player_choice
player_number = name_to_number(player_choice)
else:
print "Name not in list",player_choice
import random
comp_number = random.randrange(0,4)
if comp_number == 0:
print "Computer choses",number_to_name(0)
elif comp_number == 1:
print "Computer choses",number_to_name(1)
elif comp_number == 2:
print "Computer choses",number_to_name(2)
elif comp_number == 3:
print "Computer choses",number_to_name(3)
elif comp_number == 4:
print "Computer choses",number_to_name(4)
diffrence = player_number - comp_number
if diffrence % 5 == 1 or 2:
print 'Player wins!'
elif (diffrence % 5) == 3 or 4:
print 'Computer wins!'
else:
print 'Game tie'
rpsls("rock")
You have this assigment
player_number = name_to_number(player_choice)
But none of your if/elif/else cases in name_to_number use the return keyword. To return a value from this function, you would do
def name_to_number(name):
if name == "rock":
return 0
elif name == "paper":
return 1
elif name == "Spock":
return 2
elif name == "lizard":
return 3
elif name == "scissors":
return 4
else:
print 'Name is not listed:',name
The same goes for your number_to_name function
Untested Fix. 'return' statement was missing. Using the same variable name for a string and an int is not nice programming style.
def name_to_number(name):
number = -1
if name == "rock":
number = 0
elif name == "paper":
number = 1
elif name == "Spock":
number = 2
elif name == "lizard":
number = 3
elif name == "scissors":
number = 4
else:
print 'Name is not listed:',name
return number #This line was missing