0

I have a simple ice cream program, everything works. But how do I loop a single input like scoops if the input is invalid? Currently if the input is incorrect it re-asks the user for the first input again. Meaning if I enter vanilla for the flavor and 7 for the number of scoops, it will print the error message, then instead of re-asking how many scoops you'd like, it asks you what flavor you'd like. How would I fix this for my three inputs throughout the program? I would still like the program to restart after a valid order is entered. Thank you for your time.

Program code:

#Error for wrong flavor selection
class FlavorError(Exception):
    def __init__(self, flavors, message):
        Exception.__init__(self, flavors, message)
        self.flavors = flavors

#Error for wrong number of scoops selection
class ScoopsError(Exception):
    def __init__(self, scoops, message):
        Exception.__init__(self, scoops, message)
        self.scoops = scoops

#Error for wrong carrier selection
class HoldingsError(Exception):
    def __init__(self, holdings, message):
        Exception.__init__(self, holdings, message)
        self.holdings = holdings


def main():
    #Creating Values
    flavors= " "
    scoops = " "
    holdings = " "
    while True:
        try:
            #MENU / INFORMATION
            print("Flavors: vanilla, chocolate, strawberry, mint, pistachio, and spumoni")
            print("Number of scoops: 1, 2, 3")
            print("Ways to eat: bowl or cone")
            print()
            #FLAVOR INPUT
            flavors = input("Enter a flavor of ice cream: ").lower()
            print()
            #ERROR CHECK
            if flavors not in ['vanilla', 'chocolate', 'strawberry', 'mint', 'pistachio', 'spumoni']:
                raise FlavorError(flavors, "is not on the menu.")
            #NUMBER OF SCOOPS INPUT
            scoops = int(input("Enter the number of scoops: "))
            print()
            #ERROR CHECK
            if scoops > 3 or scoops <= 0:
                raise ScoopsError(scoops, 'We do not offer that many scoops!')
            #CARRIER INPUT
            holdings = input("Would you like a cone or bowl? ")
            print()
            #ERROR CHECK
            if holdings not in ['bowl', 'cone']:
                raise HoldingsError(holdings, 'Please select between a bowl or a cone')
            print(scoops , "scoops of" , flavors , "in a" , holdings)
            print()
        except Exception as e:
            print("Invalid choice! try again!"+str(e))
            print()

main()
  • Welcome to StackOverflow! Could you clarify on what you mean by `restarts the entire program`? Does the program error out? Thanks! – Axiumin_ Nov 09 '20 at 23:25
  • First of all, you don't necessarily need to raise an exception for the wrong number of scoops. Just a message and a new prompt will suffice. – NotAName Nov 09 '20 at 23:32
  • I just editted my post, I hope it is more clear. – George Davidson Nov 09 '20 at 23:34
  • In your own words, how are you deciding which lines of your program should be within your `while` loop and which should not? In your own words, how do you signal to Python which lines are within the `while` loop and which are not? In your own words, what lines should go inside a loop in order to get the behaviour you want? So, just put it together. This is not really a programming question, it's a reasoning question; and the best way to get through it is to practice doing the reasoning yourself. – Karl Knechtel Nov 09 '20 at 23:40

2 Answers2

0

There are many ways to do this, but I believe this will work for you and you are already using similar logic for your flavors.

If you need more complexity, the link at the top is what you want.

while scoops not in [1,2,3]:
    scoops = int(input("Enter the number of scoops: "))
Boomer
  • 464
  • 3
  • 10
0

To achieve the required behavior you may surround each input with while loop.

and break the loop only if the input is valid so it will continue executing the program