首页 > 解决方案 > 如何用用户输入替换一行?

问题描述

我需要在函数中执行此提示。有人可以帮帮我吗?我只是不明白怎么做。我只是一个初学者。

这应该写在一个函数中。

标签: python-3.xlistfunction

解决方案


这是一个可以帮助您入门的框架。使用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

推荐阅读