[PYTHON] Rock-paper-scissors

script.py import utils

Please load the random module

import random

print ('Start rock-paper-scissors') player_name = input ('Please enter a name:') print ('What are you going to print? (0: Goo, 1: Choki, 2: Par)') player_hand = int (input ('Please enter numbers:'))

if utils.validate(player_hand): Use # randint to get a number from 0 to 2 and assign it to the variable computer_hand computer_hand = random.randint(0,2)

if player_name == '':
    utils.print_hand(player_hand)
else:
    utils.print_hand(player_hand, player_name)

utils.print_hand (computer_hand,'computer')

result = utils.judge(player_hand, computer_hand)

print ('The result was' + result +'was') else: print ('Enter the correct number')


utils.py def validate(hand): if hand < 0 or hand > 2: return False return True

def print_hand (hand, name ='guest'): hands = ['goo','choki','par'] print (name +'issued'+ hands [hand] +')

def judge(player, computer): if player == computer: return'draw' elif player == 0 and computer == 1: return'win' elif player == 1 and computer == 2: return'win' elif player == 2 and computer == 0: return'win' else: return'losing'

Recommended Posts

Rock-paper-scissors
Rock-paper-scissors in Ruby