0

I have an issue with my codes. I received an output meant for an if statement whenever the rice_ingredients = input("How many ingredients: ") is > 8 instead of the output of else statement which should be "Too much ingredients, unprofessional!"

def Chef() -> int:
    rice_ingredients = input("How many ingredients: ")
    for ingredients in rice_ingredients:
        if ingredients <= str(6):
            print("Professional")
        elif ingredients == str(7) or str(8):
            print("Well, can still be considered professional")
        else:
            print("Too much ingredients, unprofessional!")
        exit()
Hass786123
  • 666
  • 2
  • 7
  • 16
  • 2
    `elif ingredients == str(7) or str(8):` == `elif (ingredients == str(7)) or (str(8)):`. You need to do either `elif ingredients == str(7) or ingredients == str(8):` or `elif ingredients in [str(7), str(8)]:` – h4z3 Dec 06 '19 at 11:17

3 Answers3

3

This:

elif ingredients == str(7) or str(8):

is different from:

elif ingredients == str(7) or ingredients == str(8):

and this:

elif ingredients == (str(7) or str(8)):

The second form is probably the logic you want to implement, which could also be written as:

elif any(ingredients == str(x) for x in (7, 8)):

or:

elif ingredients in {str(7), str(8)}:

EDIT

Also, perhaps you want to check the code as a whole:

  • Chef() does not follow PEP8 naming conventions and does not return a int (contrarily to your annotation).
  • rice_ingredients contains a string which should contain a single number (based on the question you ask "How many ingredients: "), looping through it will cause you to consider each character of the input separately, so that if you type 123, ingredients will be 1, 2 and 3 at each iteration. Likely, what you want to do is convert rice_ingredients to int, e.g. rice_ingredients = input("How many ingredients: ") to become rice_ingredients = int(input("How many ingredients: "))
  • now that rice_ingredients is a int you do not need to compare it against strings
  • exit() will completely exit the interpreter, if you just want to quit the function use return instead. Note that is is not needed to explicitly exit from the function at the end of it -- it will automatically do so
  • the logic of the if-elif-else clause can be simplified further

A possible cleaned up code would look like:

def chef():
    rice_ingredients = int(input("How many ingredients: "))
    if rice_ingredients <= 6:
        print("Professional")
    elif rice_ingredients <= 8:
        print("Well, can still be considered professional")
    else:
        print("Too much ingredients, unprofessional!")
Community
  • 1
  • 1
norok2
  • 25,683
  • 4
  • 73
  • 99
1

I am not sure if I understood your question, but there is a logial error:

def Chef() -> int:
    rice_ingredients = input("How many ingredients: ")
    for ingredients in rice_ingredients:
        if ingredients <= str(6):
            print("Professional")
        #elif ingredients == str(7) or str(8): please note
        elif ingredients == str(7) or ingredients == str(8):
            print("Well, can still be considered professional")
        else:
            print("Too much ingredients, unprofessional!")
        exit()

Please also note that in this case you should consider a different check eg (< and >) or a in

ghovat
  • 1,033
  • 1
  • 12
  • 38
1

This can also solve your problem. As essentially you want to compare number, converting to integer is logical.

def Chef() -> int:
    rice_ingredients = input("How many ingredients: ")
    for ingredients in rice_ingredients:
        value = int(ingredients)
        if value < 7:
            print("Professional")
        elif value < 9:
            print("Well, can still be considered professional")
        else:
            print("Too much ingredients, unprofessional!")