2018年2月3日土曜日

開発環境

Teach Your Kids to Code: A Parent-Friendly Guide to Python Programming (Bryson Payne(著)、No Starch Press)のChapter 6.(Random Fun and Games: Go Ahead, Take a Chance!)、PROGRAMMING CHALLENGES、#3: WARを取り組んでみる。

#3: WAR

コード(Emacs)

Python 3

#!/usr/bin/env python3
import random

suits = ['lubs', 'diamonds', 'hearts', 'spades']
faces = ['two', 'three', 'four', 'five', 'six', 'seven',
         'eight', 'nine', 'ten', 'jack', 'queen', 'king', 'ace']
keep_going = True
my_won = 0
your_won = 0
hands = 0
ties = 0
while keep_going and hands < 26:
    hands += 1
    my_face = random.choice(faces)
    my_suit = random.choice(suits)
    your_face = random.choice(faces)
    your_suit = random.choice(suits)
    print(f'I have the {my_face} of {my_suit}')
    print(f'You have the {your_face} of {your_suit}')
    if faces.index(my_face) > faces.index(your_face):
        print('I win!')
        my_won += 1 + ties
        ties = 0
    elif faces.index(my_face) < faces.index(your_face):
        print('You win!')
        your_won += 1 + ties
        ties = 0
    else:
        print("It's a tie!")
        ties += 1
    answer = input('Hit [Enter] to keep going, any key to exit: ')
    keep_going = (answer == '')

print(f'end(my_won: {my_won}, your_won: {your_won})')
if my_won > your_won:
    print('I win!')
elif my_won < your_won:
    print('You win!')
else:
    print("It's a tie!")

入出力結果(Terminal, Jupyter(IPython))

$ ./sample3.py
I have the six of lubs
You have the three of hearts
I win!
Hit [Enter] to keep going, any key to exit: 
I have the ace of spades
You have the six of hearts
I win!
Hit [Enter] to keep going, any key to exit: 
I have the three of spades
You have the ten of hearts
You win!
Hit [Enter] to keep going, any key to exit: 
I have the ace of diamonds
You have the five of hearts
I win!
Hit [Enter] to keep going, any key to exit: 
I have the seven of hearts
You have the ace of diamonds
You win!
Hit [Enter] to keep going, any key to exit: 
I have the jack of lubs
You have the queen of spades
You win!
Hit [Enter] to keep going, any key to exit: 
I have the queen of spades
You have the six of spades
I win!
Hit [Enter] to keep going, any key to exit: 
I have the five of hearts
You have the jack of spades
You win!
Hit [Enter] to keep going, any key to exit: 
I have the ten of diamonds
You have the king of lubs
You win!
Hit [Enter] to keep going, any key to exit: 
I have the queen of spades
You have the nine of spades
I win!
Hit [Enter] to keep going, any key to exit: 
I have the ace of spades
You have the queen of spades
I win!
Hit [Enter] to keep going, any key to exit: 
I have the king of spades
You have the three of spades
I win!
Hit [Enter] to keep going, any key to exit: 
I have the five of diamonds
You have the four of diamonds
I win!
Hit [Enter] to keep going, any key to exit: 
I have the six of spades
You have the two of diamonds
I win!
Hit [Enter] to keep going, any key to exit: 
I have the three of hearts
You have the eight of diamonds
You win!
Hit [Enter] to keep going, any key to exit: 
I have the two of lubs
You have the ten of hearts
You win!
Hit [Enter] to keep going, any key to exit: 
I have the jack of lubs
You have the seven of diamonds
I win!
Hit [Enter] to keep going, any key to exit: 
I have the six of spades
You have the six of spades
It's a tie!
Hit [Enter] to keep going, any key to exit: 
I have the ace of lubs
You have the nine of lubs
I win!
Hit [Enter] to keep going, any key to exit: 
I have the five of spades
You have the ace of lubs
You win!
Hit [Enter] to keep going, any key to exit: 
I have the queen of lubs
You have the king of hearts
You win!
Hit [Enter] to keep going, any key to exit: 
I have the five of lubs
You have the ace of lubs
You win!
Hit [Enter] to keep going, any key to exit: 
I have the king of diamonds
You have the six of diamonds
I win!
Hit [Enter] to keep going, any key to exit: 
I have the king of lubs
You have the ace of spades
You win!
Hit [Enter] to keep going, any key to exit: 
I have the two of hearts
You have the four of diamonds
You win!
Hit [Enter] to keep going, any key to exit: 
I have the two of diamonds
You have the seven of lubs
You win!
Hit [Enter] to keep going, any key to exit: 
end(my_won: 13, your_won: 13)
It's a tie!
$ ./sample3.py
I have the nine of hearts
You have the seven of spades
I win!
Hit [Enter] to keep going, any key to exit: 
I have the two of hearts
You have the two of diamonds
It's a tie!
Hit [Enter] to keep going, any key to exit: 
I have the four of spades
You have the jack of diamonds
You win!
Hit [Enter] to keep going, any key to exit: 
I have the five of hearts
You have the two of hearts
I win!
Hit [Enter] to keep going, any key to exit: 
I have the ace of hearts
You have the two of hearts
I win!
Hit [Enter] to keep going, any key to exit: 
I have the six of lubs
You have the five of spades
I win!
Hit [Enter] to keep going, any key to exit: 
I have the nine of lubs
You have the jack of hearts
You win!
Hit [Enter] to keep going, any key to exit: 
I have the two of hearts
You have the jack of spades
You win!
Hit [Enter] to keep going, any key to exit: 
I have the four of lubs
You have the king of lubs
You win!
Hit [Enter] to keep going, any key to exit: 
I have the ace of hearts
You have the ace of lubs
It's a tie!
Hit [Enter] to keep going, any key to exit: 
I have the four of diamonds
You have the five of hearts
You win!
Hit [Enter] to keep going, any key to exit: 
I have the seven of diamonds
You have the eight of diamonds
You win!
Hit [Enter] to keep going, any key to exit: 
I have the eight of spades
You have the king of hearts
You win!
Hit [Enter] to keep going, any key to exit: 
I have the four of diamonds
You have the three of lubs
I win!
Hit [Enter] to keep going, any key to exit: 
I have the ten of diamonds
You have the ten of hearts
It's a tie!
Hit [Enter] to keep going, any key to exit: 
I have the six of lubs
You have the five of lubs
I win!
Hit [Enter] to keep going, any key to exit: 
I have the nine of diamonds
You have the queen of lubs
You win!
Hit [Enter] to keep going, any key to exit: 
I have the jack of hearts
You have the king of spades
You win!
Hit [Enter] to keep going, any key to exit: 
I have the two of spades
You have the jack of spades
You win!
Hit [Enter] to keep going, any key to exit: 
I have the five of diamonds
You have the queen of spades
You win!
Hit [Enter] to keep going, any key to exit: 
I have the king of spades
You have the three of lubs
I win!
Hit [Enter] to keep going, any key to exit: 
I have the queen of lubs
You have the ace of diamonds
You win!
Hit [Enter] to keep going, any key to exit: 
I have the ace of spades
You have the seven of hearts
I win!
Hit [Enter] to keep going, any key to exit: 
I have the six of lubs
You have the jack of hearts
You win!
Hit [Enter] to keep going, any key to exit: 
I have the queen of hearts
You have the king of hearts
You win!
Hit [Enter] to keep going, any key to exit: 
I have the six of diamonds
You have the king of diamonds
You win!
Hit [Enter] to keep going, any key to exit: 
end(my_won: 9, your_won: 17)
You win!
$ ./sample3.py
I have the six of spades
You have the two of spades
I win!
Hit [Enter] to keep going, any key to exit: 
I have the ace of lubs
You have the three of hearts
I win!
Hit [Enter] to keep going, any key to exit: 
I have the ace of lubs
You have the nine of lubs
I win!
Hit [Enter] to keep going, any key to exit: 
I have the six of lubs
You have the nine of diamonds
You win!
Hit [Enter] to keep going, any key to exit: 
I have the six of diamonds
You have the ten of spades
You win!
Hit [Enter] to keep going, any key to exit: q
end(my_won: 3, your_won: 2)
I win!
$ 

0 コメント:

コメントを投稿