0

When I input text for my verdict I can put anything, e.g. "zzzzz", and the program continues on through the list.

I want the program to stop when user inputs "Guilty". And only move on when user inputs "Not Guilty".

This is what I have so far, I also tried using while True if that's better?

guilty = False
character = ["Miss Scarlett", "Professor Plum", "Mrs Peacock", "Reverend Green", "Colonel Mustard", "Dr Orchid"]

while not guilty:
    for accused in character:
        verdict = input("Do you find %s guilty or not guilty? " % accused)
        if verdict == "guilty":
            print("User finds %s guilty" % accused)
            guilty = True
            break
        else:
            guilty = False
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Mike
  • 11
  • 2
  • it is case sensitive, so it might be a good idea to use `verdict.lower()` where you're doing the if comparison, other than that it looks like it's fitting your needs – ihoryam Oct 26 '20 at 15:59
  • 3
    Does this answer your question? [How do I do a case-insensitive string comparison?](https://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison) – mkrieger1 Oct 26 '20 at 16:01
  • Just to make it more explicit: string comparisons in Python are case sensitive, so `verdict == "guilty"` and `verdict == "Guilty"` are different statements and will only match strings that have the exact same case. Using something like `verdict.lower() == "guilty"` will transform the user input to lower case and, only then, compare it -- this will make your program work regardless of whether the user wrote `guilty`, `Guilty`, or `gUiLtY`. – Matheus Portela Oct 26 '20 at 16:08
  • thanks for the help guys – Mike Nov 03 '20 at 19:31

1 Answers1

1

To accept only answers guilty and not guilty you can remove the top while loop and move it inside for-loop. Also, use str.lower to make answers case-insensitive:

character = ["Miss Scarlett", "Professor Plum", "Mrs Peacock", "Reverend Green", "Colonel Mustard", "Dr Orchid"]

for accused in character:
    verdict = ''
    while verdict not in ('guilty', 'not guilty'):
        verdict = input("Do you find %s guilty or not guilty? " % accused).lower()

    if verdict == "guilty":
        print("User finds %s guilty" % accused)
        break

Prints (for example):

Do you find Miss Scarlett guilty or not guilty? d
Do you find Miss Scarlett guilty or not guilty? d
Do you find Miss Scarlett guilty or not guilty? f
Do you find Miss Scarlett guilty or not guilty? not guilty
Do you find Professor Plum guilty or not guilty? guilty
User finds Professor Plum guilty
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91