我想用python编写一个非常简单的程序:
while True:
# 1. input = get sys.std
if input == 'y':
print "yes"
elif input == 'n':
print "no"
else input == 'q':
# 2. quit
如何实施step 1
和step 2
?
答案 0 :(得分:1)
raw_input()
从用户处获取价值。input
if elif else
循环检查条件。break
关键字退出while循环。<强>演示强>:
while True:
# 1. input = get sys.std
user_input = raw_input("Enter 'y' to print yes, 'n' to print 'no', "
"and 'q' to quit: ")
if user_input == 'y':
print "yes"
elif user_input == 'n':
print "no"
elif user_input == 'q':
# 2. quit
break
else:
print "Wrong input try again."
<强>输出强>:
Enter 'y' to print yes, 'n' to print 'no' and 'q' break code: y
yes
Enter 'y' to print yes, 'n' to print 'no' and 'q' break code: n
no
Enter 'y' to print yes, 'n' to print 'no' and 'q' break code: y
yes
Enter 'y' to print yes, 'n' to print 'no' and 'q' break code: df
Wrong input try again.
Enter 'y' to print yes, 'n' to print 'no' and 'q' break code: q
按命令行
使用sys.argv
获取参数列表,第一个元素是py文件名
<强>演示强>:
import sys
arguments = sys.argv
print "Arguments:", sys.argv
print "Type Arguments:", type(arguments)
<强>输出强>:
Arguments: ['polydict.py', 'argument1', 'argument2']
Type Arguments: <type 'list'>
注意强>:
对Python 3.x使用input()
对Python 2.x使用raw_input()
答案 1 :(得分:0)
如果是python 2.x,你说的是你可以使用raw_input
方法:
name = raw_input("What's your name? ")
在python 3.x中,你应该使用input
方法:
name = input("What's your name? ")
要终止您的计划,您可以使用sys.exit()
:
import sys
sys.exit()
答案 2 :(得分:0)
在python3中,你有一个内置的叫做输入
def Entersomething():
myinp = input("Enter your options:")
if myinp == 'y':
print("yes")
elif myinp =='n':
print("no")
elif myinp =='e':
print("exit")
Entersomething()