python中从键盘输入名字不能带有数字_将输入添加到数字列表(如果该数字不在列表Python中)...

该Python程序从用户获取整数并添加到列表中,仅当数字未出现时。不允许使用in/not in或index()检查重复。另外,允许用户替换列表中的数值,同样不依赖于in/not in或index()。程序包含figure_it_out和replace_number两个功能函数,以及main函数来运行整个流程。
摘要由CSDN通过智能技术生成

我正在编写一个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}$

谢谢你的帮助!在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现该功能的 Python 代码: ```python # 定义链表结点类 class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next # 从键盘输入一串数字,建立单向链表,并返回链表的头结点 def createLinkedList(): dummy = ListNode() # 创建头结点 cur = dummy # 链表当前结点指针 while True: val = int(input("请输入一个数字输入0表示结束):")) if val == 0: break new_node = ListNode(val) cur.next = new_node cur = new_node return dummy.next # 对给定的单向链表进行从大到小排序 def sortLinkedList(head): if not head or not head.next: return head prev, slow, fast = None, head, head while fast and fast.next: prev, slow, fast = slow, slow.next, fast.next.next prev.next = None # 将链表拆成两半 left = sortLinkedList(head) right = sortLinkedList(slow) return merge(left, right) # 合并两个已排序的链表 def merge(l1, l2): dummy = ListNode() cur = dummy while l1 and l2: if l1.val >= l2.val: cur.next = l1 l1 = l1.next else: cur.next = l2 l2 = l2.next cur = cur.next cur.next = l1 if l1 else l2 return dummy.next # 打印链表的所有结点值 def printLinkedList(head): cur = head while cur: print(cur.val, end=" ") cur = cur.next print() # 测试代码 head = createLinkedList() print("原始链表:") printLinkedList(head) head = sortLinkedList(head) print("排序后的链表(从大到小):") printLinkedList(head) ``` 运行以上代码,输入一些数字,例如:`9 2 5 4 7 0`,则输出结果如下: ``` 请输入一个数字输入0表示结束):9 请输入一个数字输入0表示结束):2 请输入一个数字输入0表示结束):5 请输入一个数字输入0表示结束):4 请输入一个数字输入0表示结束):7 请输入一个数字输入0表示结束):0 原始链表: 9 2 5 4 7 排序后的链表(从大到小): 9 7 5 4 2 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值