-4

This is my code

print('Hello There, Welcome to EmoBot,Your personal diary assistant')
from six.moves import input as raw_input
ans=True
while ans:
    print("""
    1.I am happy
    2.I feel sad
    3.I feel angry
    """)
    ans=raw_input("How can I help you?(Choose 1,2 or3)")
    if ans=="1":
      print("\n Oh! That is Wonderful")
      a=True
      while a:
        print("""
        p.It is work-related
        q.It is home-related
        r.It is school-related
        s.It is my personal-life related
        """)
        a=raw_input('What is the reason for your happiness?(Choose p,q,r or s)')
        a=None
        if a=="p":
            print('\n Oh Cool! Always remember to stay focused in work. Good Luck') 
        elif a=="q":
            print('\n Nice! Family is the one stop for all the emotions in life')
        elif a=="r":
            print('\n Great! School is the place full of experiences, once you live it you cant relive it. So enjoy your days at school')
        else:  
            print('\n Epic! The happiness that you get when you achieve something that you wanted to achieve for long is the greatest')
ans=None

This is the Output

Hello There, Welcome to EmoBot,Your personal diary assistant

    1.I am happy
    2.I feel sad
    3.I feel angry
    
How can I help you?(Choose 1,2 or3)1

 Oh! That is Wonderful

        p.It is work-related
        q.It is home-related
        r.It is school-related
        s.It is my personal-life related
        
What is the reason for your happiness?(Choose p,q,r or s)p

 Epic! The happiness that you get when you achieve something that you wanted to achieve for long is the greatest

Here, it was supposed to show the result in "p" variable but instead, it showed the one in "s".

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Jack
  • 1
  • 1
  • 1
    remove `a=None`, this sets `a` variable to none, making the program go to `else` in the condition check – pxDav Sep 01 '23 at 17:01
  • I tried shifting it to the end and it worked, thanks – Jack Sep 01 '23 at 17:02
  • Welcome to Stack Overflow! Please take the [tour]. SO is a Q&A site, but there's no question here. Please read [ask]. For debugging help, please make a [mre], meaning only enough code to demonstrate the problem; most of this code is irrelevant. – wjandrea Sep 01 '23 at 17:05
  • 1
    Shifting it to the end? Why not just remove it? Is it supposed to be breaking the loop when the user input is valid? Cause it's not actually doing that. Check out [Asking the user for input until they give a valid response](/q/23294658/4518341). – wjandrea Sep 01 '23 at 17:11
  • Oh Ok Thank you! – Jack Sep 01 '23 at 17:38
  • 1
    If you're new to programming why do you use `six`? `six` was intended to help with the migration from Python 2 to Python 3, but since Python 2 is dead for over 3 years I don't assume you have written any code for it. – Matthias Sep 01 '23 at 17:43
  • @Matthias possibly something cargo-culted from an instructor? – Karl Knechtel Sep 01 '23 at 20:44

2 Answers2

-1

You set a to None after reading raw input, and you print the 's' answer if a does not match any other:

    a=raw_input('What is the reason for your happiness?(Choose p,q,r or s)')
    a=None # Here you have set a to none
    if a=="p":
        print('\n Oh Cool! Always remember to stay focused in work. Good Luck') 
    elif a=="q":
        print('\n Nice! Family is the one stop for all the emotions in life')
    elif a=="r":
        print('\n Great! School is the place full of experiences, once you live it you cant relive it. So enjoy your days at school')
    else:  # As None fails above tests, the else condition executes
        print('\n Epic! The happiness that you get when you achieve something that you wanted to achieve for long is the greatest')
Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • Thanks, i shifted a=None to the last and it worked – Jack Sep 01 '23 at 17:07
  • 1
    Please read [answer] and do not use the answer section to point out obvious typos. Such questions should be closed and not answered; answering them interferes with the cleanup process. Unless you think the question is sincerely "why does writing `a = None` cause `a` to stop being the string that was input on the previous line?", in which case I don't know how anyone can answer that - because there is no way to understand the reason for expecting anything else. – Karl Knechtel Sep 01 '23 at 18:14
-1

You have assigned a=None after requesting for second user input in line a=raw_input('What is the reason for your happiness?(Choose p,q,r or s)'). This causes the value to a to be processed as None regardless of what input the user provides. Therefore, the condition for else executed resulting in the output that you are not expecting. You can simply remove the line a=None to fix this.