我必须写一个程序我的类使用Python,我如何告诉用户他们已经在列表中输入了一个数字?
- 获取用户输入(数字)
- 化妆平行于偶数和奇数
- 告诉用户列表是一些已经进入
- 打印清单
- 和其他我知道该怎么做的事情。
我拥有的一切只是我不知道该怎么做才告诉用户已经输入了一个数字。
我的老师说要尝试一个for循环,但我无法弄清楚。
我必须写一个程序我的类使用Python,我如何告诉用户他们已经在列表中输入了一个数字?
我拥有的一切只是我不知道该怎么做才告诉用户已经输入了一个数字。
我的老师说要尝试一个for循环,但我无法弄清楚。
的唯一列表阅读下面的示例代码,并尝试去适应它的想法。 “in”关键字是特定于python的,并且在比较时会替换对列表的迭代。
odd = [1, 3, 5, 7]
even = [2, 4, 6, 8]
user_input = int(raw_input("enter a number: "))
if user_input in odd or user_input in even:
print "the number "+str(user_input)+" was already entered"
else:
if user_input %2 == 0:
even.append(user_input)
print "added to even list"
else:
odd.append(user_input)
print "added to odd list"
这里是如何使用for循环的例子:
some_list = [1,2,6,7]
is_in_the_list = False
for x in some_list:
if x == 7: # 7 plays the role of the user input here
is_in_the_list = True
if is_in_the_list:
print "It is in the list"
谢谢我终于搞定了! – 2011-12-16 22:08:46
如果这是你应该标记为“功课”的功课。并请张贴迄今为止尝试过的内容。 – 2011-12-16 21:04:47