使用 'While' 语句
using 'While' statements
当试图做到这一点时,当用户不在输入中键入登录或注册时,程序将要求他们从他们拥有的选项中进行选择,但是当我尝试在 or 操作员进入 while 语句的条件并单击 运行,程序将继续循环说同样的事情,即使用户输入登录或注册,但是当我删除 or 运算符时,while 语句起作用...... .... ps 我是 python
的初学者
定义菜单():
print("Welcome to the banking app")
print("Would you like to login or register?")
ans = input()
while ans != "login" or 'register':
print("Please choose the choices given")
ans = input()
if ans == 'login':
print('They want to log in')
elif ans == 'register':
print('They want to register')
菜单()
问题出在你的条件上,or
在这种情况下不合适,你需要改用and
。但是如果您需要向条件添加更多元素,它可能会非常庞大,因此您还可以验证 ans
是否在预定义答案列表中:
print("Welcome to the banking app")
print("Would you like to login or register?")
ans = ""
while ans not in ["register", "login"]::
ans = input("Please choose the choices given ")
print('They want to ', ans)
while ans != "login" or 'register':
行与您认为的不一样。它首先评估 ans != "login"
为 True 或 False。然后它评估 'register'
为 True 或 False。你看,'register' 将始终计算为真,因为它是非空的(python 在布尔上下文中将所有非空项计算为真)。
您要使用的是:
while ans != "login" and ans != "register":
您还可以使用:
while ans not in ["login", "register"]:
print("Welcome to the banking app")
print("Would you like to login or register?")
ans=''
while ans != "login" or "register":
print("Please choose the choices given")
ans = input()
if ans == 'login':
print('They want to log in')
break
elif ans == 'register':
print('They want to register')
break
当试图做到这一点时,当用户不在输入中键入登录或注册时,程序将要求他们从他们拥有的选项中进行选择,但是当我尝试在 or 操作员进入 while 语句的条件并单击 运行,程序将继续循环说同样的事情,即使用户输入登录或注册,但是当我删除 or 运算符时,while 语句起作用...... .... ps 我是 python
的初学者定义菜单():
print("Welcome to the banking app")
print("Would you like to login or register?")
ans = input()
while ans != "login" or 'register':
print("Please choose the choices given")
ans = input()
if ans == 'login':
print('They want to log in')
elif ans == 'register':
print('They want to register')
菜单()
问题出在你的条件上,or
在这种情况下不合适,你需要改用and
。但是如果您需要向条件添加更多元素,它可能会非常庞大,因此您还可以验证 ans
是否在预定义答案列表中:
print("Welcome to the banking app")
print("Would you like to login or register?")
ans = ""
while ans not in ["register", "login"]::
ans = input("Please choose the choices given ")
print('They want to ', ans)
while ans != "login" or 'register':
行与您认为的不一样。它首先评估 ans != "login"
为 True 或 False。然后它评估 'register'
为 True 或 False。你看,'register' 将始终计算为真,因为它是非空的(python 在布尔上下文中将所有非空项计算为真)。
您要使用的是:
while ans != "login" and ans != "register":
您还可以使用:
while ans not in ["login", "register"]:
print("Welcome to the banking app")
print("Would you like to login or register?")
ans=''
while ans != "login" or "register":
print("Please choose the choices given")
ans = input()
if ans == 'login':
print('They want to log in')
break
elif ans == 'register':
print('They want to register')
break