0

This is just a section from my python code that I want to restart from the start of the program in the 'else' section. Can somebody please help me by telling me what code i need to add in the 'else' section to make my program restart from the beginning.

Thanks.

if fuelType == "Petrol":
    minPetrolPrice = min(list1)
    minPetrolPriceIndex = list1.index(min(list1))
    minPetrolPriceName = namelist[minPetrolPriceIndex]

    print("The cheapest price of petrol today is: "), minPetrolPrice
    print("")

    print("This can be found at"),minPetrolPriceName, ("petrol station ")
    print("The average price of petrol at all the stations today is:"),avgPetrol
    print("Just in case you were intersted, the average price of diesel today is:"),avgDiesel

elif fuelType == "Diesel":
    minDieselPrice = min(list2)
    minDieselPriceIndex = list2.index(min(list2))
    minDieselPriceName = namelist[minDieselPriceIndex]

    print("The cheapest price of diesel today is: "), minDieselPrice
    print("")

    print("This can be found at"),minDieselPriceName, ("petrol station ")
    print("The average price of diesel at all the stations today is:"),avgDiesel
    print("Just in case you were intersted, the average price of petrol today is:"),avgPetrol

else:
    print("You did not enter a valid option")
    print("Please try again!")
#I want it to restart the whole program from this point
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
M.C
  • 11
  • 1
  • 1
  • 4
  • Maybe have a lookt at: http://stackoverflow.com/questions/20337489/python-how-to-keep-repeating-a-program-until-a-specific-input-is-obtained and http://stackoverflow.com/questions/8114355/loop-until-a-specific-user-input – zinjaai Feb 17 '16 at 13:15

4 Answers4

1

There is no goto equivalent in Python which would allow You to do such things (but using goto is a bad programming practice anyway). You should put your whole program in a loop and just exit the loop using break, if You get valid fuelType value.

Like this:

while True:
    if fuelType == "Petrol":
        minPetrolPrice = min(list1)
        minPetrolPriceIndex = list1.index(min(list1))
        minPetrolPriceName = namelist[minPetrolPriceIndex]

        print("The cheapest price of petrol today is: "), minPetrolPrice
        print("")

        print("This can be found at"),minPetrolPriceName, ("petrol station ")
        print("The average price of petrol at all the stations today is:"),avgPetrol
        print("Just in case you were intersted, the average price of diesel today is:"),avgDiesel
        break
    elif fuelType == "Diesel":
        minDieselPrice = min(list2)
        minDieselPriceIndex = list2.index(min(list2))
        minDieselPriceName = namelist[minDieselPriceIndex]

        print("The cheapest price of diesel today is: "), minDieselPrice
        print("")

        print("This can be found at"),minDieselPriceName, ("petrol station ")
        print("The average price of diesel at all the stations today is:"),avgDiesel
        print("Just in case you were intersted, the average price of petrol today is:"),avgPetrol
        break
    else:
        print("You did not enter a valid option")
        print("Please try again!")
Tony Babarino
  • 3,355
  • 4
  • 32
  • 44
0

Put a while loop around it

Condition = True
while(Condition):
    getInput()
    if input == "hest":
         print "its a horse"
    else:
         print "Quiting"
         Condition = False 
Svend Feldt
  • 758
  • 4
  • 17
0

As there is no 'goto' statement in python, you can do something like this

i=0
While i==0:
    if fuelType == "Petrol":
        i=1
        minPetrolPrice = min(list1)
        minPetrolPriceIndex = list1.index(min(list1))
        minPetrolPriceName = namelist[minPetrolPriceIndex]

        print("The cheapest price of petrol today is: "), minPetrolPrice
        print("")

        print("This can be found at"),minPetrolPriceName, ("petrol station ")
        print("The average price of petrol at all the stations today is:"),avgPetrol
        print("Just in case you were intersted, the average price of diesel today is:"),avgDiesel

    elif fuelType == "Diesel":
        i=1
        minDieselPrice = min(list2)
        minDieselPriceIndex = list2.index(min(list2))
        minDieselPriceName = namelist[minDieselPriceIndex]

        print("The cheapest price of diesel today is: "), minDieselPrice
        print("")

        print("This can be found at"),minDieselPriceName, ("petrol station ")
        print("The average price of diesel at all the stations today is:"),avgDiesel
        print("Just in case you were intersted, the average price of petrol today is:"),avgPetrol

    else:
        print("You did not enter a valid option")
        print("Please try again!")
Rndomcoder
  • 158
  • 2
  • 18
0

Typically, that kind of statements used:

while True:
    a = raw_input('Type anything. N for exit. \n')
    if a == "Petrol":
        print "Petrol"
    elif a == "Diesel":    
        print "Diesel"        
    elif a == 'N': break
    else: 
        print "Wrong. Try again"
Erba Aitbayev
  • 4,167
  • 12
  • 46
  • 81