python-3.x - 如何用用户输入替换一行?
问题描述
我需要在函数中执行此提示。有人可以帮帮我吗?我只是不明白怎么做。我只是一个初学者。
这应该写在一个函数中。
提示输入两个行号,一个是要复制的文本的起始行,第二个是要复制的文本的结束行。对列表的这一部分进行切片,并将其作为单个字符串添加到列表的末尾。检查输入的行号是否有效。如果它们无效,请再次询问行号。
提示输入行号,然后用用户提供的新行替换该行。检查输入的行号是否有效。如果无效,请再次询问行号
解决方案
这是一个可以帮助您入门的框架。使用input
内置函数获取用户输入并检查它们在while
循环中是否有效
def isValid(start, end):
# Check if your start and end numbers are value here
return start <= end
def getUserInput():
start = 0
end = -1
while True:
start = input("Enter starting line number: ")
end = input("Enter ending line number: ")
if not isValid(start, end):
print("Invalid input, try again")
else:
break
## Do what you want with the list here
return start, end
推荐阅读
- pass-by-reference - 通过引用传递并分配给变量
- symfony - (Symfony 4)如何从非控制器类中获取项目的基本 URI(http://www.yourwebsite.com/)?
- powerquery - Power Query 从日期转换为 MMM-YY 和 QYY 格式
- pine-script - 尝试在 Pinescript Backtesting 中正确配置挂单
- android - 从 Play 商店下载的 Android 应用程序不是我最终播放的应用程序版本
- java - Spring Security 未创建 CSRF 令牌
- ios - Appcelerator Angular Routing iOS - 如何运行示例应用程序?
- docker - 在同一个应用程序中处理多个 docker-compose 文件
- c# - 在给定角度的椭圆上找到点,然后反转该过程
- reactjs - ReactJS:如何在没有 1 单击延迟的情况下刷新组件