简体   繁体   中英

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.

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.

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.

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.

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.

您的代码永远不会进入while循环内部,因为最初的Current = 1和Max = 4,所以它们不相等。

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM