python - 我需要重新打印我的输入,但只打印我的哨兵
问题描述
sentinel = "Done"
input_string(str(input("Enter strings (end with DONE):")))
while input_string != sentinel:
input_string= str(input())
#remove duplicates
res = []
for i in input_string:
if i not in res:
res.append(i)
print("\nUnique list:")
print(input_string)
这是预期的输出
Sample I/O
Enter strings (end with DONE):
the
old
man
and
the
sea
DONE
Unique list:
the
old
man
and sea
解决方案
在您的代码中,input_string
不是列表,而是每个输入重新分配的值。正因为如此,1)for
循环并没有完全按照您的意图执行,2)print(input_string)
只是打印最后一个用户输入,即“完成”。
以下应该做的工作:
sentinel = "DONE"
input_list = []
input_string = str(input("Enter strings (end with DONE):"))
input_list.append(input_string)
while input_string != sentinel:
input_string = str(input())
input_list.append(input_string)
#remove duplicates
res = []
for i in input_list:
if i not in res and i != "DONE":
res.append(i)
print("\nUnique list:")
print(res)
PS:在我的片段中,我已更改为“ DONE sentinel
”以确保.sentinel
input_string
推荐阅读
- reactjs - 如何在与firebase数据库的反应中执行crud操作
- python - 特定页面的Sphinx配置
- youtube - 处理已放弃 无法处理视频
- c++ - 为什么这是打印内存地址而不是值
- php - 从许多变量中选择 3 个最大的数字并使用这些值来回应适当的反应
- asp.net-core - 为什么调用 Breeze metadata() 会返回 .net core 3.0.100 的错误
- python - 如何正确地将大尺寸数组重塑为两个单独的分配?
- azure - 从 Node.js 创建 WebApp 资源时,“参数 LinuxFxVersion 的值无效”
- python-3.x - 返回节点的更改(通过 apoc.trigger)版本而不是原始版本
- node.js - 试图处理 mkdir 抛出的错误