[Tutor] looping - beginner question

leam hall leamhall at gmail.com
Thu Mar 2 10:02:07 EST 2017


On Thu, Mar 2, 2017 at 8:42 AM, Rafael Knuth <rafael.knuth at gmail.com> wrote:

> I wrote a program that is supposed to take orders from customers in a bar.
> If the desired drink is available, the customer will be served. If
> not, he will be informed that the drink is not available. This is what
> I wrote:
>
> bar = ["beer", "coke", "wine"]
>
> customer_order = input("What would you like to drink, dear guest? ")
>
> for drink in bar:
>     if customer_order != drink:
>         print ("Sorry, we don't serve %s." % customer_order)
>     else:
>         print ("Sure, your %s will be served in a minute!" %
> customer_order)
>
> What I want the program to do is to "silently" loop through the list
> of drinks and to print the correct answer. Instead, it loops through
> each item on the list like this:
>
> >>>
> == RESTART: C:/Users/Rafael/Documents/01 - BIZ/Python/Python Code/PPC_4.py
> ==
> What would you like to drink, dear guest? coke
> Sorry, we don't serve coke.
> Sure, your coke will be served in a minute!
> Sorry, we don't serve coke.
> >>>
>
> Rafael, don't forget that your input string might have a newline character
that needs to be cleaned off. I think, can't test at the moment. The
simplest test might be:

if drink in bar:
  <do_stuff>


More information about the Tutor mailing list