-1

I'm new to Python and I'm trying to create a very specific kind of loop. Nothing I read has really helped me. Maybe because it's not what I mean, or maybe because I don't understand. I'll try asking and hope someone understands me. I'm basically just trying to replay the four lines from the top.

def defoption():
   option = ("Give an input") #<- Trying to replay this line
   option_input = input("Input: ") #<- and then this
   if (option_input == 'a'):
       print("Option a works")
   else:
       return option and print("Option unavailable")
defoption()
Alex Pshenko
  • 167
  • 8
maa
  • 5
  • 4

2 Answers2

0
def defoption():
   print("Give an input") 
   option_input = input("Input: ")
   return option_input

opt = defoption()
while( opt not in ['a'] ):
   print("Option unavailable") 
   opt = defoption()
print( "Option {} works!".format(opt))
corn3lius
  • 4,857
  • 2
  • 31
  • 36
-1

What about this:

def defoption():
   print("Give an input") #<- Trying to replay this line
   option_input = input("Input: ") #<- and then this
   if option_input == 'a':
       print("Option a works")
   else:
       print("Option unavailable")
       return defoption()

defoption()
mrCarnivore
  • 4,638
  • 2
  • 12
  • 29
  • Woah, I've been working on this for 3 hours and I didn't even try this... *biggest facepalm* Thank you very much for pointing it out – maa Oct 19 '17 at 13:53
  • Nice to hear that it worked! Wondering why anybody would downvote my answer though... – mrCarnivore Oct 19 '17 at 14:03
  • It's a bad idea to answer duplicates, especially when the [answer to the linked question](https://stackoverflow.com/a/23294659/3650362) is clearly better and even *explains why your code-only answer is flawed* – trent Oct 19 '17 at 14:08
  • Since you posted the link to the duplicate question after I have provided my answer I am not sure why I deserve a downvote for providing an answer that clearly helped somebody... – mrCarnivore Oct 19 '17 at 14:09
  • Sorry, I didn't find this thread before I posted mine. Only afterwards it suggested it for some reason... Nevertheless, I'm just writing some small scripts to figure out how the language works and I'm very thankful mrCarnivore provided an answer that helps me right now. – maa Oct 19 '17 at 14:16
  • @mwaning: since you are new here: you could also upvote my answer if you do not agree with trentcl. – mrCarnivore Oct 19 '17 at 14:19
  • I tried a few times actually, but it says "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." so the first time I was kind of confused when it changed to -1, haha. But sorry, not sure how to upvote you right now... – maa Oct 19 '17 at 14:36
  • @mrCarnivore Don't make assumptions. Someone else downvoted you; it was -1 when I wrote my comment. – trent Oct 19 '17 at 15:32
  • @mrCarnivore You may enjoy (or not) reading [this answer on Meta](https://meta.stackoverflow.com/a/344624/3650362), particularly starting at "Altogether though..." – trent Oct 19 '17 at 15:59
  • @trentcl: Sorry for the false accusations. It just seemed too fitting. I read the article you posted and take your point. – mrCarnivore Oct 19 '17 at 16:02