Python: Game not running through - python

I have written logic in python for the game of rock, paper , scissor but having trouble to get it to run.
It wont run the through the entire game and just keeps asking me for my input. I have looked through it multiple times but cant see where it might be getting stuck.
Any idea what the issue may be?
Code is as below:
import random
comp_wins = 0
player_wins = 0
def choose_option ():
user_choice = input("choose rock, paper and scissors: ")
if user_choice in ["rock","Rock"]:
user_choice = "rock"
elif user_choice in ["paper","Paper"]:
user_choice = "paper"
elif user_choice in ["scissors","Scissor"]:
user_choice = "scissors"
else:
print ("error try again")
return user_choice
def computer_option ():
comp_choice = random.randint(1,3)
if comp_choice ==1:
comp_choice = "rock"
elif comp_choice ==2:
comp_choice = "paper"
elif comp_choice ==3:
comp_choice = "scissors"
return comp_choice
while True:
print("")
user_choice = choose_option ()
comp_choice = computer_option ()
print ("")
if user_choice ==["rock"]:
if comp_choice == "rock":
print("draw")
elif comp_choice == "paper":
print("computer wins")
comp_wins += 1
elif comp_choice == "scissors":
print("user wins")
player_wins += 1
elif user_choice == "paper":
if comp_choice == "rock":
print("user wins")
player_wins += 1
elif comp_choice == "paper":
print("draw")
elif comp_choice == "scissors":
print("computer wins")
comp_wins += 1
elif user_choice == "scissors":
if comp_choice == "rock":
print("computer wins")
comp_wins += 1
elif comp_choice == "paper":
print("user wins")
player_wins += 1
elif comp_choice == "scissors":
print("draw")
print("")
print("user_wins: " + str(player_wins))
print("comp_wins: " + str(comp_wins))
print("")

Youur issue is here
while True:
print("")
user_choice = choose_option ()
comp_choice = computer_option ()
print ("")
You never exit this while, you should put an exit condition or a flag when a choice has been made.
Also it would be better to clean up and use a Top-Down structure with a main function in which you control the file.

It looks that there is a wrong indent: the main while True loop never ends. Try to move all blocks after this loop to the right (i.e. add four spaces in the beginning of all next lines).

Your indentation levels aren't correct. The if user_choice ==["rock"]: part is the same indentation level as the while statement, so it isn't included in the loop. You need to indent it to let Python know it's part of the loop block. And since you don't have anything that ends the loop, it will go on forever. Also, using strings to represent choices is really inefficient. If you represent choices with numbers, you can use modular arithmetic.

Related

First little program, cant seem to fix the capitalization error

This is my first little program I have created. Can anyone help me with making it so it works with the first letter being capitalized?
# Rock Paper Scissors
import time
import random
user_wins = 0
computer_wins = 0
def RockPaperScissors():
user_choice = input("Enter your choice in a game of Rock, Paper, Scissors: ")
if user_choice == "rock" or user_choice == "paper" or user_choice == "scissors":
print("Good choice, lets see who wins.")
time.sleep(1.5)
else:
print('Please enter either "rock", "paper", or "scissors"')
computer_choice = ["rock", "paper", "scissors"]
rand_choice = random.choice(computer_choice)
print("The computer chose", rand_choice)
if user_choice == "rock" and rand_choice == "scissors":
print("You won!")
elif user_choice == "scissors" and rand_choice == "rock":
print("The computer won, try again.")
elif user_choice == "paper" and rand_choice == "rock":
print("You won!")
elif user_choice == "rock" and rand_choice == "paper":
print("The computer won, try again")
elif user_choice == "scissors" and rand_choice == "paper":
print("You won!")
elif user_choice == "paper" and rand_choice == "scissors":
print("The computer won, try again")
RockPaperScissors()
I also have a slight issue with if you capitalize the first letter, the program does not run as intended. Appreciate the help!

Rock, paper, scissors game in Python - some clarification needed

I am writing a simple game of rock, paper and scissors, where the user competes with a computer. There are 5 tries.
The problems I ran into are :
If I enter something apart from three options, it should return "Invalid Entry". Instead, the program stops
The program never prints "You won the game" or "You lost the game", it finishes after 5 attempts
Other feedback would also be appreciated
import random
def game():
win_count = 0
loose_count = 0
tries = 0
while tries < 5:
chosen = input("Make your choice: ")
if chosen == "scissors" or chosen == "Scissors":
element = "scissors"
elif chosen == "paper" or chosen == "Paper":
element = "paper"
elif chosen == "rock" or chosen == "Rock":
element = "rock"
else:
return "Invalid Entry"
computer_choices = ["scissors", "paper", "rock"]
computer_choice = random.choice(computer_choices)
if element == "scissors" and computer_choice == "paper":
print("Computer chose paper, you chose scissors, you win !")
win_count += 1
tries += 1
elif element == "paper" and computer_choice == "scissors":
print("Computer chose scissors, you chose paper, you loose !")
loose_count += 1
tries += 1
elif element == "paper" and computer_choice == "rock":
print("Computer chose rock, you chose paper, you win !")
win_count += 1
tries += 1
elif element == "rock" and computer_choice == "paper":
print("Computer chose paper, you chose rock, you loose !")
loose_count += 1
tries += 1
else:
print("Whoops, that's a draw, try again")
tries+=1
print("Your Wins: "+ str(win_count))
print("Computer Wins: "+str(loose_count))
if win_count > loose_count:
return "Congrats, you won the game!"
else:
return "Sorry, you lost"
game()
return statement does not print anything but they return the value from a function and as soon as a return statement executes the function ends up executing that's why whenever the user inputs something invalid the program stop.
Also not use the if statement from if chosen = paper or chosen = Paper instead use .lower() to lower the string.
I have made some changes to your code.
Try this code
import random
def game():
win_count = 0
loose_count = 0
tries = 0
while tries < 5:
element = input("Make your choice: ").lower()
computer_choices = ["scissors", "paper", "rock"]
if element not in computer_choices:
print("Invalid choice")
continue
computer_choice = random.choice(computer_choices)
if element == "scissors" and computer_choice == "paper":
print("Computer chose paper, you chose scissors, you win !")
win_count += 1
tries += 1
elif element == "paper" and computer_choice == "scissors":
print("Computer chose scissors, you chose paper, you loose !")
loose_count += 1
tries += 1
elif element == "paper" and computer_choice == "rock":
print("Computer chose rock, you chose paper, you win !")
win_count += 1
tries += 1
elif element == "rock" and computer_choice == "paper":
print("Computer chose paper, you chose rock, you loose !")
loose_count += 1
tries += 1
else:
print("Whoops, that's a draw, try again")
tries+=1
print("Your Wins: "+ str(win_count))
print("Computer Wins: "+str(loose_count))
if win_count > loose_count:
print("Congrats, you won the game!")
else:
print("Sorry, you lost")
game()

How do I make my basic Rock/Paper/Scissors game with fewer lines of code?

I built this simple rock/paper/scissor game by watching some tutorials. Everything is working fine. The issue is that I want to implement something where if the user and the computer choose the same word, then it should say something like "draw."
I could go ahead and add a bunch of "if" and "else" statements, but I don't want that. Can you guys think of any other way to implement that with fewer lines of code?
#Simple rock, paper and scissor game
import random
user_wins = 0
computer_wins = 0
options = ["rock", "paper", "scissors"]
while True:
user_pick = input("Please choose Rock/Paper/Scissors Or Press Q to quit: ")
if user_pick.lower() == "q":
print("You quit.")
break
elif user_pick not in options:
print("Please enter a valid input.")
continue
random_number = random.randint(0, 2)
computer_pick = options[random_number]
print("The computer picked", computer_pick)
if computer_pick == "rock" and user_pick == "scissors":
print("You lost!")
computer_wins += 1
elif computer_pick == "paper" and user_pick == "rock":
print("You lost!")
computer_wins += 1
elif computer_pick == "scissors" and user_pick == "paper":
print("You lost!")
computer_wins += 1
else:
print('You win!')
user_wins += 1
You could use a dictionary to map which choices beat each other. This will enable to you make the conditional part of the code, which determines who wins, more concise. It is also more easily expanded to rock-paper-scissors variants with more options, without the need for many more conditional statements.
#Simple rock, paper and scissor game
import random
user_wins = 0
computer_wins = 0
options = ["rock", "paper", "scissors"]
beaten_by = {
"rock": "scissors",
"scissors": "paper",
"paper": "rock"
}
while True:
user_pick = input("Please choose Rock/Paper/Scissors Or Press Q to quit: ")
if user_pick.lower() == "q":
print("You quit.")
break
elif user_pick not in options:
print("Please enter a valid input.")
continue
random_number = random.randint(0, 2)
computer_pick = options[random_number]
print("The computer picked", computer_pick)
if user_pick == computer_pick:
print("Draw!")
elif user_pick == beaten_by[computer_pick]:
print("You lost!")
computer_wins += 1
else:
print("You win!")
user_wins += 1

Why is a random result for the code being displayed?

main.py:
import functions
def main():
count = True
while count == True:
print("Enter R for Rock")
print("Enter P for Paper")
print("Enter S for Scissors")
value = input("Choose from R/P/S: ") # taking input from the user, they can tpye R/P/S or r/p/s to play a game. Rock Paper Scissors
if value == "R" or value =="r":
print()
print(functions.rock(value))
elif value == "P" or value =="p":
print()
print(functions.paper(value))
elif value == "S" or value =="s":
print()
print(functions.scissors(value))
#Using If statement so if the user input is "R" then it will call the rock fucntion, if its "P" it will call the paper function and if its "S" it will call the scissors function.
else:
print()
print("Invalid Input, Please enter R for Rock, P for Paper, S for Scissors")
# if there's an invalid input the program will stop and redirect them to the correct input process
break
print("------------")
main()
function.py:
import random
def computer_choice():
choice = random.randint(1, 3)
if choice == 1:
return "Rock"
elif choice == 2:
return "Paper"
elif choice == 3:
return "Scissors"
# getting a random string from a,b,c variable. The random variable will be the computers input
def rock(value):
print("Computer choice:", computer_choice())
print()
if computer_choice() == "Rock":
return("Its a draw")
elif computer_choice() == "Paper":
return("The computer wins")
else:
return("You Win")
def paper(value):
print("Computer choice:", computer_choice())
print()
if computer_choice() == "Rock":
return("You Win")
elif computer_choice() == "Paper" :
return("Its a draw")
else:
return("The computer wins")
def scissors(value):
print("Computer choice:", computer_choice())
print()
if computer_choice() == "Rock":
return("The computer wins")
elif computer_choice() == "Paper":
return("You win")
else:
return("Its a draw")
# defining all the functions and giving each function a decision structure and the pattern for rock paper scissors.
Why is a random result for the code being displayed? The only thing random was supposed to be the choice of the computer but the results are random too The code seems to send random results from the functions where I used the random function. The "function.py" returns a random result and not the one that was supposed to be send
Every time you use computer_choice() in an if statement it is generating a new random choice because you are calling the function. Instead you want to make a variable and assign computer_choice() to it, then refer to this variable in your if statements.
For example rock would become:
def rock(value):
comp = computer_choice()
print("Computer choice:", comp)
print()
if comp == "Rock":
return("Its a draw")
elif comp == "Paper":
return("The computer wins")
else:
return("You Win")

My while loop in the rock, paper and scissor game isn't working. How should I correct it?

The game should end when either the human or the computer reaches 5 points. This game was just a function before. I then added the input function and the while loop to make it more efficient and user-friendly.
import random
choices=('rock','paper','scissor')
HUMAN_SCORE=0
COMPUTER_SCORE=0
while COMPUTER_SCORE<5 or HUMAN_SCORE<5:
computer=random.choice(choices)
human=input("Choose from rock,paper or scissor")
print("You picked:")
print(human)
print("Computer picked:")
print(computer)
if human == "rock" and computer == "rock":
print("play again")
elif human == "rock" and computer == "paper":
COMPUTER_SCORE=COMPUTER_SCORE+1
print("sorry,you lost!Better luck next time!")
elif human == "rock" and computer == "scissor":
HUMAN_SCORE=HUMAN_SCORE+1
print("Congratulations,you won!")
elif human == "paper" and computer == "paper":
print("play again")
elif human == "paper" and computer == "scissor":
COMPUTER_SCORE = COMPUTER_SCORE + 1
print("sorry,you lost!Better luck next time!")
elif human == "paper" and computer == "rock":
print("Congratulations,you won!")
HUMAN_SCORE = HUMAN_SCORE + 1
elif human == "scissor" and computer == "scissor":
print("play again")
elif human == "scissor" and computer == "rock":
print("sorry,you lost!Better luck next time!")
COMPUTER_SCORE = COMPUTER_SCORE + 1
elif human == "scissor" and computer == "paper":
print("Congratulations,you won!")
HUMAN_SCORE = HUMAN_SCORE + 1
else:
print("try again!Choose from the options above!")
print("Human_Score=")
print(HUMAN_SCORE)
print("Computer_Score=")
`print(COMPUTER_SCORE)
while COMPUTER_SCORE<5 or HUMAN_SCORE<5:
Most likely this will always be true (the human or computer can't both hit 5 at the same time so one must be true)
You need to make sure they're both less than 5 with and
while COMPUTER_SCORE<5 and HUMAN_SCORE<5:

Categories

Resources