Skip to content

Instantly share code, notes, and snippets.

@u1and0
Last active September 18, 2016 01:30
Show Gist options
  • Save u1and0/f576a56a0eec46df16740b78c67c6534 to your computer and use it in GitHub Desktop.
Save u1and0/f576a56a0eec46df16740b78c67c6534 to your computer and use it in GitHub Desktop.
標準入力の受け取りtips@python ref: http://qiita.com/u1and0/items/66a72fef8bc0a7ce5eda
inp=input('なんか入力しろ>')
# [In]# なんか入力しろ>hogehoge
# [In]# inp
# [Out]# 'hogehoge'
while True:
inp=input('hogeって入力して>>')
if inp=='hoge':break
# [Out]# hogeって入力して>>foo
# [Out]# hogeって入力して>>hogehoge
# [Out]# hogeって入力して>>hoge
# [In]# inp
# [Out]# 'hoge'
[input() for _ in range(3)]
# [In]# a
# [In]# d
# [In]# v
# [Out]#: ['a', 'd', 'v']
a=input('コンマ区切りでなんか入力しろ >> ').split(',')
# [In] a=input('コンマ区切りでなんか入力しろ >> ').split(',')
# [Out] コンマ区りでなんか入力しろ >> 2016,2017
# [In] a
# [Out] ['2016', '2017']
import sys
inp=sys.stdin.readline().strip()
# [In]# inp=sys.stdin.readline()
# [In]# pp
# [In]# inp
# [Out]# 'pp\n'
# [In]# inp=sys.stdin.readline().strip()
# [In]# inp
# [Out]# 'pp'
import sys
[i.strip() for i in sys.stdin.readlines()]
# [In]# ho
# [In]# hsop
# [In]# hpoge
# [In]# ^Z
# [Out]# ['ho', 'hsop', 'hpoge']
while True:
try:
inp=input('6桁の数字を入力してや >> ')
if inp.isdigit() and len(inp)==6:
break
except:
pass
print('入力まちごうとるで')
# [Out] # 6桁の数字を入力してや >> 3
# [Out]#入力まちごうとるで
# [Out] # 6桁の数字を入力してや >> 45
# [Out]#入力まちごうとるで
# [Out] # 6桁の数字を入力してや >> 666666
# [In] # inp
# [Out]# 666666
inp=input('y/n? >> ')=='y'
# In# inp=True if input('y/n? >> ')=='y' else False
y/n? >> y
# In# inp
# Out# True
# In# inp=True if input('y/n? >> ')=='y' else False
y/n? >> n
# In# inp
# Out# False
# In# inp=True if input('y/n? >> ')=='y' else False
y/n? >> yes
# In# inp
# Out# False
# In# inp=True if input('y/n? >> ')=='y' else False
y/n? >> t
# In# inp
# Out# False
dic={'y':True,'yes':True,'n':False,'no':False}
dic[input('[Y]es/[N]o? >> ').lower()]
# [In]# dic[input('[Y]es/[N]o? >> ').lower()]
# [Out]# [Y]es/[N]o? >> y
# [Out]# True
# [In]# dic[input('[Y]es/[N]o? >> ').lower()]
# [Out]# [Y]es/[N]o? >> NO
# [Out]# False
# [In]# dic[input('[Y]es/[N]o? >> ').lower()]
# [Out]# [Y]es/[N]o? >> ye
# [Out]# ---------------------------------------------------------------------------
# KeyError Traceback (most recent call last)
# <ipython-input-34-67f9611165bf> in <module>()
# 1 dic={'y':True,'yes':True,'n':False,'no':False}
# ----> 2 dic[input('[Y]es/[N]o? >> ').lower()]
#
# KeyError: 'ye'
dic={'y':True,'yes':True,'n':False,'no':False}
while True:
try:
inp=dic[input('[Y]es/[N]o? >> ').lower()]
break
except:
pass
print('Error! Input again.')
# [Out]# [Y]es/[N]o? >> ye
# [Out]# Error! Input again.
# [Out]# [Y]es/[N]o? >> yes
# [In]# inp
# [Out]# True
while True:
inp = input('[Y]es/[N]o? >> ').lower()
if inp in dic:
inp = dic[inp]
break
print('Error! Input again.')
while True:
inp = input('[Y]es/[N]o? >> ').lower()
if inp in ('y', 'yes', 'n', 'no'):
inp = inp.startswith('y') # inp[0] == 'y'と同義
# string.startwithは最初の文字列を調べる
break
print('Error! Input again.')
while True:
inp = dic.get(input('[Y]es/[N]o? >> ').lower(), -1) #inpのデフォルト値を-1(←bool値でなければなんでもよい)にしておく
if type(inp) is bool:
break
print('Error! Input again.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment