0
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.

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it was just the "current!=max:" that was the problem. Silly mistake.
As others have pointed out, there are plenty of logical errors besides that one.
This seems like a very 'pythonic' way of doing this. Great answer.
1
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.

Comments

1

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.

Comments

1

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.

Comments

1

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.