Last active
September 18, 2016 01:30
-
-
Save u1and0/f576a56a0eec46df16740b78c67c6534 to your computer and use it in GitHub Desktop.
標準入力の受け取りtips@python ref: http://qiita.com/u1and0/items/66a72fef8bc0a7ce5eda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inp=input('なんか入力しろ>') | |
# [In]# なんか入力しろ>hogehoge | |
# [In]# inp | |
# [Out]# 'hogehoge' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while True: | |
inp=input('hogeって入力して>>') | |
if inp=='hoge':break | |
# [Out]# hogeって入力して>>foo | |
# [Out]# hogeって入力して>>hogehoge | |
# [Out]# hogeって入力して>>hoge | |
# [In]# inp | |
# [Out]# 'hoge' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[input() for _ in range(3)] | |
# [In]# a | |
# [In]# d | |
# [In]# v | |
# [Out]#: ['a', 'd', 'v'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a=input('コンマ区切りでなんか入力しろ >> ').split(',') | |
# [In] a=input('コンマ区切りでなんか入力しろ >> ').split(',') | |
# [Out] コンマ区りでなんか入力しろ >> 2016,2017 | |
# [In] a | |
# [Out] ['2016', '2017'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
[i.strip() for i in sys.stdin.readlines()] | |
# [In]# ho | |
# [In]# hsop | |
# [In]# hpoge | |
# [In]# ^Z | |
# [Out]# ['ho', 'hsop', 'hpoge'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while True: | |
inp = input('[Y]es/[N]o? >> ').lower() | |
if inp in dic: | |
inp = dic[inp] | |
break | |
print('Error! Input again.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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