Skip to content

Instantly share code, notes, and snippets.

@naosim
Created December 16, 2015 22:38
Show Gist options
  • Save naosim/a193480afea32389e480 to your computer and use it in GitHub Desktop.
Save naosim/a193480afea32389e480 to your computer and use it in GitHub Desktop.
pythonで標準入力
from readutil import *
inputData = readInput([
InputData('name'),
InputData('age', 'age(number)?')
])
# -*- 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 ''
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