Created
December 16, 2015 22:38
-
-
Save naosim/a193480afea32389e480 to your computer and use it in GitHub Desktop.
pythonで標準入力
This file contains hidden or 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
from readutil import * | |
inputData = readInput([ | |
InputData('name'), | |
InputData('age', 'age(number)?') | |
]) |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
import sys | |
class InputData: | |
def __init__(self, key, message = None): | |
self.key = key | |
self.message = message if message else ("%s?" % key) | |
def startInput(self): | |
print self.message | |
self.value = raw_input().strip() | |
return len(self.value) > 0 | |
def printKeyValue(self): | |
print "%s: %s" % (self.key, self.value) | |
def confirm(): | |
print 'OK? y/N' | |
c = raw_input().strip().lower() | |
return c == 'y' or c == 'yes' | |
def readInput(inputDataList): | |
# input | |
for inputData in inputDataList: | |
if inputData.startInput() == False: | |
print "%s required" % inputData.key | |
sys.exit(1) | |
print '' | |
print '[input]' | |
for inputData in inputDataList: | |
inputData.printKeyValue() | |
print '' | |
if confirm() == False: | |
print 'bye-bye' | |
sys.exit(1) | |
# create result | |
result = {} | |
for inputData in inputDataList: | |
result[inputData.key] = inputData.value | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment