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
1

4 Answers 4

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!")
Sign up to request clarification or add additional context in comments.

Comments

0

Put a while loop around it

Condition = True
while(Condition):
    getInput()
    if input == "hest":
         print "its a horse"
    else:
         print "Quiting"
         Condition = False 

Comments

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!")

Comments

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.