1

I would like this program to only accept as inputted text any of "yes ,"y", "Yes" but for some reason when I input one of them nothing happens and the loop below doesn't seem to run:

import time

print ("Welcome to my first ever RPG! created 10/07/2016")
time.sleep(2)
begin = raw_input("Would you like to start the game?")

Start = False

if begin == ("yes" , "y" , "Yes" ):
    Start == True        

while Start == True:   
    player_name = raw_input("What would you like to name your character")
    print ("welcome " + player_name.capitalize())

(PS: simplest solution preferred, I'm sort of new to python)

smci
  • 32,567
  • 20
  • 113
  • 146

2 Answers2

1

begin is a string and ("yes" , "y" , "Yes" ) is a tuple. Thus, begin == ("yes" , "y" , "Yes" ) will never be true. However, there are three strings in the tuple which can be compared to begin. The verbose way of doing that would be to write:

for element in ("yes" , "y" , "Yes" ):
    if element == begin:
        Start = True

Python has a handy way of doing this operation in fewer lines of code using the in keyword:

if begin in ("yes" , "y" , "Yes" ):
    Start = True

Note that I also changed Start == True to Start = True since == is only for comparison and here you probably want an assignment which is done using =.

To catch more variations of the user input ("Yes", "YES", "yES", "y", "Y", etc.):

begin = begin.strip().lower()
if begin in ("y", "yes"):
    Start = True
jDo
  • 3,962
  • 1
  • 11
  • 30
0

You could go with your original solution and just change it like so (not recommended):

if begin.strip() == "yes" or begin.strip() == "y" or begin.strip() == "Yes":

Or just check for containment in a tuple:

if begin.strip() in ("yes" , "y" , "Yes" ):

Or even better:

if begin.strip().lower().startswith('y'):

The .strip() deals with any whitespace the user may have inputted.

And you also want to change

Start == True

to

Start = True

Because the former line is an equality test rather than assignment, so in your case Start is always False.

ifma
  • 3,673
  • 4
  • 26
  • 38