我有一个new.py文件,可用于自动执行过程。我想在脚本中添加一个用户输入,将其另存为变量。
username = input("Please enter username:")
while username not in users:
print("Incorrect Username")
username = input("Please enter username:")
print("Username Accepted")
但是当我使用如下所示的批处理文件执行new.py文件时:
cmd /c C:\ProgramData\Anaconda3\condabin\conda.bat run "C:\ProgramData\Anaconda3\python.exe"
"C:\Users\mbeig\Downloads\new.py"
pause
我收到一条错误消息:
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
我希望用户在命令行中输入一个可用作脚本变量的输入。 谢谢!
答案 0 :(得分:1)
重新安排程序逻辑以:
users = ['batman', 'robin', 'superman']
while True:
username = input("Please enter username:")
if username in users:
break
else:
print("Incorrect username")
答案 1 :(得分:0)
如果您使用的是python 3.8,则可以使用walrus运算符来缩短代码
users = ['indianajones', 'wonderwoman', 'flash']
while (username := input("Please enter username:")) not in users:
print("Incorrect username")
print(username)
# Other statements