this is what I have so far I just don't know where to go with it. extremely new with code in general.
# Write a program that lets the user play the game of Rock, Paper, Scissors against the computer.
# The program should work as follows:
# When the program begins, a random number in the range of 1 through 3 is generated.
# If the number is 1, then the computer has chosen rock.
# If the number is 2, then the computer has chosen paper.
# If the number is 3, then the computer has chosen scissors. (Don’t display the computer’s choice yet.)
The user enters his or her choice of “rock,” “paper,” or “scissors” at the keyboard. The computer's choice is displayed. A winner is selected according to the following rules: If one player chooses rock and the other player chooses scissors, then rock wins. (Rock smashes scissors.) If one player chooses scissors and the other player chooses paper, then scissors wins. (Scissors cuts paper.) If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps rock.) If both players make the same choice, the game must be played again to determine the winner. import random
def comp_choice():
comp = random.randint(1, 3)
if comp == 1:
print("rock")
if comp == 2:
print("paper")
if comp == 3:
print("scissors")
player = input("Enter a choice rock, paper, or scissors: ")
comp_choice()
if comp_choice()==player:
#start over
if comp_choice()= rock and player=scissors
We can save the data in a dict so that it makes us easier. Create a dict which has numbers as keys and it's corresponding representative as values. Then use random module to get random choice of the computer. Then ask for input from user. Now use while loop. If computer choice and user input is same, then continue the loop so that it can ask again for input, if not, break the loop. Inside loop, use if and else statements to find who is the winner. Your code:
import random
c={1:"rock",2:"paper",3:"scissors"}
comp=random.randint(1,3)
print(c[comp])
while True:
player=int(input("Enter your input= "))
if player==comp:
continue
elif player not in range(1,4):
print("Invalid input, enter again")
elif (player==1 and comp==2) or (player==2 and comp==3) or (player==3 and comp==1):
print("Computer wins")
break
else:
print("You win")
break
if you want a more data driven desining
from random import randint
#create object numbers
ROCK, PAPER, SCISSORS= '1', '2', '3'
LOST = {
ROCK : SCISSORS, #rock wins against scissors
PAPER: ROCK, #paper wins against rock
SCISSORS: PAPER #scissors wins against paper
}
while True:
#generate the randon number and cast to string
computer_choose = str(randint(a=1,b=3))
question = f'{"-"*50}\nEnter a choice rock({ROCK}) paper({PAPER}) scissors({SCISSORS}): '
user_choose = input(question)
#check if user input is valid
if user_choose not in [ROCK,PAPER,SCISSORS]:
print('invalid answer, only numbers in range(1,2,3) are valid')
continue
elif user_choose == computer_choose:
print(f'its a draw, lets try again:\nComputer Choice:{computer_choose}')
continue
elif user_choose == LOST[computer_choose]:
print(f'you lost\nComputer Choice:{computer_choose}')
else:
print(f'you win\nComputer Choice:{computer_choose}')
break
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.