【问题标题】:How do I loop a python list input validation?如何循环 python 列表输入验证?
【发布时间】:2021-04-25 00:39:37
【问题描述】:
newcompany = input("Enter Company Name: ")
for stock in portfolioStock:
    if newcompany.lower() == stock[0].lower():
        newcompany = input("Company already exist in the Portfolio\nPlease enter another Company Name: ")
        break
    else:
        newcompany = newcompany.capitalize()
        continue

上面的代码只检查一次输入验证,然后再继续我的下一个代码块,我如何让它连续验证输入。 比如输入,我输入了apple,但是我的list里面有apple,所以又提示了,但是当我再次输入apple时,就直接跳到下一行代码了。

Enter Company Name: apple
Company already exist in the Portfolio
Please enter another Company Name: apple
Enter market capitalisation of company: Mega, Large or Mid: 

这就是它的样子

接下来是while 循环

newcompany = input("Enter Company Name: ")
while True:
    for stock in portfolioStock:
        if newcompany.lower() == stock[0].lower():
            newcompany = input("Company already exist in the Portfolio\nPlease enter another Company Name: ")
            break
        else:
            newcompany = newcompany.capitalize()
            continue

这就是我的代码在while 循环中的样子 这些是结果

Enter Company Name: apple
Company already exist in the Portfolio
Please enter another Company Name: apple
Company already exist in the Portfolio
Please enter another Company Name: samsung

它在samsung下面留下一个空格并且不会继续到下一行代码

【问题讨论】:

  • 你应该在这里使用while循环
  • 我也尝试了一个while循环,但它产生了相同的结果:(
  • 你能提供什么是portfolioStock吗?我假设它是一个列表列表,因为您正在将输入与 stock[0] 进行比较。但是,这仅检查列表中的第一个元素是否等于输入。这就是你想要的吗?
  • 是的,portfolioStock 是一个二维列表,我想用二维列表中的列表中的第一项验证输入

标签: python list loops validation input


【解决方案1】:

谢谢米切尔·奥利斯拉格斯!!

newcompany = input("Enter Company Name: ")
for stock in portfolioStock:
    while newcompany.lower() == stock[0].lower():
        newcompany = input("Company already exist in the Portfolio\nPlease enter another Company Name: ")
    newcompany = newcompany.capitalize()

因为我的portfolioStock 是一个包含str 和int 的二维列表,所以我不得不稍微编辑Mitchell 的代码,但它可以工作!!!非常感谢!!

【讨论】:

  • 很高兴听到。我可以想象你的代码最终按你想要的方式运行时的感觉。
  • 你应该接受他的回答并支持他,不要把它粘贴在这里
  • 对不起,我不知道 :(
【解决方案2】:

使用while 循环检查公司是否已经存在,如果存在,它将不断提示您输入新名称。当有新名称时,它将被添加到列表中

portfolioStock = ["Apple"]
company = input("Enter Company Name: ")
while company.capitalize() in portfolioStock: # e.g. 'Apple' is already in there
    company = input("Company already exist in the Portfolio\nPlease enter another Company Name: ")

portfolioStock.append(company.capitalize())

【讨论】:

    【解决方案3】:

    我并不完全清楚您在寻找什么。假设 portfolioStock 是列表中的 list,这可能对您有用。

    newcompany = input("Enter Company Name: ")
    for stock in portfolioStock:
      #Convert all elements in list to lowercase
      stock = [x.lower() for x in stock]
      #This loop wil continue as long as the input is equal to any value in stock
      while newcompany.lower() in stock:
        newcompany = input("Company already exist in the Portfolio\nPlease enter another Company Name: ")
      newcompany = newcompany.capitalize()
    

    【讨论】:

    • 什么是我的二维列表也包含整数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    相关资源
    最近更新 更多