Function not taking changed variable [duplicate] - python

This question already has answers here:
Why does assigning to my global variables not work in Python?
(6 answers)
Closed 8 months ago.
I'm trying to create a rock, paper, scissors game using functions but when I run the game it doesn't show who won. The function is called "Game()". That is the code:
import random
playerChoice=0
computerChoice=0
computerChoices=["Rock","Paper","Scissors"]
def Game():
def Rock():
if playerChoice=="Rock":
if computerChoice=="Rock":
print("Tie")
if computerChoice=="Paper":
print("You Lost")
if computerChoice=="Scissors":
print("You Won")
else:
Paper()
def Paper():
if playerChoice=="Paper":
if computerChoice=="Paper":
print("Tie")
if computerChoice=="Scissors":
print("You Lost")
if computerChoice=="Rock":
print("You Won")
else:
Scissors()
def Scissors():
if computerChoice=="Scissors":
print("Tie")
if computerChoice=="Rock":
print("You Lost")
if computerChoice=="Paper":
print("You Won")
Rock()
def Play():
playerChoice=input("Do you pick rock, paper or scissors? \n").capitalize()
computerChoice=random.choice(computerChoices)
print(f"Computer Picked {computerChoice}")
Game()
Play()

If you examine the choices right before you call Rock() in Game, you'll see that playerChoice and computerChoice are still set to 0. This is because when you do
playerChoice=input("Do you pick rock, paper or scissors? \n").capitalize()
computerChoice=random.choice(computerChoices)
it doesn't actually change the values of the global variables playerChoice or computerChoice. This is called shadowing.
What you can do is accept the choices as arguments:
def Game(playerChoice, computerChoice):
# ...
def Play():
playerChoice=input("Do you pick rock, paper or scissors? \n").capitalize()
computerChoice=random.choice(computerChoices)
print(f"Computer Picked {computerChoice}")
Game(playerChoice, computerChoice)
Also, you have an indentation error in Paper(), it should be this:
def Paper():
if playerChoice=="Paper":
if computerChoice=="Paper":
print("Tie")
if computerChoice=="Scissors":
print("You Lost")
if computerChoice=="Rock":
print("You Won")
else:
Scissors()

Your if statements are a bit messed up. Try using elif more.Try studying this.
import random
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.")

Related

RPS- Creating a tally with end result in Python

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.

Rock, Paper, Scissors. Code won't read user input that's lowercase

I made a rock paper scissors game (code below). Is there a simpler way of making the code able to read both uppercase and lowercase inputs? So far, it only reads uppercase (i.e. Rock, Paper, Scissors). If the input is lowercase (i.e. rock, paper, scissors), the game won't tell you if you've won or not.
import random
import time
options = ["Rock", "Paper", "Scissors"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n")
computerChoice = (random.choice(options))
print("Rock...")
time.sleep(1)
print("Paper...")
time.sleep(1)
print("Scissors...")
time.sleep(1)
print("Shoot! \n")
time.sleep(1)
print("You chose " + yourMove)
print("The computer chose " + computerChoice + "\n")
if yourMove == "Rock" and computerChoice == "Paper":
print("You lose!")
elif yourMove == "Paper" and computerChoice == "Rock":
print("You win!")
if yourMove == "Scissors" and computerChoice == "Rock":
print("You lose!")
elif yourMove == "Rock" and computerChoice == "Scissors":
print("You win!")
if yourMove == "Paper" and computerChoice == "Scissors":
print("You lose!")
elif yourMove == "Scissors" and computerChoice == "Paper":
print("You win!")
if yourMove == computerChoice:
print("It's a draw!")
I would recommend that if you are going to work with strings in a list, leave everything in lowercase.
When capturing the value desired by the user, you could use:
value = input("[Text] ").lower().strip()
Python is a case sensitive language, so
"Paper" != "paper" or " paper " != " paper"
python has many methods to work with strings.
your program can use many of them and work.
here are some solutions.
1- use lower() to make user input all lowercase and your options should be lower cased too.
options = ["rock", "paper", "scissors"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n").lower()
2- use upper() to make user input all uppercase and your options should be upper case.
options = ["ROCK", "PAPER", "SCISSORS"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n").upper()
3- or you can keep your current options and just use capitalize() to make the first letter uppercase.
options = ["Rock", "Paper", "Scissors"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n").capitalize()

I wrote a simple python game but functions in that are not executing

I am fairly new in python coding. I made a simple rock, paper, scissor game using random module. I wrote it in three functions. when I am executing it, I get no error but it is not executing the functions.
what I wrote:
import random
def main():
global comp
global user
global play
play = ''
#preparing list for computer to select from
game = ['rock', 'paper', 'scissor']
#computer chooses randomly from the list above
comp = random.choice(game)
print(comp)
#user inputs their choice
user = input("Rock, Paper or Scissor? ")
def user_choice():
global play
play = input("Do you want to continue? press y or n for yes and no respectively. ")
if play == y:
main()
elif play == n:
print("Thank you for playing!")
exit()
#conditions to the game
def play_game():
global user
global comp
while True:
if user == comp:
print("Tie!")
print("Computer: ", comp, "and User:", user)
elif user == 'rock':
if comp == 'paper':
print("Rock covers paper, computer wins")
else:
print("rock smashes through scissor, you win")
elif user == 'paper':
if comp == 'rock':
print("paper covers rock, you win!")
else:
print("scissor cuts through paper, computer wins")
elif user == 'scissor':
if comp == 'rock':
print("Rock smashes through scissor, Computer wins!")
else:
print("Scissor cuts through paper")
user_choice()
if __name__ == '__main__':
main()
play_game()
it is not executing play_game() and user_choice().
It only ask for the input.
The first thing which is wrong is that y and n are not under "" thus python is treating them as a variable. the second thing which was wrong is the position of the play game which should come below the main() to get executed. All user_choice() was not indented properly
Try this code
import random
def main():
global comp
global user
global play
play = ''
# preparing list for computer to select from
game = ['rock', 'paper', 'scissor']
# computer chooses randomly from the list above
comp = random.choice(game)
print(comp)
# user inputs their choice
user = input("Rock, Paper or Scissor? ")
def user_choice():
global play
play = input(
"Do you want to continue? press y or n for yes and no respectively. ")
if play == "y":
main()
elif play == "n":
print("Thank you for playing!")
exit()
# conditions to the game
def play_game():
global user
global comp
while True:
if user == comp:
print("Tie!")
print("Computer: ", comp, "and User:", user)
elif user == 'rock':
if comp == 'paper':
print("Rock covers paper, computer wins")
else:
print("rock smashes through scissor, you win")
elif user == 'paper':
if comp == 'rock':
print("paper covers rock, you win!")
else:
print("scissor cuts through paper, computer wins")
elif user == 'scissor':
if comp == 'rock':
print("Rock smashes through scissor, Computer wins!")
else:
print("Scissor cuts through paper")
user_choice()
if __name__ == '__main__':
main()
play_game()
Create another function main() and call play_game() and user_choice() within main.
The code calls the main() function and this takes an input, but it does not call any of the other functions.
This is why.
Expect that you mean to call play_game() and user_choice() from within main().
Working code.
import random
def main():
global comp
global user
global play
play = ''
#preparing list for computer to select from
game = ['rock', 'paper', 'scissor']
#computer chooses randomly from the list above
comp = random.choice(game)
print(comp)
#user inputs their choice
user = input("Rock, Paper or Scissor? ")
user_choice()
play_game()
def user_choice():
global play
play = input("Do you want to continue? press y or n for yes and no respectively. ")
if play == 'y':
main()
elif play == 'n':
print("Thank you for playing!")
exit()
#conditions to the game
def play_game():
global user
global comp
while True:
if user == comp:
print("Tie!")
print("Computer: ", comp, "and User:", user)
elif user == 'rock':
if comp == 'paper':
print("Rock covers paper, computer wins")
else:
print("rock smashes through scissor, you win")
elif user == 'paper':
if comp == 'rock':
print("paper covers rock, you win!")
else:
print("scissor cuts through paper, computer wins")
elif user == 'scissor':
if comp == 'rock':
print("Rock smashes through scissor, Computer wins!")
else:
print("Scissor cuts through paper")
if __name__ == '__main__':
main()

If condition within game running as if true, when it should be false. (Python) [duplicate]

This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 1 year ago.
When I run this code and input "r" "p" or "s" the first if condition within the function runs as though it is true. My goal is for the else statement to run when the user inputs a valid answer, such as "s" "p" or "r". Tried debugging, calling the global choice variable, not sure what to do here. Help would be appreciatd. This is Python 3.7.
possible_options = ["rock", "paper", "scissors"]
choice = input("Enter r for rock, p for paper, and s for scissors: ")
def game():
global choice
print(choice)
if choice != "r" or choice != "p" or choice != "s":
choice = input("Invalid choice. Must be r, p, or s: ")
game()
else:
selection = choice
if selection == "r":
if random.choice(possible_options) == "rock":
print("The computer chose rock!")
print("It was a tie!")
elif random.choice(possible_options) == "paper":
print("The computer chose paper!")
print("Paper covers rock! You lose.")
elif random.choice(possible_options) == "scissors":
print("The computer chose scissors!")
print("Rock smashes scissors! You win!")
elif selection == "p":
if random.choice(possible_options) == "rock":
print("The computer chose rock!")
print("Paper covers rock! You win!")
elif random.choice(possible_options) == "paper":
print("The computer chose paper!")
print("It was a tie!")
elif random.choice(possible_options) == "scissors":
print("The computer chose scissors!")
print("Scissors cut paper! You lose.")
elif selection == "s":
if random.choice(possible_options) == "rock":
print("The computer chose rock!")
print("Rock smashes scissors! You lose.")
elif random.choice(possible_options) == "paper":
print("The computer chose paper!")
print("Scissors cut paper! You win!")
elif random.choice(possible_options) == "scissors":
print("The computer chose scissors!")
print("It was a tie!")
game()
Replace
if choice != "r" or choice != "p" or choice != "s":
with
if choice != "r" and choice != "p" and choice != "s":

Rock, Paper And Scissors Program (Upper And Lower Case Problem)

I Was Practicing Programs Of Python And Then I Made This Rock, Paper, Scissors Program. And The Program Is Running Perfectly But The Problem Is Like This:
I Chose "Rock, Paper, Scissors" Like This Than It Runs.
But If I Enter Rock, Paper, Scissors In Any Other Way It Prints The Else Statment i.e., Invalid Choice.
I Want To Use Upper/Lower Case But How And Where.
import random
user = input("""Tell Me What Do You Choose:
~Rock
~Paper
~Scissors.
-> I Choose : """)
choose = ["Rock", "Paper", "Scissors"]
computer = random.choice(choose)
print(f"Computer Chose {computer}.")
#Function To Compare Choices
def compare(user, computer):
if user == computer:
return("It's A Tie :(....")
elif user == "Rock":
if computer == "Scissors":
return("User Wins!")
else:
return("Computer Wins!")
elif user == "Scissors":
if computer == "Paper":
return("User Wins!")
else:
return("Computer Wins!")
elif user == "Paper":
if computer == "Rock":
return("User Wins!")
else:
return("Computer Wins!")
else:
return("User Choose Out Of The Options!")
exit()
print(compare(user, computer))
just transform your input into lowercase with the lower() method
user = user.lower()
that makes sCiSsOrS into scissors
you also need to adjust your if statements to be all lowercase
elif user == "Rock": --> elif user == "rock":

Categories