-7

I need help (again). New to python, but how do I test for both "yes" and "y" in the same if block?

Code block showing logic switches for both 'yes' and 'y'

In shell I would just do:

#!/bin/bash
read -r -n 1 -p $'Would you like to continue? [y/n]\n\n--> ' ANSWER
case "$ANSWER" in
    Y|y)
        echo -en "\n\nContinue\n"
        ;;
    N|n)
        echo -en "\n\nStop\n"
        ;;
    *)
        echo -en "\n\nERROR\n"
        ;;
esac

I'm still new to Python3 (well, Python in general) that I don't know how to do this, and I've tried to google it, but I don't really understand what I'm actually asking for. I'm assuming it'll be very simple like BASH, but the answers I get on google seem overtly complex.

Thank you!

misteralexander
  • 448
  • 2
  • 7
  • 19

1 Answers1

1

Write

response = input('Would like to …?').lower()

if response == 'y' or response == 'yes':
    …

Or

if response in ['y', 'yes']:
    …

Note that it’s not necessary to call str — your object is already a string.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214