lundi 30 octobre 2017

Python 3 Check for exact input and repeat until correct answer given [duplicate]

This question already has an answer here:

I have never asked a question as it makes me nervous and I'm a potato. I'm very new and feel intimidated asking but I want to code and I can't because I'm stuck. I'm trying to make a text based game. this game requires specific input. and I'm trying to def a block of code specifically for that but I'm coming up with nothing.

So anyway, Here's what I'm trying to do. First there's user input. just a single word to make things easier on myself. based on what they type the program will do one of several different things. To prevent the program from crashing I need input to be specific or either the program will crash, or nothing will happen, or it will loop forever. Now I've tried looking up answers, I tried coming up with my own, I've looked all through stack overflow, I tried youtube... So this is what I've tried, and nothing has worked and for all intents and purposes... it seriously looks to me like it should work... why is it not working?! :'(

while True:
    print('what direction?')
    path1 = input()
    path = path1.lower()
    if path1 != 'south':
        print('north or south?')
    elif path1 != 'north':
        print('north or south?')
    elif path == 'north':
        print(path)
        break
    elif path == 'south':
        print(path)
        break

(doesn't work just loops)

while True:
    print('what direction?')
    direc = input()
    direc = direc.lower()
    if direc in x:
        print('you chose ' + direc)
        break
    elif direc not in 'abcdefghijklmnopqrstuvwxyz':
        print('letters only')
    else:
        break

(ends up accepting any input except it does print 'letters only' if a number is inputted)

The rest were kind of variations of this, I also tried to put north and south in a list and do it that way but it didn't work either. Please help me figure out what the best way is to define a function that forces the player to pick a specific option otherwise repeat the question until correct input is provided. <3 Please and thank you to everyone.

Aucun commentaire:

Enregistrer un commentaire