2019年4月30日火曜日

生まれた月のカレンダーを表示させるプログラム その2

生まれた月のカレンダーを表示させるプログラム その2です。

生まれた年を入力して下さい:1981
生まれた月を入力して下さい:1
-----------------------------------
 あなたが生まれた月のカレンダー
-----------------------------------
    January 1981
Mo Tu We Th Fr Sa Su
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31



#coding:utf-8

import calendar

my_year = int(input('生まれた年を入力して下さい:'))
my_month = int(input('生まれた月を入力して下さい:'))

print('''-----------------------------------
 あなたが生まれた月のカレンダー
-----------------------------------''')
print(calendar.month(my_year,my_month))
   

メモ

・「’’’」で囲むと改行などもそのまま表記できる。

2019年4月29日月曜日

生まれた日のカレンダーを表示させるプログラム

生まれた日のカレンダーを表示させるプログラム

生まれた年を入力して下さい:1981
生まれた月を入力して下さい:1
-----------------------------------
あなたが生まれた日のカレンダー
    January 1981
Mo Tu We Th Fr Sa Su
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31


#coding:utf-8

import calendar

my_year = int(input('生まれた年を入力して下さい:'))
my_month = int(input('生まれた月を入力して下さい:'))

print('-----------------------------------')
print('あなたが生まれた日のカレンダー')
print(calendar.month(my_year,my_month))
   

メモ

・[input]で入力した数字は文字列になるので[int]で数値に変換する必要がある。
・カレンダーモジュールを[import]で読み込んでいる。

2019年4月27日土曜日

「Progate」のじゃんけんゲーム 改良その5

プログラムの学習サイト「Progate」の「Python学習コースⅢ」のじゃんけんゲームを改良したプログラム。

じゃんけんをはじめます
名前を入力してください:テストプレイヤー
--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:2

テストプレイヤーは 「チョキ」 を出しました
コンピューターは 「グー」 を出しました

結果は 「負け」 でした

0勝 1負 0引き分け です

--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:5

正しい数値を入力してください
--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:3

テストプレイヤーは 「パー」 を出しました
コンピューターは 「パー」 を出しました

結果は 「引き分け」 でした

0勝 1負 1引き分け です

--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:2

テストプレイヤーは 「チョキ」 を出しました
コンピューターは 「チョキ」 を出しました

結果は 「引き分け」 でした

0勝 1負 2引き分け です

--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:0

ゲームを終了します
>>> 


#coding:utf-8

def validate(hand):
    if hand == '1' or hand == '2' or hand == '3' :
        return True
    else:
        return False

def print_hand(hand, name='ゲスト'):
    hands = ['グー', 'チョキ', 'パー']
    print(name + 'は 「' + hands[hand] + '」 を出しました')

def judge(player, computer):
    if player == computer:
        return '引き分け'
    elif player == 0 and computer == 1:
        return '勝ち'
    elif player == 1 and computer == 2:
        return '勝ち'
    elif player == 2 and computer == 0:
        return '勝ち'
    else:
        return '負け'


import random

player_win = 0
computer_win = 0
draw = 0
            
print('じゃんけんをはじめます')

player_name = input('名前を入力してください:')

while True :
    
    print('--------------------------------------------------------------')
    print('')
    print('何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)')

    player_hand = input('数字で入力してください:')

    print('')

    if player_hand == '0':
        print('ゲームを終了します')
        break

    else:    
        if validate(player_hand):
            
            computer_hand = random.randint(0,2)

            
            
            if player_name == '':
                print_hand(int(player_hand)-1)
            else:
                print_hand(int(player_hand)-1, player_name)

            print_hand(computer_hand, 'コンピューター')
            
            result = judge(int(player_hand)-1, computer_hand)

            print('')
            print('結果は 「' + result + '」 でした')
        
            if result == '勝ち':
                player_win += 1
            elif result == '負け':
                computer_win += 1
            else :
                draw += 1
            
            print('')
            print(str(player_win) + '勝 ' + str(computer_win) + '負 ' + str(draw) + '引き分け です')
            print('')
            
        else:
            print('正しい数値を入力してください')


メモ

・勝ち負け引き分けの数を出るようにしました。

「Progate」のじゃんけんゲーム 改良その4

プログラムの学習サイト「Progate」の「Python学習コースⅢ」のじゃんけんゲームを改良したプログラム。

じゃんけんをはじめます
名前を入力してください:
--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:3

ゲストは 「パー」 を出しました
コンピューターは 「チョキ」 を出しました

結果は 「負け」 でした

--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:3

ゲストは 「パー」 を出しました
コンピューターは 「グー」 を出しました

結果は 「勝ち」 でした

--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:2

ゲストは 「チョキ」 を出しました
コンピューターは 「グー」 を出しました

結果は 「負け」 でした

--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:1

ゲストは 「グー」 を出しました
コンピューターは 「パー」 を出しました

結果は 「負け」 でした

--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:0

ゲームを終了します


#coding:utf-8

def validate(hand):
    if hand == '1' or hand == '2' or hand == '3' :
        return True
    else:
        return False

def print_hand(hand, name='ゲスト'):
    hands = ['グー', 'チョキ', 'パー']
    print(name + 'は 「' + hands[hand] + '」 を出しました')

def judge(player, computer):
    if player == computer:
        return '引き分け'
    elif player == 0 and computer == 1:
        return '勝ち'
    elif player == 1 and computer == 2:
        return '勝ち'
    elif player == 2 and computer == 0:
        return '勝ち'
    else:
        return '負け'


import random

print('じゃんけんをはじめます')

player_name = input('名前を入力してください:')

while True :
    
    print('--------------------------------------------------------------')
    print('')
    print('何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)')

    player_hand = input('数字で入力してください:')

    print('')

    if player_hand == '0':
        print('ゲームを終了します')
        break

    else:    
        if validate(player_hand):
            
            computer_hand = random.randint(0,2)
            
            if player_name == '':
                print_hand(int(player_hand)-1)
            else:
                print_hand(int(player_hand)-1, player_name)

            print_hand(computer_hand, 'コンピューター')
            
            result = judge(int(player_hand)-1, computer_hand)

            print('')
            print('結果は 「' + result + '」 でした')
            print('')
            
        else:
            print('正しい数値を入力してください')

メモ

・じゃんけんをするごとに終了していたのを繰り返しできるようにしました。
・「while True:」を使うことで無限ループになるので「break」で終了しています。
・読みにくかったので少し見やすくしました。

2019年4月26日金曜日

「Progate」のじゃんけんゲーム 改良その3

プログラムの学習サイト「Progate」の「Python学習コースⅢ」のじゃんけんゲームを改良したプログラム。

じゃんけんをはじめます
名前を入力してください:
何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:0
ゲームを終了します
>>> 

じゃんけんをはじめます
名前を入力してください:test
何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:1
testはグーを出しました
コンピューターはグーを出しました
結果は引き分けでした
>>> 


#coding:utf-8

def validate(hand):
    if hand == '1' or hand == '2' or hand == '3' :
        return True
    else:
        return False

def print_hand(hand, name='ゲスト'):
    hands = ['グー', 'チョキ', 'パー']
    print(name + 'は' + hands[hand] + 'を出しました')

def judge(player, computer):
    if player == computer:
        return '引き分け'
    elif player == 0 and computer == 1:
        return '勝ち'
    elif player == 1 and computer == 2:
        return '勝ち'
    elif player == 2 and computer == 0:
        return '勝ち'
    else:
        return '負け'


import random

print('じゃんけんをはじめます')

player_name = input('名前を入力してください:')

print('何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)')

player_hand = input('数字で入力してください:')

if player_hand == '0':
    print('ゲームを終了します')

else:    
    if validate(player_hand):
        
        computer_hand = random.randint(0,2)
        
        if player_name == '':
            print_hand(int(player_hand)-1)
        else:
            print_hand(int(player_hand)-1, player_name)

        print_hand(computer_hand, 'コンピューター')
        
        result = judge(int(player_hand)-1, computer_hand)

        print('結果は' + result + 'でした')
        
    else:
        print('正しい数値を入力してください')  

メモ

・「0」を選ぶとゲームが終了するように改良

「Progate」のじゃんけんゲーム 改良その2 

プログラムの学習サイト「Progate」の「Python学習コースⅢ」のじゃんけんゲームを改良したプログラム。

じゃんけんをはじめます
名前を入力してください:
何を出しますか?(1: グー, 2: チョキ, 3: パー)
数字で入力してください:3
ゲストはパーを出しました
コンピューターはグーを出しました
結果は勝ちでした
>>> 

じゃんけんをはじめます
名前を入力してください:test
何を出しますか?(1: グー, 2: チョキ, 3: パー)
数字で入力してください:2.5
正しい数値を入力してください
>>> 

じゃんけんをはじめます
名前を入力してください:test
何を出しますか?(1: グー, 2: チョキ, 3: パー)
数字で入力してください:test
正しい数値を入力してください
>>> 

じゃんけんをはじめます
名前を入力してください:test
何を出しますか?(1: グー, 2: チョキ, 3: パー)
数字で入力してください:2
testはチョキを出しました
コンピューターはチョキを出しました
結果は引き分けでした
>>> 


#coding:utf-8

def validate(hand):
    if hand == '1' or hand == '2' or hand == '3' :
        return True
    else:
        return False

def print_hand(hand, name='ゲスト'):
    hands = ['グー', 'チョキ', 'パー']
    print(name + 'は' + hands[hand] + 'を出しました')

def judge(player, computer):
    if player == computer:
        return '引き分け'
    elif player == 0 and computer == 1:
        return '勝ち'
    elif player == 1 and computer == 2:
        return '勝ち'
    elif player == 2 and computer == 0:
        return '勝ち'
    else:
        return '負け'


import random

print('じゃんけんをはじめます')

player_name = input('名前を入力してください:')

print('何を出しますか?(1: グー, 2: チョキ, 3: パー)')

player_hand = input('数字で入力してください:')

if validate(player_hand):
    
    computer_hand = random.randint(0,2)
    
    if player_name == '':
        print_hand(int(player_hand)-1)
    else:
        print_hand(int(player_hand)-1, player_name)

    print_hand(computer_hand, 'コンピューター')
    
    result = judge(int(player_hand)-1, computer_hand)

    print('結果は' + result + 'でした')
    
else:
    print('正しい数値を入力してください')


メモ

・「何を出しますか?(0: グー, 1: チョキ, 2: パー)」の表記を「何を出しますか?(1: グー, 2: チョキ, 3: パー)」に変更。
こちらの方が馴染みやすいかな。

2019年4月25日木曜日

「Progate」のじゃんけんゲームの改良プログラム

プログラムの学習サイト「Progate」の「Python学習コースⅢ」のじゃんけんゲームを改良したプログラム。

じゃんけんをはじめます
名前を入力してください:テストプレイヤー
何を出しますか?(0: グー, 1: チョキ, 2: パー)
数字で入力してください:1.5
正しい数値を入力してください
>>> 

じゃんけんをはじめます
名前を入力してください:テストプレイヤー
何を出しますか?(0: グー, 1: チョキ, 2: パー)
数字で入力してください:テスト
正しい数値を入力してください
>>> 

じゃんけんをはじめます
名前を入力してください:テストプレイヤー
何を出しますか?(0: グー, 1: チョキ, 2: パー)
数字で入力してください:2
テストプレイヤーはパーを出しました
コンピューターはグーを出しました
結果は勝ちでした


#coding:utf-8

def validate(hand):
    if hand == '0' or hand == '1' or hand == '2' :
        return True
    else:
        return False

def print_hand(hand, name='ゲスト'):
    hands = ['グー', 'チョキ', 'パー']
    print(name + 'は' + hands[hand] + 'を出しました')

def judge(player, computer):
    if player == computer:
        return '引き分け'
    elif player == 0 and computer == 1:
        return '勝ち'
    elif player == 1 and computer == 2:
        return '勝ち'
    elif player == 2 and computer == 0:
        return '勝ち'
    else:
        return '負け'


import random

print('じゃんけんをはじめます')

player_name = input('名前を入力してください:')

print('何を出しますか?(0: グー, 1: チョキ, 2: パー)')

player_hand = input('数字で入力してください:')

if validate(player_hand):
    
    computer_hand = random.randint(0,2)
    
    if player_name == '':
        print_hand(int(player_hand))
    else:
        print_hand(int(player_hand), player_name)

    print_hand(computer_hand, 'コンピューター')
    
    result = judge(int(player_hand), computer_hand)
    print('結果は' + result + 'でした')
else:
    print('正しい数値を入力してください')


メモ

・じゃんけんの数字を入力する時に、「0」「1」「2」以外の数字をいれた場合エラーになるのを改良しました。
「1.5」などの少数、「テスト」などの文字でもエラーにならないようにしました。

・入力した文字列をチェックするようにしています。

はじめとおわりの数字を指定して2の倍数、3の倍数、6の倍数かを調べるプログラム

はじめとおわりの数字を指定して2の倍数、3の倍数、6の倍数かを調べるプログラムです。

はじめの数字を入力して下さい。:2
おわりの数字を入力して下さい。:12
-------------------------------------------
2は2の倍数です
3は3の倍数です
4は2の倍数です
5は特になし
6は6の倍数です
7は特になし
8は2の倍数です
9は3の倍数です
10は2の倍数です
11は特になし
12は6の倍数です
-------------------------------------------


 c:
        print('-------------------------------------------')
        break

    if b % 2 == 0 and b % 3 == 0:
        print(str(b) + 'は6の倍数です')
    elif b % 2 == 0:
        print(str(b) + 'は2の倍数です')
    elif b % 3 == 0:
        print(str(b) + 'は3の倍数です')
    else :
        print(str(b) + 'は特になし')
    b += 1


メモ

・はじめとおわりを指定できるようにしました。

2019年4月24日水曜日

自分の好きな数字から100までの数字を2の倍数、3の倍数、6の倍数かを調べるプログラム

自分の好きな数字から100までの数字を2の倍数、3の倍数、6の倍数かを調べるプログラムです。

はじめの数字を入力して下さい。:4
4は2の倍数です
5は特になし
6は6の倍数です
7は特になし
8は2の倍数です
9は3の倍数です
10は2の倍数です
11は特になし
12は6の倍数です
13は特になし
14は2の倍数です
:
:
:
96は6の倍数です
97は特になし
98は2の倍数です
99は3の倍数です
100は2の倍数です


#coding:utf-8


b = int(input('はじめの数字を入力して下さい。:'))
    
while True:
    
    if b > 100:
        break
    
    if b % 2 == 0 and b % 3 == 0:
        print(str(b) + 'は6の倍数です')
    elif b % 2 == 0:
        print(str(b) + 'は2の倍数です')
    elif b % 3 == 0:
        print(str(b) + 'は3の倍数です')
    else :
        print(str(b) + 'は特になし')
    b += 1

メモ

「input」を使ってはじまりの数字を決めれるように変更。
表示の仕方も変えました。
「b = b +1」を同じ意味の「b += 1」に変更。

1から100までの数字を2の倍数、3の倍数、6の倍数かを調べるプログラム

1から100までの数字を2の倍数、3の倍数、6の倍数かを調べるプログラムです。

1
特になし
2
は2の倍数です
3
は3の倍数です
4
は2の倍数です
5
特になし
6
は6の倍数です
:
:
:
98
は2の倍数です
99
は3の倍数です
100
は2の倍数です


#coding:utf-8


b = 0
    
while True:
    b = b +1
    if b > 100:
        break
    print(b)
    if b % 2 ==0 and b % 3 ==0:
        print("は6の倍数です")
    elif b % 2 ==0:
        print("は2の倍数です")
    elif b % 3 ==0:
        print("は3の倍数です")
    else :
        print("特になし")    

メモ

「%」は割り算をしたあまりの数。あまりが0ということは割った数の倍数ということになる。