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
  1. repeat = True
  2. while(repeat):
  3. str_press = input('Print \'Hello world\' if you press y , don not print \'Hello world\' if you press n\n')
  4. if str_press=='y':
  5. repeat = False
  6. print('Hello world!')
  7. elif str_press=='n':
  8. repeat = False
  9. print('Nothing happened.')
  10. else:
  11. 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

沒有留言:

張貼留言