I have tried to learn some coding before but didn't have enough time/ambition to do so. This year, I started learning Python again and I am taking it more seriously. I am determined to learn it and do something "useful" with it. One of the courses I am taking is An Introduction to Interactive Programming in Python on Coursera and the code below is for our first Miniproject (Rock-paper-scissors-lizard-Spock). Any feedback is appreciated.
# My first program
# Rock-paper-scissors-lizard-Spock
import random
import math
def name_to_number(name): # converts name to number
if name == "rock":
return "0"
elif name == "Spock":
return "1"
elif name == "paper":
return "2"
elif name == "lizard":
return "3"
elif name == "scissors":
return "4"
else:
print "Please choose one of these : rock, Spock, paper, lizard, scissors."
def number_to_name(number): # converts number to a name
if number == 0:
return "rock"
elif number == 1:
return "Spock"
elif number == 2:
return "paper"
elif number == 3:
return "lizard"
elif number == 4:
return "scissors"
def rpsls(player_choice):
print "New game!"
print ""
print "Player chooses " + player_choice
comp_number = int(random.randrange(0 , 5)) # computes random guess for comp_number
comp_choice = number_to_name(comp_number)
player_number = int(name_to_number(player_choice))
print "Computer chooses", comp_choice
num = int((comp_number - player_number) % 5) # computes difference of comp_number and player_number modulo five
if (num == 1) or (num == 2) or (num == - 1) or (num == - 2):
print "Computer wins!"
elif (num == 3) or (num == 4)or (num == - 3) or (num == - 4):
print "Player wins!"
elif (num == 0):
print "Player and computer tie!"
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")
# Rock-paper-scissors-lizard-Spock
import random
import math
def name_to_number(name): # converts name to number
if name == "rock":
return "0"
elif name == "Spock":
return "1"
elif name == "paper":
return "2"
elif name == "lizard":
return "3"
elif name == "scissors":
return "4"
else:
print "Please choose one of these : rock, Spock, paper, lizard, scissors."
def number_to_name(number): # converts number to a name
if number == 0:
return "rock"
elif number == 1:
return "Spock"
elif number == 2:
return "paper"
elif number == 3:
return "lizard"
elif number == 4:
return "scissors"
def rpsls(player_choice):
print "New game!"
print ""
print "Player chooses " + player_choice
comp_number = int(random.randrange(0 , 5)) # computes random guess for comp_number
comp_choice = number_to_name(comp_number)
player_number = int(name_to_number(player_choice))
print "Computer chooses", comp_choice
num = int((comp_number - player_number) % 5) # computes difference of comp_number and player_number modulo five
if (num == 1) or (num == 2) or (num == - 1) or (num == - 2):
print "Computer wins!"
elif (num == 3) or (num == 4)or (num == - 3) or (num == - 4):
print "Player wins!"
elif (num == 0):
print "Player and computer tie!"
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")
No comments:
Post a Comment