简体   繁体   中英

Output strings into command line from Python script

I am currently working on an automation task. I have arrived at a part of the script where I need confirmation from the user to continue. For example the script is automatically entering some values in Selenium for the user however I need confirmation from the user that these are the correct variables. Let's say I want to output this list into the command prompt:

['ESPN', 'ESPN Radio', 'ESPNews', 'ESPN2', 'ESPN3']

The user then can enter 'ESPN2' and my script will continue with the rest of its processes. How would I go about implementing the first portion of this? I believe the second portion of this task would involve using sys.argv[0] to correctly store the value the user enters back. However the issue is with the first part, how do I implement this so that my script knows to pause and wait for the user input? Thanks!

IIUC you need to wait for the user until it matches your list.

Also make sure that you go through below link and reads the basics https://docs.python.org/3/

lst=['ESPN', 'ESPN Radio', 'ESPNews', 'ESPN2', 'ESPN3']
print lst


while True:
    value=raw_input("enter a value from list\n")
    if value not in ('ESPN', 'ESPN Radio', 'ESPNews', 'ESPN2', 'ESPN3'):
        print("Not an appropriate choice.")
    else:
        print("Continue to other modules")
        break

output

    ['ESPN', 'ESPN Radio', 'ESPNews', 'ESPN2', 'ESPN3']

    enter a value from list
    xx
    Not an appropriate choice.

    enter a value from list
    yy
    Not an appropriate choice.

    enter a value from list
    ESPN
    Continue to other modules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM