Python-基于用户输入在IF语句中重启程序

时间:2016-02-17 12:48:29

标签: python

这只是我的python代码中的一部分,我想从“#else;'”中的程序开始重新启动。部分。有人可以通过告诉我需要在“其他”中添加哪些代码来帮助我。从一开始就重新启动程序的部分。

感谢。

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

4 个答案:

答案 0 :(得分:0)

围绕它放一个循环

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

答案 1 :(得分:0)

Python中没有goto等价物允许你做这些事情(但是使用goto是一个糟糕的编程习惯)。如果您获得有效的break值,则应该将整个程序放在一个循环中,然后使用fuelType退出循环。

像这样:

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

答案 2 :(得分:0)

由于python中没有'goto'语句,你可以这样做

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

答案 3 :(得分:0)

通常,使用的是这种陈述:

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"