对照列表内容验证用户输入的最佳方法是什么? [英] What is the best way to validate user input against the contents of a list?

查看:44
本文介绍了对照列表内容验证用户输入的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有很多Python新手,正在尝试学习.我有一个同事启动的脚本,我正在努力向其中添加新功能.我想验证用户输入是否在我拥有的两个列表之一之内.

Pretty much a Python newbie here and trying to learn. I have a script that a colleague started and I'm working on adding new functionality to it. I'd like to validate that a user input is within one of two lists I have.

我从用户那里得到一个原始输入,要求一个站点名称,我想添加代码来检查用户输入是否在预定义列表中sites_2017和sites_2018(稍后在代码中使用),如果不是,则返回错误;如果不是,则继续执行脚本的其余部分.我一直在搜索,发现很多不同的while循环答案和函数,但是到目前为止,没有一个引用多个列表来进行匹配.

I have a raw input from the user asking for a site name and I'd like to add code that will check if the user's input is within the pre-defined lists sites_2017 and sites_2018 (which are used later in the code), then return an error if it isn't or proceed with the rest of the script if it is. I've been searching around and have seen lots of different while loop answers as well as functions, but none so far that reference multiple lists to match against.

只是想尽办法,找出最适合的代码,以及在当前代码中的while循环中的其他地方,等等.

Just trying to wrap my head around this and figure out the best code for it as well as where in the current code a while loop, etc would need to go.

# Ask user input for what they'd like to do? Render or audit? _Step user through process_

import os
import subprocess

getuser = raw_input("Please enter your username :")

print("1. render_device")
print("2. audit_deivce")
askuser = raw_input("Would you like to render_device or audit_deivce? : ")

#Render:

if askuser == "1":
        get_site_name = raw_input("Please enter the site name you'd like to render :")

        sites_2017 = ["bob", "joe", "charlie"]
        sites_2018 = ["sarah", "kelly", "christine"]

推荐答案

已更新为可处理评论

您将要在此处利用 in ,最好从@abarnert在评论中提到的两个列表中创建一个集合.您可以将其包装在函数内部,并在不满足条件的情况下递归调用该函数(请注意,为了与Python 3兼容,我已将 raw_input()更改为 input()):

You will want to leverage in here and it's best to create a set from the two lists as mentioned by @abarnert in the comments. You could wrap this inside of a function and recursively call the function if the conditions are not satisfied (note I have changed raw_input() to input() for Python 3 compatibility):

getuser = input("Please enter your username :")

print("1. render_device")
print("2. audit_device")

askuser = input("Would you like to render_device or audit_device? : ")

def verify_input(sites_set):

    get_site_name = input("Please enter the site name you'd like to render :")

    if get_site_name in sites_set:
        print('Proceed')
        return
    else:
        print('Not in either list!')
        verify_input(sites_set)

if askuser == "1":

        sites_2017 = ["bob", "joe", "charlie"]
        sites_2018 = ["sarah", "kelly", "christine"]

        verify_input(set(sites_2017 + sites_2018))

编辑

但是,更简单的实现是只使用 while 循环:

However, a much simpler implementation is to just use a while loop:

getuser = input("Please enter your username :")

print("1. render_device")
print("2. audit_device")

askuser = input("Would you like to render_device or audit_device? : ")

if askuser == "1":

        sites_2017 = ["bob", "joe", "charlie"]
        sites_2018 = ["sarah", "kelly", "christine"]

        sites_set = set(sites_2017 + sites_2018)
        proceed = False

        while not proceed:

            get_site_name = input("Please enter the site name you'd like to render :")

            if get_site_name in sites_set:
                print('Proceed')
                proceed = True
            else:
                print('Not in either list!')

这篇关于对照列表内容验证用户输入的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆