2019年5月16日木曜日

じゃんけんゲームのゲーム画面のプログラム

じゃんけんゲームのゲーム画面のプログラムです。



#coding:utf-8

import tkinter as tk

root = tk.Tk()
root.geometry('500x380')
root.title('じゃんけんゲーム')

label1 = tk.Label(root,text = 'じゃんけんをはじめます。', font = ('Courier',14))
label2 = tk.Label(root,text = '名前を入力して下さい。', font = ('Courier',14))
editbox1 = tk.Entry(width = 15, font = ('Courier',14))
label3 =tk.Label(root,text = '何を出しますか?', font = ('Courier',14))
button1 = tk.Button(root,text = ' グー  ', font = ('Courier',14))
button2 = tk.Button(root,text = ' チョキ ', font = ('Courier',14))
button3 = tk.Button(root,text = ' パー  ', font = ('Courier',14))
button4 = tk.Button(root,text = 'ゲーム終了', font = ('Courier',14))

label1.place(x = 20, y = 20)
label2.place(x = 20, y = 50)
editbox1.place(x = 40, y = 90)
label3.place(x = 20, y = 150)
button1.place(x = 20, y = 190)
button2.place(x = 150, y = 190)
button3.place(x = 280, y = 190)
button4.place(x = 320, y = 300)

root.mainloop()


メモ

・まだゲームは出来ません。
・画面表示をこんな感じにしたいなと思っています。
・「いちばんやさしいPython入門教室」という本を参考にしています。

2019年5月13日月曜日

文字数をチェックするプログラム

文字数をチェックするプログラムです。

4文字を入力して下さい:123
4文字ではありません。やり直して下さい
4文字を入力して下さい:te3
4文字ではありません。やり直して下さい
4文字を入力して下さい:ab12
入力したのはab12で4文字です
>>> 


#coding:utf-8

a = False
while a == False:
    b = input('4文字を入力して下さい:')
    if len(b) != 4:
        print('4文字ではありません。やり直して下さい')
    else:
        a = True
print('入力したのは'+ b + 'で4文字です')  

メモ

・「len」は文字数やリストの数をカウント出来ます。

2019年5月9日木曜日

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

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

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

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

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

結果は 「勝ち」 でした

1勝 0負 0引き分け です

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

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

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

結果は 「負け」 でした

1勝 1負 0引き分け です

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

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

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

結果は 「負け」 でした

1勝 2負 0引き分け です

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

何を出しますか?(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.choice((0,1,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('正しい数値を入力してください')


メモ

・ランダムで数字を作る部分を「choice」をつかったバージョンにした。

2019年5月8日水曜日

任意の数から数までの和を求めるプログラム その2。

任意の数から数までの和を求めるプログラム その2。

始まりの数字を入力して下さい:2
終わりの数字を入力して下さい:10
2 合計: 2
3 合計: 5
4 合計: 9
5 合計: 14
6 合計: 20
7 合計: 27
8 合計: 35
9 合計: 44
10 合計: 54


#coding:utf-8

def total(e,f):
   b = e
   for a in range(e,f+1):
      print(str(a) + ' 合計: ' + str(b))
      b += (a+1)

c = int(input('始まりの数字を入力して下さい:'))
d = int(input('終わりの数字を入力して下さい:'))

total(c,d)

メモ

・以前のプログラムを無駄に関数を使って作り直してみました。

2019年5月4日土曜日

任意の数から数までの和を求めるプログラム

任意の数から数までの和を求めるプログラム。

始まりの数字を入力して下さい:2
終わりの数字を入力して下さい:5
2 合計: 2
3 合計: 5
4 合計: 9
5 合計: 14


#coding:utf-8

c = int(input('始まりの数字を入力して下さい:'))
d = int(input('終わりの数字を入力して下さい:'))

b = c

for a in range(c,d+1):
   print(str(a) + ' 合計: ' + str(b))
   b += (a+1)

メモ

・変数が増えるとややこしくなるね。

2019年5月3日金曜日

任意の数字までの合計を出すプログラム

任意の数字までの合計を出すプログラム

終わりの数字を入力して下さい:12
1 合計: 1
2 合計: 3
3 合計: 6
4 合計: 10
5 合計: 15
6 合計: 21
7 合計: 28
8 合計: 36
9 合計: 45
10 合計: 55
11 合計: 66
12 合計: 78
>>> 


#coding:utf-8

b=0

c = int(input('終わりの数字を入力して下さい:'))

for a in range(c):
   b += (a+1)
   print(str(a+1) + ' 合計: ' + str(b))

メモ

・好きな数字までの和を出すように変更

2019年5月2日木曜日

数字の1から5までの数字を足すプログラム

数字の1から5までの数字を足すプログラムです。

1
3
6
10
15
合計:15


#coding:utf-8

b=0
for a in range(5):
   b += (a+1)
   print(b)
print('合計:' + str(b))

メモ

・「range(開始する値(以上になる),終了する値(未満になる))」で連続した数を扱うことができる。
・開始する値が0の場合は省略が可能。
・「for文」と混ぜると繰り返す回数を指定できる。
・終了する値が未満なのに注意