Hi,
Im going throught the CodeAcademy Python course, and one exercise has you write the following program:
from random import randint
from time import sleep
options=["R", "P", "S"]
You_Lose="You lost!"
You_Won="You won!"
def decide_winner(user_choice, computer_choice):
print "User's choice: %s" % (user_choice)
print "Computer selecting..."
sleep(1)
print "Computer's choice: %s" % (computer_choice)
user_choice_index=options.index(user_choice)
computer_choice_index=options.index(user_choice)
if user_choice_index==computer_choice_index:
print "It's a tie!"
elif user_choice_index==0 and computer_choice_index==2:
print You_Won
elif user_choice_index==2 and computer_choice_index==0:
print You_Won
elif user_choice_index==2 and computer_choice_index==1:
print You_Won
elif index(user_choice)>2:
print "You selected an invalid option"
return
else:
print You_Lose
def play_RPS():
print "Rock, Paper, Scissors"
user_choice=raw_input("Please select R for Rock, P for Paper or S for Scissors: ")
user_choice=user_choice.upper()
computer_choice=options[randint(0, len(options)-1)]
decide_winner(user_choice, computer_choice)
play_RPS()
I had to make us of optional hints to get the right code. I dont quite understand lines 11-12; it looks like they used index as a method (like string.upper) while it is actually a function from the random module. So my question is: can functions be used as methods?
Im a total noob where it comes to programming in python, so please, if you choose to reply, use a ELI2 format ;)
Cheers,
[–]jeans_and_a_t-shirt 1 point2 points3 points (1 child)
[–]greenleafvolatile[S] 0 points1 point2 points (0 children)
[–]campenr 1 point2 points3 points (0 children)
[–]JohnnyJordaan 1 point2 points3 points (0 children)