我在错误时使用吗(Python)
我正在尝试让用户输入正确的名称,但是当我运行时,即使我进入Jayson Tatum,也一直要求我重写,但是当我写Lebron James时,它可以正常工作吗?感谢您的检查 这是代码
while favPlayer != 'lebron james' and 'jayson tatum':
favPlayer = str.lower(input('Favorite player on the list (Lebron James, Jayson Tatum): '))
if favPlayer == 'jayson tatum':
position = 'Small Forward'
print('Your position is Small Forward')
elif favPlayer == 'lebron james':
position = 'Power Forward'
print('Your position is Power Forward')
以下是运行: [在这里输入图像描述] [1] [1]:https://i.sstatic.net/9bem2.png
I'm trying to get the user to input the right name, but when I run it keeps asking me to rewrite even when I entered jayson tatum, but when I wrote lebron james it works? Thanks for your inspections
Here's the code
while favPlayer != 'lebron james' and 'jayson tatum':
favPlayer = str.lower(input('Favorite player on the list (Lebron James, Jayson Tatum): '))
if favPlayer == 'jayson tatum':
position = 'Small Forward'
print('Your position is Small Forward')
elif favPlayer == 'lebron james':
position = 'Power Forward'
print('Your position is Power Forward')
Here's the run:
[enter image description here][1]
[1]: https://i.sstatic.net/9bem2.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此处的问题在您的
和
中,条件中的中。
和
用于测试两个条件,但是这里正在测试条件(favplayer!='lebron james
)和一个字符串('jayson tatum')。
取而代之的是,
您的尝试没有错误进行的原因是,python中的任何非零东西都被视为
true
。因此,实际上您的陈述被解释为:
The issue here is in your
and
in yourwhile
condition.and
is used to to test two conditions, but here it's testing a condition (favPlayer != 'lebron james
) and a string ('jayson tatum'
).Instead
The reason your attempt ran without error is that any non-zero thing in python is considered
True
. So really your while statement was being interpreted as:Which is the same as
您可以在循环时使用无限进行输入,直到用户输入其中一个名称:
输出:
You can take input using an infinite while loop until the user enters one of the names:
Output: