Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

数あてゲームの作成

Last updated at Posted at 2020-05-12

ヒットとブロー

  • ヒット 数と場所があってる
  • ブロー 場所は違うが数はあってる

プログラムの構造

  1. 4桁のランダムな数字を作る
  2. ユーザからの4桁の数字を読み込む
  3. ヒットとブローをチェック
  4. ヒットが4なら終了

1桁で作ってみる

import random

a = random.randint(0, 9)
print(a)

b = int(input("数を入れてね>"))
if a==b:
    print("当たり")
else:
    print("はずれ")

単純にinputしただけではbは数値となっていない.
intで整数にキャストする.

プログラムの構造を理解しやすいようにコメントを加えておく.
将来的にはmethodにするべき.
intのキャストはinput部でやるよりもcheck部でやるほうがいいかも.

# generate_numbers
import random
a = random.randint(0, 9)
print(a)

# input
b = int(input("数を入れてね>"))

# check
if a==b:
    print("当たり")
else:
    print("はずれ")

4桁のランダムな数字の生成

a = [random.randint(0, 9),
random.randint(0, 9),
random.randint(0, 9),
random.randint(0, 9)
]
print(a)
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?