-1

so this is just basic Python. It uses if, else statements and while loops to create choice based responses. Is there a way to make it more concise? Also, I would really look to know how to make it so if a user inputs something other than 'yes' or 'no' it repeats the questions. Thanks :) !

def func1():
    while True:
        raining = input('Is it raining?: ')

        if raining == 'yes':
            A1 = input('Have an umbrella?: ')

            if A1 == 'no':
                Q1 = 'yes'
                while Q1 == 'yes':
                    print('Wait a while.')
                    Q1 = input('Is it raining? ')

                if Q1 == 'no':
                    print('go outside')

            elif A1 == 'yes':
                print('go outside...')

        elif raining == 'no':
            print('go outside...')

The circuit the code is based on!

  • This doesn't repeat `Is it raining?` if neither `yes` nor `no` is entered? – Scott Hunter Mar 15 '20 at 01:16
  • For your second question, see [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/4518341) – wjandrea Mar 15 '20 at 01:36
  • 1
    Welcome to Stack Overflow! Check out the [tour] and [ask]. You might get better advice on [codereview.se], though you would need to provide context for your code. – wjandrea Mar 15 '20 at 01:48

2 Answers2

1

You should break out of the while loop when you can go outside, put your predefined answers inside a list and instead of storing the questions inside variables you can do this:

yes = ['y','yes']
no = ['n', 'no']


def func1():
    cant_go_outside = True
    while cant_go_outside:
        if input('Is it raining?: ') in yes:
            if input('Have an umbrella?: ') in no:
                while cant_go_outside:
                    print('Wait a while.')
                    if input('Is it raining?: ') in no:
                        print('go outside...')
                        cant_go_outside = False
            else:
                print('go outside...')
                cant_go_outside = False
        else:
            print('go outside...')
            cant_go_outside = False
marcos
  • 4,253
  • 1
  • 9
  • 20
1

You could place the question loops in a general purpose function that returns a boolean. This would allow the rest of the code to use and/or logic directly:

def getAnswer(question):
    while True:
        answer = input(question)
        if answer in ["yes","no"]: return answer == "yes"

def func1():
    while True:
        if  getAnswer("Is it raining?: ") \
        and not getAnswer("Have an umbrella?: "):
            while getAnswer("Wait a while.\nIs it still raining?: "): pass
        print("go outside...")

Another approach is to build "meta-data" (i.e. data that will drive the behaviour of a generic program). This allows you to create a completely different decision tree without writing a new function/program.

def decide(decisions, step="Q1"):
    while True:
        prompt,*nextSteps = decisions[step]
        if "Q" in step: answer = input(prompt+": ")
        else:           answer = print(prompt) or "no"
        if answer not in ["yes","no"]: continue
        step = nextSteps[answer=="yes"]

In this case, the meta-data is a dictionary of steps that can be yes/no questions or simple printed statements. Each step indicates what is the next step either as a choice between two (for questions) or a single next step (for statements).

example1:

rainyDay = {  "Q1":("Is it raining?","P1","Q2"),
              "P1":("go outside...","Q1"),
              "Q2":("Have an umbrella?","P2","P1"),
              "P2":("Wait a While.","Q3"),
              "Q3":("Is it still raining?","P1","P2")
            }
decide(rainyDay)

Is it raining?: no
go outside...
Is it raining?: r
Is it raining?: yes
Have an umbrella?: yes
go outside...
Is it raining?: yes
Have an umbrella?: no
Wait a While.
Is it still raining?: no
go outside...
Is it raining?: yes
Have an umbrella?: yes
go outside...
Is it raining?:

example2:

happiness = { "Q1":("Do you have a problem ?","P1","Q2"),
              "P1":("Be Happy!","Q1"),
              "Q2":("Can you do something about it","P1","P2"),
              "P2":("Do It!","Q1"),
            }
decide(happiness)

Do you have a problem ?: no
Be Happy!
Do you have a problem ?: yes
Can you do something about it: no
Be Happy!
Do you have a problem ?: yes
Can you do something about it: yes
Do It!
Do you have a problem ?: no
Be Happy!
Do you have a problem ?:
Alain T.
  • 34,859
  • 4
  • 30
  • 47