練習問題/解答例/Hit and Blow/Python
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[問題文>練習問題#ffeaab60]]
from random import shuffle
N = 4
a = range(1, 10)
shuffle(a)
a = ''.join(map(str,a[:N]))
x = ''
while x != a:
hit, blow = 0, 0
x = raw_input('Input {0} digit number: '.format(N))
for i, e in enumerate(a):
if x[i] == e:
hit += 1
elif e in x:
blow += 1
print '{0} hit, {1} blow'.format(hit, blow)
#初心者用
# coding:utf-8
import random
print "数当てゲーム Hit & Blow"
a = []
b = []
r = random.randint(1, 9)
n = int(raw_input("遊びたい桁を入力してください(1桁以上)...
s = 0
while len(a) != n:
if r not in a:
a.append(r)
r = random.randint(1, 9)
while s != n:
while len(b) != n:
x = int(raw_input("予想する数字を大きい桁から1つずつ...
if x not in b:
b.append(x)
print b
else:
print "同じ数は入力できません"
c = set(a)
d = set(b)
c &= d
s = 0
for i in range(n):
if a[i] == b[i]:
s += 1
print str(len(c) - s) + "Blow"
print str(s) + "Hit"
b = []
print "おめでとう!"
終了行:
[[問題文>練習問題#ffeaab60]]
from random import shuffle
N = 4
a = range(1, 10)
shuffle(a)
a = ''.join(map(str,a[:N]))
x = ''
while x != a:
hit, blow = 0, 0
x = raw_input('Input {0} digit number: '.format(N))
for i, e in enumerate(a):
if x[i] == e:
hit += 1
elif e in x:
blow += 1
print '{0} hit, {1} blow'.format(hit, blow)
#初心者用
# coding:utf-8
import random
print "数当てゲーム Hit & Blow"
a = []
b = []
r = random.randint(1, 9)
n = int(raw_input("遊びたい桁を入力してください(1桁以上)...
s = 0
while len(a) != n:
if r not in a:
a.append(r)
r = random.randint(1, 9)
while s != n:
while len(b) != n:
x = int(raw_input("予想する数字を大きい桁から1つずつ...
if x not in b:
b.append(x)
print b
else:
print "同じ数は入力できません"
c = set(a)
d = set(b)
c &= d
s = 0
for i in range(n):
if a[i] == b[i]:
s += 1
print str(len(c) - s) + "Blow"
print str(s) + "Hit"
b = []
print "おめでとう!"
ページ名: