2017年7月7日 星期五

Python - How to get user input? - 透過input函數與使用者互動

Python version :Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
System version :Windows 10
repeat = True
while(repeat):
    str_press = input('Print \'Hello world\' if you press y  , don not print \'Hello world\' if you press n\n')
    if str_press=='y':
        repeat = False
        print('Hello world!')
    elif str_press=='n':
        repeat = False
        print('Nothing happened.')
    else:
        print('Please press y or n.')
執行結果:
Print 'Hello world' if you press y  , don not print 'Hello world' if you press n
y
Hello world!

# or
Print 'Hello world' if you press y  , don not print 'Hello world' if you press n
n
Nothing happened.

# or
Print 'Hello world' if you press y  , don not print 'Hello world' if you press n
h
Please press y or n.
Print 'Hello world' if you press y  , don not print 'Hello world' if you press n

沒有留言:

張貼留言