Rock-Scissors-Paper
#rsp.py while True: # get computer's choice computer = random.randint(1, 3) if computer == 1: computer = 'R' elif computer == 2: computer = 'S' elif computer == 3: computer = 'P' # get human's choice print("Rock, Scissors, Paper, Quit:", end=' ') human = input()[0].upper() if human == 'Q': break # show result print("computer:%s, human:%s: " % (computer, human), end=' ') if computer == 'R' and human == 'R': print("It's a tie.") elif computer == 'R' and human == 'S': print("Computer wins.") cWins += 1 elif computer == 'R' and human == 'P': print("Human wins.") hWins += 1 elif computer == 'S' and human == 'R': print("Human wins.") hWins += 1 elif computer == 'S' and human == 'S': print("It's a tie.") elif computer == 'S' and human == 'P': print("Computer wins.") cWins += 1 elif computer == 'P' and human == 'R': print("Computer wins.") cWins += 1 elif computer == 'P' and human == 'S': print("Human wins.") hWins += 1 elif computer == 'P' and human == 'P': print("It's a tie.") print("Computer wins:%d, Human wins:%d!" % (cWins, hWins))
# rspf.py import random def getComputerChoice(): computer = random.randint(1, 3) if computer == 1: return 'R' elif computer == 2: return 'S' elif computer == 3: return 'P' def getHumanChoice(): print("Rock, Scissors, Paper, Quit:", end=' ') return input()[0].upper() def getResult(me, you): if me == you: return "Tie" elif (me == 'R' and you == 'S') \ or (me == 'S' and you == 'P') \ or (me == 'P' and you == 'R'): return "Win" else: return "Lose" def main(): cWins, hWins = 0, 0 while True: computer = getComputerChoice() human = getHumanChoice() if human == 'Q': break result = getResult(human, computer) print("computer:%s, human:%s: You %s"\ % (computer, human, result)) print("Computer wins:%d, Human wins:%d!"\ % (cWins, hWins)) if __name__ == "__main__": main()
#functions.py import random def getComputerChoice(): computer = random.randint(1, 3) if computer == 1: return 'R' elif computer == 2: return 'S' elif computer == 3: return 'P' def getHumanChoice(): print("Rock, Scissors, Paper, Quit:", end=' ') return input()[0].upper() def getResult(me, you): if me == you: return "Tie" elif (me == 'R' and you == 'S') \ or (me == 'S' and you == 'P') \ or (me == 'P' and you == 'R'): return "Win" else: return "Lose" # unit testing if __name__ == "__main__": print(getResult("R", "R"))
# rspfm.py import functions def main(): cWins, hWins = 0, 0 while True: computer = functions.getComputerChoice() human = functions.getHumanChoice() if human == 'Q': break result = functions.getResult(human, computer) print("computer:%s, human:%s: You %s"\ % (computer, human, result)) print("Computer wins:%d, Human wins:%d!"\ % (cWins, hWins)) if __name__ == "__main__": main()
# email parser def isValid(c): if ('0' <= c and c <= '9'): return True if ('A' <= c and c <= 'Z'): return True if ('a' <= c and c <= 'z'): return True if ('.' == c or c == '-'): return True return False line = "< rburns@dvc.edu tina@rdb3.com " for i in range(len(line)): if (line[i] == '@'): hasDot = False for s in range(i - 1, -1, -1): if not isValid(line[s]): break if not isValid(line[s]): s += 1 for e in range(i + 1, len(line)): if not isValid(line[e]): break if line[e] == '.': hasDot = True if isValid(line[e]): e += 1 print(i, s, e, hasDot, line[s:e])