Can you show me 2 different ways in writting a Program: On python or pycharm You
ID: 3808391 • Letter: C
Question
Can you show me 2 different ways in writting a Program:
On python or pycharm
Your program should operate as follows It must let the user choose from among three options of rock, paper, and scissors It shall play an honest game of Rock Paper Scissors Display the choice the user made If the user input is not one of the allowed, then ask for the user to re- enter - input validation! Print out its choice of R/P/S Display who won the round, or tie Offer an option to continue until user wants to quit the game. Your program must be well commented
Explanation / Answer
Sol 1)
# Without using function
# import random number generator
from random import randint
#create the options available to play
game_opt = ["Rock", "Paper", "Scissors"]
#assign a random play to the computer
computer = game_opt [randint(0,2)]
#set player to False
user = False
# while loop
while user == False:
#set user to True
user = input("Rock, Paper, Scissors, or Quit?")
#Compare the choices of the computer and player
# and display output showing who wins
if user == computer:
print("It’s a Draw!")
elif user == "Rock":
if computer == "Paper":
print("You lose!, Computer selects ", computer)
else:
print("You win!")
elif user == "Paper":
if computer == "Scissors":
print("You lose!, Computer selects ", computer)
else:
print("You win!")
elif user == "Scissors":
if computer == "Rock":
print("You lose!, Computer selects ", computer)
else:
print("You win!")
elif user == "Quit"
sys.exit(0);
# Check for invalid option and display error
else:
print("That's not a valid option. Check your spelling!")
# user was set to True before the game
# We again set it to False so the loop continues
user = False
computer = game_opt [randint(0,2)]
Sol 2)
# The following program implementation is
# by using Functions
import random
import sys
# Select the correct option
def enterOpt():
print "Press R for Rock"
print "Press P for Paper"
print "Press S for Scissors"
print "Press Q to quit!"
sel_opt = raw_input("Enter your Option?").lower()
# Return the corresponding matching string
# based on selected option
if sel_opt == "r":
return "Rock"
if sel_opt == "p":
return "Paper"
if sel_opt == "s":
return "Scissors"
if sel_opt == "q":
sys.exit(0)
else:
enterOpt()
# Generates a Random Number for Computer
def pc_rand_num():
options = ["Rock","Paper","Scissors"]
rand_choice = random.randint(0,2)
return options[rand_choice]
# Compare User Choice with Computer Choice
# Also Display who Wins
def user_pc_comp(userChoice, PCChoice):
if userChoice == PCChoice:
return "It’s a Tie"
if userChoice == "Rock" and PCChoice == "Paper":
return "Computer Wins"
if userChoice == "Paper" and PCChoice == "Scissors":
return "Computer Wins"
if humanChoice == "Scissors" and computerChoice == "Rock":
return "Computer Wins"
else: return "You Win"
#print enterOpt()
# Loops until user selects Quit option
while True:
userChoice = enterOpt()
PCChoice = pc_rand_num()
print "You select", userChoice
print "The computer select", PCChoice
# Prints the final result
final_res = user_pc_comp(userChoice, PCChoice)
if final_res == "Draw":
print "Its a draw"
elif final_res == "Computer Wins":
print "Computer Wins"
else: print "Congratulation! You Win"
print " "
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.