我有一个新的.py文件,用于自动化一个过程。我想在脚本中添加一个用户输入,该输入将保存为变量。
username = input("Please enter username:")
while username not in users:
print("Incorrect Username")
username = input("Please enter username:")
print("Username Accepted")
但是当我使用如下的批处理文件执行我的新.py文件时:
cmd /c C:ProgramDataAnaconda3condabinconda.bat run "C:ProgramDataAnaconda3python.exe"
"C:UsersmbeigDownloadsnew.py"
pause
我得到一个错误说:
Please enter username:
Traceback (most recent call last):
File "C:UsersmbeigDownloadsnew.py", line 39, in <module>
username = input("Please enter username:")
EOFError: EOF when reading a line
我希望用户在命令行中输入一个可以在脚本中用作变量的输入。谢谢
将程序的逻辑重新安排为:
users = ['batman', 'robin', 'superman']
while True:
username = input("Please enter username:")
if username in users:
break
else:
print("Incorrect username")
如果你使用的是python 3.8,你可以使用walrus操作符来缩短你的代码
users = ['indianajones', 'wonderwoman', 'flash']
while (username := input("Please enter username:")) not in users:
print("Incorrect username")
print(username)
# Other statements