M. Sameer Beig
M. Sameer Beig

Reputation: 53

Taking a user input in python using batch file

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!

Upvotes: 2

Views: 1674

Answers (2)

bigbounty
bigbounty

Reputation: 17368

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

Upvotes: 0

Jan
Jan

Reputation: 43169

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")

Upvotes: 2

Related Questions