-2

I want to return the if statements if they equal the value of the dictionary and print the dictionary value not just use == 'HW' and print some text. Right Now I return nothing So I am wondering what I did wrong. How do I get out of the while loop when I give a wrong answer first and then a correct one?

from ex45f import live
class adult(live):

    def __init__(self, choose):
        self.choose = choose

    def choices(self):
        options = {
            'HW': 'Hard Working',
            'PA': 'Partying',
            'DE': 'Doing everyting a bit',
            }
#while choose != 'HW' or 'PA' or 'DE':
        while not (self.choose == 'HW' or self.choose == 'PA' or self.choose == 'DE'):
            x = input("""Choose again
> """)
            print(x)
        if self.choose == options['HW']:
            return "You are going to be millionare"
        elif self.choose == options['PA']:
            return "You will have the first year a great life and then you will hate it"
        elif self.choose == options['DE']:
            return "Nothing intersting in life happens."
        else:
            return "Wrong input"

choose = input("""Choose one of those options: HW, PA, DE)
> """)
zw = adult(choose)
zw.choices()
ilearnthis
  • 13
  • 3
  • Does this answer your question? [Check if a given key already exists in a dictionary](https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary) – mkrieger1 Sep 30 '20 at 21:06
  • Although I don’t quite understand what your question is. – mkrieger1 Sep 30 '20 at 21:07
  • In your `while` loop you're checking `self.choose` but you're only updating `x`. – khelwood Sep 30 '20 at 21:15

1 Answers1

0

So a couple of comments:

  1. self.choose will either be HW, PA, or DE your if statemen checks if self.choose == options['HW'].
    options['HW'] is really "Hard Working", so in the case above self.choose will always end at the else.
    Your if statements should be:
    if self.choose == "HW":
    if self.choose == "PA":
    if self.choose == "DE":

  2. Your while loop could look like: while self.choose not in options:

  3. if self.choose is not in the list, you get a new input and store it in x. self.choose remains unchanged. Thus the while loop will be infinite.
    instead of x = input do self.choose = input so when the user enters one of the correct options, they will leave the while loop.

  4. If you want to get the value of options add return "{} The text to return".format(options[self.choose])

  5. Finally, if you have no interest in the terms Hard Working, Partying, or Doing everthing a bit, then just make options a list:
    options = ["DE", "PA", "HW"] Then you do not need options[self.choose]



I hope that helped!

Aryeh K
  • 311
  • 1
  • 2
  • 8
  • Thank you so much it helped me a lot! The only question I still have is that if you want to get the value of options add return "{} The text to return".format(options[self.choose]) doesn't work. I had to change it into print("{} The text to return".format(options[self.choose])). Why doesn't return work? – ilearnthis Oct 01 '20 at 12:12
  • Because return only sends it back to the caller. you can either do what you did and put print in the function, or you can try: print(zw.choices()) – Aryeh K Oct 29 '20 at 15:06