我在错误时使用吗(Python)

发布于 2025-01-24 10:32:08 字数 560 浏览 3 评论 0原文

我正在尝试让用户输入正确的名称,但是当我运行时,即使我进入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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2025-01-31 10:32:08

此处的问题在您的中,条件中的中。

while favPlayer != 'lebron james' and 'jayson tatum':

用于测试两个条件,但是这里正在测试条件(favplayer!='lebron james)和一个字符串('jayson tatum')。

取而代之的是,

while favPlayer != 'lebron james' and favPlayer != 'jayson tatum':

您的尝试没有错误进行的原因是,python中的任何非零东西都被视为true。因此,实际上您的陈述被解释为

 while favPlayer != 'lebron james' and True:

 while favPlayer != 'lebron james':

The issue here is in your and in your while condition.

while favPlayer != 'lebron james' and 'jayson tatum':

and is used to to test two conditions, but here it's testing a condition (favPlayer != 'lebron james) and a string ('jayson tatum').

Instead

while favPlayer != 'lebron james' and favPlayer != 'jayson tatum':

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:

 while favPlayer != 'lebron james' and True:

Which is the same as

 while favPlayer != 'lebron james':
合久必婚 2025-01-31 10:32:08

您可以在循环时使用无限进行输入,直到用户输入其中一个名称:

while True:
    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')
        break
    elif favPlayer == 'lebron james':
        position = 'Power Forward'
        print('Your position is Power Forward')
        break

输出:

Favorite player on the list (Lebron James, Jayson Tatum): Arsho
Favorite player on the list (Lebron James, Jayson Tatum): Lebron James
Your position is Power Forward

You can take input using an infinite while loop until the user enters one of the names:

while True:
    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')
        break
    elif favPlayer == 'lebron james':
        position = 'Power Forward'
        print('Your position is Power Forward')
        break

Output:

Favorite player on the list (Lebron James, Jayson Tatum): Arsho
Favorite player on the list (Lebron James, Jayson Tatum): Lebron James
Your position is Power Forward
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文