'am i using while wrong (PYTHON) [duplicate]
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
Solution 1:[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':
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | arsho |
