0

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.stack.imgur.com/9bem2.png

JNevill
  • 46,980
  • 4
  • 38
  • 63

2 Answers2

1

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':
JNevill
  • 46,980
  • 4
  • 38
  • 63
-1

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
arshovon
  • 13,270
  • 9
  • 51
  • 69
  • I really don't understand why `while True:` would be preferable to putting the condition that causes `break` in the `while` itself. I know it's likely a matter of opinion, but every time I see `while True:` in real life code, I know I'm going on a wild goose chase to find all the `break` lines. Maybe there is more to it since this seems to be more common in python than other languages. – JNevill Apr 25 '22 at 16:00
  • Don't indent the if-elif block: the while loop *only* checks for valid input; the result of the user entry is handled outside of the while loop. – 9769953 Apr 25 '22 at 16:03
  • This is not a production-level code. This kind of exercise typically requires a lot of `if`, `else-if` and using breaks gives some kind of feel of `switch` block. Assume, there are more than 5 names. It would be really hard to maintain this condition in the `while name!="name_1" or name!="name_2" ...` – arshovon Apr 25 '22 at 16:04
  • `while name not in valid_names:` would be perfectly fine in that case. – 9769953 Apr 25 '22 at 16:05
  • @9769953 good catch on the list. I did not want to introduce the list concept assuming OP is practicing loop and conditional statements only. – arshovon Apr 25 '22 at 16:06
  • That is often hard to tell; it may be practice, it may be learning Python without any specific language practices. I guess that's why I automatically commented the correct comparison, and the more Python-esque comparison for this case. – 9769953 Apr 25 '22 at 16:10