0

In my program, the user is supposed to enter a pizza-topping, then press 'enter', at which time a message is printed that says 'We'll add (topping) to your pizza.'

In the case of the user entering an empty string, I want to print the message "Please enter a topping.".

In the case of the user entering 'quit' I want to exit the while-loop.

Currently, if the user enters an empty-string, or 'quit' the program prints the message 'We'll add to your pizza.' Or, We'll add quit to your pizza.', respectively.

prompt = "\nPlease enter the topping you want and press 'enter'"
prompt += "\nType 'quit', then press 'enter' when you're finished:"
active = True
while active:

    topping = raw_input(prompt)
    if topping == '':
        print("Please enter a topping.")

    elif topping == 'quit':
        print("\nThank you.")
        active = False

    else:
        print("\n We'll add {} to your pizza.".format(topping))

I'm sure there is a simple problem with my structure, I just can't seem to find it. Any help is appreciated!

mrm
  • 59
  • 9
  • 1
    Try printing repr(topping). There might be a trailing \n or \r – Blupper Jul 29 '21 at 19:13
  • 2
    Seems fine with input (my python version can't use raw_input) – ᴓᴓᴓ Jul 29 '21 at 19:15
  • 2
    From `raw_input`, it looks like you're writing your code on Python 2, which no longer has official support. If possible, you should consider switching to Python 3 since that's where almost all of the community is these days. – Randy Jul 29 '21 at 19:15
  • 1
    I can't reproduce your problem. When I run your script, it works as it should. (Note that since your script used `raw_input()`, I tested it with Python 2.7.16. It won't work with Python 3.x unless I replace `raw_input` with `input`). – jjramsey Jul 29 '21 at 19:16
  • 1
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – quamrana Jul 29 '21 at 19:21
  • 1
    @Randy updating to Python 3 and running my program using python3 'program name' in thee terminal worked! Didn't realize I was working in python 2. Much appreciated! – mrm Jul 29 '21 at 19:31
  • 1
    Good deal! It's a common issue since Python2 still launches as the default on a lot of OSes. – Randy Jul 29 '21 at 19:33
  • @Randy I've been looking into setting Python3 as the default for when I type 'Python 'program name' in the terminal, but it seems to be an involved process. Is there a simple way to set Python3 as the default when typing Python 'program name'? – mrm Jul 29 '21 at 19:56
  • 1
    The easiest way on a Linux system would be to add `alias python=python3` to your `.bashrc` or `.zshrc` – Randy Jul 30 '21 at 01:45

1 Answers1

1

Your code works fine, and you're likely running into an issue with trailing characters like @Blupper mentioned. If you wanted to implement a simple way of sanitizing your inputs you could refactor topping as:

topping = raw_input(prompt).strip()

The strip would remove any trailing characters.

Shanard
  • 131
  • 6