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!
Related
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
I have created a rock, paper scissors game in Python, posted it on replit, and now the 'if statements' refuse to function.
print("Welcome to the official game of Rock, Paper, Scissors!")
option = input('''Pick an option: Rock, Paper, Scissors
>>> ''').lower()
import random
game_op =["rock", "paper", "scissors"]
computer_option = print(random.choice(game_op))
def options():
if option == "rock" and computer_option == "rock":
print("You and the Computer chose the same options.")
print('The end')
elif option == "rock" and computer_option == "paper":
print("The Computer wins!")
print('The end')
elif option == "rock" and computer_option == "scissors":
print("You win!")
print('The end')
elif option == "paper" and computer_option == "rock":
print("You win!")
print('The end')
elif option == "paper" and computer_option == "paper":
print("You and the Computer chose the same options.")
print('The end')
elif option == "paper" and computer_option == "scissors":
print("The Computer wins!")
print('The end')
elif option == "scissors" and computer_option == "rock":
print("The Computer wins!")
print('The end')
elif option == "scissors" and computer_option == "scissors":
print("You and the Computer chose the same options.")
print('The end')
elif option == "scissors" and computer_option == "paper":
print("You win!")
print('The end')`enter code here`
print(options())
I tried making a function to handle the if statements and when you and the computer have decided on your move, the function works, but all its printing is 'None'.
In your code, you have the line option.lower. This does not call any function nor does it reassign option to anything. Thus, if the user inputs "ROCK", all the if and elif clauses will be ignored. Rewrite the first few lines to:
print("Welcome to the official game of Rock, Paper, Scissors!")
option = input('''Pick an option: Rock, Paper, Scissors
>>> ''').lower()
Note that inputting something like "banana" will still cause all of the if and elif clauses to be ignored.
The function is not working and therefore my score is not adding. I would like to run this just like a normal rock paper scissors game. It would be highly appreciated if you are able to solve this problem.
import random
user_score = 0
computer_score = 0
def Choose_Option():
user_choice = input("Choose Rock, Paper, Scissors: ")
if user_choice in ["Rock", "rock", "r", "R"]:
user_choice = "r"
elif user_choice in ["Paper", "paper", "p", "P"]:
user_choice = "p"
elif user_choice in ["Scissors", "scissors", "s", "S"]:
user_choice = "s"
else:
print("I don't understand. Try Again")
Choose_option()
return user_choice
def Computer_Option():
computer_choice = random.randint(1,3)
if computer_choice == 1:
computer_choice == "r"
elif computer_choice == 2:
computer_choice == "p"
else:
computer_choice == "s"
return computer_choice
while True:
print("")
user_choice = Choose_Option()
computer_choice = Computer_Option()
print("")
if user_choice == "r":
if computer_choice == "r":
print("You choose rock. Computer choose rock. Tied")
elif computer_choice == "p":
print("You choose rock. Computer choose paper. You Lose")
computer_score += 1
elif computer_choice == "s":
print("You choose rock. Computer choose scissors. You Win")
user_score += 1
elif user_choice == "p":
if computer_choice == "p":
print("You choose paper. Computer choose paper. Tied")
elif computer_choice == "r":
print("You choose paper. Computer choose rock. You Win")
user_score += 1
elif computer_choice == "s":
print("You choose paper. Computer choose scissors. You lose")
computer_score += 1
elif user_choice == "s":
if computer_choice == "s":
print("You choose scissors. Computer choose scissors. Tied")
elif computer_choice == "r":
print("You choose scissors. Computer choose rock. You lose")
computer_score += 1
elif computer_choice == "p":
print("You choose scissors. Computer choose paper. You win")
user_score += 1
print("")
print("Player wins: " + str(user_score))
print("Computer wins: " + str(computer_score))
print("")
user_choice = input("Do you want to play again? (y/n)")
if user_choice in ["Y", "y", "yes", "Yes", "YES"]:
pass
elif user_choice in ["N", "NO", "no", "No", "n"]:
break
else:
break
I liked this problem. Try posting it in github as well.
Here are some improvements that I made it.
import random
user_score = 0
computer_score = 0
def Choose_Option():
user_choice = input("Choose Rock, Paper, Scissors: ")
user_choice=user_choice.lower()
if user_choice in ["rock", "r"]:
user_choice = "r"
elif user_choice in ["paper", "p"]:
user_choice = "p"
elif user_choice in ["scissors", "s"]:
user_choice = "s"
else:
print("I don't understand. Try Again")
Choose_Option()
return user_choice
def Computer_Option():
computer_choice = random.randint(1,3)
dict_option = {
1:'r'
,2:'p'
,3:'s'
}
computer_choice=dict_option.get(computer_choice)
return computer_choice
while True:
print("")
user_choice = Choose_Option()
computer_choice = Computer_Option()
print("")
dict_rule={
'p':'r',
's':'p',
'r':'s'
}
dict_name={
'p':'Paper',
's':'Scissor',
'r':'Rock'
}
if user_choice==computer_choice:
print(f"You choose {dict_name[user_choice]}. Computer choose {dict_name[computer_choice]}. Tied")
elif dict_rule[user_choice] == computer_choice:
print(f"You choose {dict_name[user_choice]}. Computer choose {dict_name[computer_choice]}. You Win")
user_score += 1
else:
print(f"You choose {dict_name[user_choice]}. Computer choose {dict_name[computer_choice]}. You Lose")
computer_score += 1
print("")
print("Player wins: " + str(user_score))
print("Computer wins: " + str(computer_score))
print("")
user_choice = input("Do you want to play again? (y/n)")
user_choice=user_choice.lower()
if user_choice in ["y", "yes"]:
pass
elif user_choice in ["no","n"]:
break
else:
break
This question already has answers here:
While loop user input?
(2 answers)
Closed 2 years ago.
How do I get this program to automatically restart so the player doesn't have to run the code again to play again?
import random
game = ["Rock", "Paper", "Scissors"]
choice = random.choice(game)
question = input("Chose one (Rock/Paper/Scissors) : ")
if question == choice:
print(f'I chose {choice}')
print("We got the same thing! Try again")
elif question == "Rock" and choice == "Paper":
print(f'I chose {choice}')
print("I win!")
elif question == "Paper" and choice == "Rock":
print(f'I chose {choice}')
print("You win!")
elif question == "Rock" and choice == "Scissors":
print(f'I chose {choice}')
print("You win!")
elif question == "Scissors" and choice == "Rock":
print(f'I chose {choice}')
print("I win!")
elif question == "Scissors" and choice == "Paper":
print(f'I chose {choice}')
print("You win!")
elif question == "Paper" and choice == "Scissors":
print(f'I chose {choice}')
print("I win!")``
import random
game = ["Rock", "Paper", "Scissors"]
choice = random.choice(game)
while True:
question = input("Chose one (Rock/Paper/Scissors) : ")
if question == choice:
print(f'I chose {choice}')
print("We got the same thing! Try again")
elif question == "Rock" and choice == "Paper":
print(f'I chose {choice}')
print("I win!")
elif question == "Paper" and choice == "Rock":
print(f'I chose {choice}')
print("You win!")
elif question == "Rock" and choice == "Scissors":
print(f'I chose {choice}')
print("You win!")
elif question == "Scissors" and choice == "Rock":
print(f'I chose {choice}')
print("I win!")
elif question == "Scissors" and choice == "Paper":
print(f'I chose {choice}')
print("You win!")
elif question == "Paper" and choice == "Scissors":
print(f'I chose {choice}')
print("I win!")
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: