-4

Is there a way to use raw input inside an if-statement? I would like to ask the user a question and if they type "yes" I want to code to continue working after the if-statement. If they type "no" I want the code to say "Thank you for your time" and then stop all actions after that particular if-statement. Is this possible?

Code (I have never done this before so this is a wild guess):

    tri=raw_input("Do the points that you entered form a triangle? (yes or no)")  
    tri=str(tri)  

if tri == "yes" or "Yes" or "YES":   
    print "Your triangle is an:"  
elif tri == "no" or "NO" or "No":  
    print "Thank you for your time."  
else:  
    print "Not a valid answer, please try again later." 
  • Definitely possible. Give it a shot and post some code then we can help you out. – tknickman May 01 '16 at 23:45
  • try writing some code and see where you run into problems – dan-man May 01 '16 at 23:45
  • Why not try and see? – John Coleman May 01 '16 at 23:46
  • I entered this code: – Zach Erickson May 02 '16 at 00:07
  • tri=raw_input("Do the points that you entered form a triangle? (yes or no)") tri=str(tri) if tri == "yes" or "Yes" or "YES": print print "Your triangle is an:" elif tri == "no" or "NO" or "No": print print "Thank you for your time." else: print print "Not a valid answer, please try again later." – Zach Erickson May 02 '16 at 00:07
  • wait ignore that, ill post a second question and then put the link onto this one. – Zach Erickson May 02 '16 at 00:08
  • @ZachErickson there's an [edit] button just under the tags for your question. You can edit changes and clarifications into your question instead of posting a new one. – Dan Getz May 02 '16 at 00:20
  • Sorry, new to this website. I already made the other question so here it is: http://stackoverflow.com/questions/36973488/raw-input-in-if-statement – Zach Erickson May 02 '16 at 00:21
  • @ZachErickson: In that case, delete this question. Your other question is different enough that this question is no longer helpful. – zondo May 02 '16 at 00:23

1 Answers1

1

Give this a shot:

tri = None
while tri not in ['yes', 'no']:
    tri=raw_input("Do the points that you entered form a triangle? (yes or no): ").lower()

if tri == 'yes':
    print "Your triangle is an:"
else:
    print "Thank you for your time."
tknickman
  • 4,285
  • 3
  • 34
  • 47