我正在尝试构建一个代码,该代码将从用户那里获取整数序列。输入仅在输出包含1/0 / *时有效。根据我编写的代码,系统只检查单个值。 如果有人可以帮助我,这将是很好的。我是python的新手,并试图弄清楚它是如何工作的。感谢
答案 0 :(得分:0)
首先,以下是我对您的代码的反馈意见:
data =='0' or 'data == '1' or data == '*'
替换为data in ['0', '1', '*']
以更加pythonic break
,例如“停止”或“停止”其次,这是一个考虑到之前评论的代码建议:
# Prompt for sequence of 0, 1 and * characters only
# Stop the input sequence when "Stop" or "stop" is entered
flag = True
while flag:
data = input("Enter an input value: ")
if data in ['0', '1', '*']:
print ("The input {0} is valid".format(data))
elif data in ['Stop', 'stop']:
print ("Stop")
break
else:
print ("The input {0} is invalid".format(data))
flag = False
# Here: flag is True if input sequence was correct, otherwise flag is False