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()
Related
My teacher wants us to create a basic rock paper scissors game that loops until the user ends the game, at which point the program will add up all the wins/losses/ties (of the player) and display it. Here is what I have so far, but I cannot figure out how to create that running tally in the background that will spit out the calculation at the end. (The bottom part of the win/loss/tie product is written by my teacher. It must print this way.)
def main():
import random
Wins = 0
Ties = 0
Losses = 0
while True:
user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
if user_action == computer_action:
print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
if computer_action == "scissors":
print("Rock smashes scissors! You win!")
else:
print("Paper covers rock! You lose.")
elif user_action == "paper":
if computer_action == "rock":
print("Paper covers rock! You win!")
else:
print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
if computer_action == "paper":
print("Scissors cuts paper! You win!")
else:
print("Rock smashes scissors! You lose.")
play_again = input("Play again? (y/n): ")
if play_again.lower() != "y":
print("Good Game!")
print("Wins \t Ties \t Losses")
print("---- \t ---- \t ------")
print(wins, "\t", ties , "\t", losses)import random
Where you are printing out wether the user has tied, lost or won you can simply increment the related value.
The text may not be displaying together due to the text not being the same size, add more /t to the strings and it should line up.
For my exercise, I have to get the computer to play against the user in RPS. However, after both the computer and I input our action, the user will always receive a 'You Lose'.
import random
def get_computer_move():
"""Deterministic 'random' move chooser"""
options = ['rock', 'paper', 'scissors']
return options[random.randint(0, 2)]
def game_result(move1, move2):
"""game_result *exactly* as defined in question one"""
options = ["rock", "paper", "scissors"]
if move1 == move2:
return 0
elif move1 == "paper" and move2 == "rock":
return 1
elif move1 == "rock" and move2 == "scissors":
return 1
elif move1 == "scissors" and move2 == "paper":
return 1
else:
return 2
def main():
"""Runs a single game of PSR"""
print("Let's Play Paper, Scissors, Rock!")
user1 = input("Player one move: ").lower()
computer1 = print("The computer plays " + get_computer_move())
game_result(computer1, user1)
if user1 == computer1:
return print("It's a draw")
elif user1 == "paper" and computer1 == "rock":
return print("You win")
elif user1 == "rock" and computer1 == "scissors":
return print("You win")
elif user1 == "scissors" and computer1 == "paper":
return print("You win")
else:
return print("You lose")
main()
Here is my code although it is quite messy.
What I want to happen is:
Let's Play Paper, Scissors, Rock!
Player one move: ROCK
The computer plays rock
It's a draw
But it will always come out as:
Let's Play Paper, Scissors, Rock!
Player one move: ROCK
The computer plays rock
You lose
Help would be appreciated.
print returns none, you want to save the computers move and then print that separately
computer1 = get_computer_move()
print("The computer plays", computer1)
I am new to python and as a practice I'm trying to write a rock paper scissors with it.
I triple checked eth but i can't find the problem.
I'm using visual studio code console. the msvcrt is for "getch" and I'm not sure about about the random function syntax
problem: when you give it the input number, It doesn't do or write anything despite the program.
help me pleaaaaase.
import random
import msvcrt
##################################################
player_move = " "
system_move = " "
##################################################
def rand(system_move):
rn = random.randint(1, 3)
if rn == 1:
system_move = "Rock"
elif rn == 2:
system_move = "Paper"
elif rn == 3:
system_move = "Scissors"
else:
rand()
return system_move
##################################################
print("!!! Rock, Paper, Scissors !!!\n\n\n")
###################################################
def playermove(player_move):
print("What do you want to go with?\n1-Rock\n2-paper\n3-scissors\n")
temp = msvcrt.getch()
if temp == '1' or 1:
player_move = 'Rock'
elif temp == '2' or 2:
player_move = 'Paper'
elif temp == '3' or 3:
player_move = 'Scissors'
else:
print(f"invalid input {player_move}. Try again\n")
playermove()
return player_move
###################################################
pm = ' '
sm = ' '
rand(sm)
playermove(pm)
if pm== 'Scissors':
if sm == "Scissors":
print(f"System move: {sm}\nIt's a tie!\n")
elif sm == "Rock":
print(f"System move: {sm}\nSystem won!\n")
elif sm == "Paper":
print(f"System move: {sm}\nYou won!\n")
else:
print("Oops! Something went wrong.\n")
elif pm == "Paper":
if sm == "Scissors":
print(f"System move: {sm}\nSystem won!\n")
elif sm == "Rock":
print(f"System move: {sm}\nYou won!\n")
elif sm == "Paper":
print(f"System move: {sm}\nIt's a tie!\n")
else:
print("Oops! Something went wrong.\n")
elif pm == "Rock":
if sm == "Scissors":
print(f"System move: {sm}\nYou won!\n")
elif sm == "Rock":
print(f"System move: {sm}\nIt's a tie!\n")
elif sm == "Paper":
print(f"System move: {sm}\nSystem won!\n")
else:
print("Oops! Something went wrong.\n")
print("Press any key to exit...")
xyz = msvcrt.getch()
Well, the reason your code doesn't work is because you are not assigning the return value of your functions to any variable. To fix your code you need to do the following:
sm = rand(sm)
pm = playermove(pm)
In Python, passing an immutable object like string means that you cannot make any changes to it. As you're not using the passed value, your function's signature should actually look like this.
def rand()
def playermove()
After you do this, you'll see there are other things you need to fix in your code.
Try this code. Just replace snake, water, gun by rock, paper, scissors.
print(" \t\t\t Welecome to Snake , Water , Gun game\n\n")
import random
attempts= 1
your_point=0
computer_point=0
while (attempts<=10):
lst=["snake","water","gun"]
ran=random.choice(lst)
print("what do you choose snake, water or gun")
inp=input()
if inp==ran:
print("tie")
print(f"you choosed {inp} and computer guess is {ran}")
print("No body gets point\n")
elif inp=="snake" and ran=="water":
your_point=your_point+1
print(f"you choosed {inp} and computer guess is {ran}")
print("The snake drank water\n")
print("You won this round")
print("you get 1 point\n")
elif inp=="water" and ran=="snake":
computer_point = computer_point + 1
print(f"you choosed {inp} and computer guess is {ran}")
print("The snake drank water\n")
print("You Lost this round")
print("computer gets 1 point\n")
elif inp=="water" and ran=="gun":
print(f"you choosed {inp} and computer guess is {ran}")
your_point = your_point + 1
print("The gun sank in water\n")
print("You won this round")
print("you get 1 point\n")
elif inp == "gun" and ran == "water":
print(f"you choosed {inp} and computer guess is {ran}")
computer_point = computer_point + 1
print("The gun sank in water\n")
print("You Lost this round")
print("computer gets 1 point\n")
elif inp == "gun" and ran == "snake":
print(f"you choosed {inp} and computer guess is {ran}")
your_point = your_point + 1
print("The snake was shoot and he died\n")
print("You Won this round")
print("you get 1 point\n")
elif inp == "snake" and ran == "gun":
print(f"you choosed {inp} and computer guess is {ran}")
computer_point=computer_point+1
print("The snake was shoot and he died\n")
print("You Lost this round")
print("computer gets 1 point\n")
else:
print("invalid")
print(10 - attempts, "no. of guesses left")
attempts = attempts + 1
if attempts>10:
print("game over")
if computer_point > your_point:
print("Computer wins and you loose")
if computer_point < your_point:
print("you win and computer loose")
print(f"your point is {your_point} and computer point is {computer_point}")
I am attempting to make a basic rock, paper, scissors game. When I input either rock, paper, or scissors, I sometimes have to enter the same thing multiple times for it to continue to the if statements. See code below:
# Rock, Paper, Scissors
player_total = 0
computer_total = 0
def get_computer_hand():
choice = randint(1, 3)
if choice == 1:
return "scissors"
elif choice == 2:
return "paper"
else:
return "rock"
def ask_user():
global player_total
global computer_total
player = input("Enter your hand (stop to stop): ")
if player == "stop":
print("Computer had ", computer_total, "points, you had ", player_total, " points.")
exit(0)
computer = get_computer_hand()
if player == "rock":
if computer == "paper":
return "win"
elif computer == "scissors":
return "lose"
else:
return "tie"
elif player == "paper":
if computer == "paper":
return "tie"
elif computer == "scissors":
return "lose"
else:
return "win"
elif player == "scissors":
if computer == "scissors":
return "tie"
elif computer == "paper":
return "win"
else:
return "lose"
def count_winner():
global player_total
global computer_total
player_total = 0
computer_total = 0
while True:
outcome = ask_user()
if outcome == "win":
print("You won that one.")
player_total += 1
elif outcome == "lose":
print("Computer won that one.")
computer_total += 1
count_winner()
I expect it to work the first time and to continue as usual, but I can't seem to figure out why it just asks "Enter your hand (stop to stop): " instead sometimes when I enter either rock, paper, or scissors.
This is happening because there is a tie happening between the computer and the user. This could be fixed by adding the end with the code of
else outcome == "tie":
print("You have tied with the Computer!")
computer_total += 1
player_total += 1
This would add a point to both sides and if you don't want that just delete the last two lines of my code
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()