使用Python,我如何告诉用户他们已经在列表中输入了一个数字?
我必须为我的班级写一个程序使用Python,我如何告诉用户他们已经在列表中输入了一个数字?,python,Python,我必须为我的班级写一个程序 获取用户输入(数字) 偶数和奇数的平行列表 告诉用户是否已经输入了一个号码 打印列表 还有其他我知道怎么做的事情 我还有其他的一切,我唯一不知道怎么做的就是告诉用户已经输入了一个数字 我的老师说要尝试一个for循环,但我想不出来。开始阅读关于测试成员资格和使用创建唯一的数字列表的内容。阅读下面的示例代码并尝试调整其想法。“in”关键字是特定于python的,它代替了在比较时对列表进行迭代 odd = [1, 3, 5, 7] even = [2, 4, 6, 8]
- 获取用户输入(数字)
- 偶数和奇数的平行列表
- 告诉用户是否已经输入了一个号码
- 打印列表
- 还有其他我知道怎么做的事情
我的老师说要尝试一个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"
如果是家庭作业,你应该把它贴上“家庭作业”的标签。请把你迄今为止尝试过的东西贴出来。