我正在编写一个Python程序,它从用户那里获取整数,并将它们添加到一个数字列表中,如果这个数字不在列表中。这通常并不困难,但我不能使用in/notin运算符、index()或{}函数来完成任务。在
创建列表后,我询问用户是否要替换列表中的任何值。如果有,则获取要查找的值和替换值。在
再一次,我不能使用in/notin运算符或index()函数来完成任务。在
我的代码:def figure_it_out(numbers_list):
while True:
number = int(input("Enter the number for the list: "))
duplicate = False
for val in numbers_list:
if number == val:
duplicate = True
break
if duplicate:
print("Duplicate found")
else:
numbers_list.append(number)
print("Added {}".format(number))
answer = input("Would you like to add another number? (y/n): ")
if answer == 'y':
continue
else:
break
return numbers_list
def replace_number(current_list):
while True:
answer = input("Would you like to replace a number? (y/n): ")
if answer == 'y':
count = 0
num_replace = int(input("What number will you replace: "))
duplicate = False
for val in current_list:
count += 1
if num_replace == val:
duplicate = True
break
if duplicate:
replacment = int(input("Replace with: "))
current_list[count - 1] = replacment
print(current_list)
else:
print("There is no number in the list.")
else:
break
return current_list
def main():
adding_list = []
cur_list = figure_it_out(adding_list)
print(cur_list)
final_list = replace_number(cur_list)
print("Your final list is: " + str(final_list))
main()
输出:
^{pr2}$
谢谢你的帮助!在