0

I am new to python, I have written the following code but it will not reduce the score if B is entered, it increases the score by 1 if How, Hi etc are entered but not it 8 is entered and does not reduce the score if B is entered. Can anyone help.

grid=[8,"B","How", "Hi", "Hey", "How are you doing", "How's it going", "How", "Hello"]

def Play(): 
 count=0 
 score=0 
 while (count<11):
 I=input("Enter your guess")
 if I in grid:
 score+=1
 print("Your score is:", score)
 elif I in grid == "B":
 score=0
 print("Your score is:",score)
 else:
 print("I don't understand")
 count+=1
asked Feb 5, 2017 at 18:25
2
  • elif I in grid == "B": doesn't make sense. Take B out of grid and put it in a different list. Otherwise you need elif I in grid and I == 'B': or something similar, which is counter-intuitive as a setup. Commented Feb 5, 2017 at 18:29
  • I in grid returns a bool. You're asking "If I is in grid..." (which returns True or False) "is equal to the letter B, do this". True and False won't be equal to the letter B. Commented Feb 5, 2017 at 18:31

1 Answer 1

1

input() stores input as a string. Therefore, if the user enters 8, it will be stored as "8". "8" is not the same as 8, so if I in grid will evaluate to False. To solve this, you can change 8 to "8" in the list, or add elif I == "8":

elif I in grid == "B": will never evaluate as true. This is because you are checking (I in grid) == ("B"). I in grid will in always be True or False, so you are comparing "B" with a boolean, which is always False.

An easier way to accomplish this is to change 8 to "8" in `grid:

grid=["8","B","How", "Hi", "Hey", "How are you doing", "How's it going", "How", "Hello"]

And check if I == "B" in the first if statement:

if I in grid:
 if I == "B":
 score=0
 else:
 score+=1
 print("Your score is:",score)
else:
 print("I don't understand")
answered Feb 5, 2017 at 18:41
Sign up to request clarification or add additional context in comments.

1 Comment

There was one thing we both haven't commented on, which might be worth noting. if I in grid: is True even for "B" so elif I in grid == "B": is never even evaluated anyway in the OP's approach since it breaks out at the first True evaluation.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.