Joe Taylor
Joe Taylor

Reputation: 11

Python Program, while loop not executing?

Names=[0,1,2,3,4]
Names[1]=='Ben'
Names[2]=='Thor'
Names[3]=='Zoe'
Names[4]=='Katie'
Max=4
Current=1
Found=False
PlayerName=input('What player are you looking for?')
while Found==False and Current==Max:
    if Names[Current]==PlayerName:
        Found=True

    else:
        Current+=1


if Found==True:
    print('Yes, they have a top score')
else:
    print('No, they do not have a top score')

This is the program. When any of the 4 names at the top are entered, the program should print, 'Yes, they have a top score', but when anything else is entered it should print,'No, they do not have a top score'.

However whatever name is entered it returns the 'No, they do not have a top score' message. I think it may have something to do with the loop but not sure what.

Upvotes: 0

Views: 160

Answers (6)

glglgl
glglgl

Reputation: 91017

Do you try to program BASIC in Python? :-)

Others have answered why it does not work. Even if you do these changes, your program does look more like BASIC than like Python.

You should do the following:

  1. Use index 0 as well. You allocate 0..4 and use 1..4. Just use 0..3.
  2. iterate through a list with for item in listobject: instead of the while loop.
  3. Don't do if Found==True:, just use if Found:
  4. Don't name your variables with starting uppercase - these names are for classes. Use found instead of Found.

Upvotes: 1

Asterisk
Asterisk

Reputation: 3574

Your code never enters inside while loop, because initially Current = 1 and Max = 4, so they are not equal.

Upvotes: 1

Simeon Visser
Simeon Visser

Reputation: 122326

Look here:

Names=[0,1,2,3,4]
Names[1]=='Ben'
Names[2]=='Thor'
Names[3]=='Zoe'
Names[4]=='Katie'

This doesn't do what you think it does. Afterwards, Names is equal to [0, 1, 2, 3, 4]. The next few lines don't assign names to Names but only check whether an element is equal to a name. For example, Names[1]=='Ben' checks if the second element in Names is equal to Ben (so this evaluates to True or False) but nothing is done with the result.

Upvotes: 1

user836352
user836352

Reputation:

In your while loop, you're doing these comparisons:

Found==False and Current==Max

The second part of the condition will never evaluate to True because Current is always set to 1 before the loop, which is != to Max - therefore, the code in the loop never evaluates.

Upvotes: 1

Thomas Orozco
Thomas Orozco

Reputation: 55199

Names[1]=='Ben'
...

Is not assignment, it is equality check (and it returns False, although this is irrelevant)

Hence, your list is not modified, and names are checked against the list [0,1,2,3,4], and they never match, which is not a surprise.

Additionally, your loop condition is incorrect, and the code there is never run.

You should however consider writing your program is a more pythonic way, using the in operator, as suggested above, or a least using a for loop, iterating over your list.

Upvotes: 1

Platinum Azure
Platinum Azure

Reputation: 46183

Your second condition is inverted. You want

while Found==False and Current!=Max:

That said, in Python you can do this much more simply using the in operator:

names = ['Ben', 'Thor', 'Zoe', 'Katie']
player_name = input('What player are you looking for?')

if player_name in names:
    print('Yes, they have a top score')
else:
    print('No, they do not have a top score')

That way, you don't need the while loop at all.

Upvotes: 5

Related Questions