2

I have a new.py file that i use to automate a procedure. I want to add a user input in the script which will be saved as a variable.

username = input("Please enter username:")

while username not in users:
    print("Incorrect Username")
    username = input("Please enter username:")

print("Username Accepted")

But when i execute my new.py file using a batch file which is as follows:

cmd /c C:\ProgramData\Anaconda3\condabin\conda.bat run "C:\ProgramData\Anaconda3\python.exe" 
"C:\Users\mbeig\Downloads\new.py"
pause

I get an error saying:

Please enter username:
Traceback (most recent call last):
  File "C:\Users\mbeig\Downloads\new.py", line 39, in <module>
    username = input("Please enter username:")
EOFError: EOF when reading a line

I want the user to enter an input in command line which can be used as variable in the script. Thanks!

1
  • Your question and issue is unrelated to batch files, I've therefore removed the [batch-file] tag. If it is related to your batch file, please explain where in the single line command you've offered you think the issue lies. If your question is about how to request user input in a batch file and hold that input in a variable, then the [python] and [automation] tags, would be irrelevant, and should be removed instead, along with the python code, and error message text. However we can clearly see that your error is python generated, and not batch file or [cmd] related. Commented Aug 6, 2020 at 21:21

2 Answers 2

2

Re-arrange the logic of your program to:

users = ['batman', 'robin', 'superman']

while True:
    username = input("Please enter username:")
    if username in users:
        break
    else:
        print("Incorrect username")
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Jan, Thanks for your reply. The issue is not with the python code, i need to find a way for a user input in the command line. I hope this makes sense.
0

If you are using python 3.8, you can use walrus operator to shorten your code

users = ['indianajones', 'wonderwoman', 'flash']

while (username := input("Please enter username:")) not in users:
    print("Incorrect username")
    
print(username)
# Other statements

3 Comments

Hi bigbounty, Thanks for your reply. I need to find a way for a user input in the command line. Shortening the code wouldn't make a difference here, but thanks for the suggestion.
@M.SameerBeig the code takes the input from the command line. Please run it once in python 3.8
Hi bigbounty, I tried using the code you suggested but this creates a boolean value for username. I am trying to add a string to username with user's input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.