Hướng dẫn python restart program if condition - chương trình khởi động lại python nếu có điều kiện

Đây chỉ là một phần từ mã Python của tôi mà tôi muốn khởi động lại từ khi bắt đầu chương trình trong phần 'Khác'. Ai đó có thể vui lòng giúp tôi bằng cách cho tôi biết tôi cần thêm mã nào vào phần 'khác' để làm cho chương trình của tôi khởi động lại từ đầu.

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

hỏi ngày 17 tháng 2 năm 2016 lúc 12:48Feb 17, 2016 at 12:48

1

Không có goto tương đương trong Python cho phép bạn thực hiện những việc như vậy [nhưng sử dụng goto là một thực hành lập trình xấu]. Bạn nên đặt toàn bộ chương trình của mình vào một vòng lặp và chỉ cần thoát vòng lặp bằng break, nếu bạn nhận được giá trị fuelType hợp lệ.

Như thế này:

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

Đã trả lời ngày 17 tháng 2 năm 2016 lúc 13:16Feb 17, 2016 at 13:16

Tony Babarinotony BabarinoTony Babarino

3.2554 huy hiệu vàng30 huy hiệu bạc43 Huy hiệu đồng4 gold badges30 silver badges43 bronze badges

Đặt một vòng lặp xung quanh nó

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

Đã trả lời ngày 17 tháng 2 năm 2016 lúc 13:16Feb 17, 2016 at 13:16

Tony Babarinotony BabarinoSvend Feldt

3.2554 huy hiệu vàng30 huy hiệu bạc43 Huy hiệu đồng4 silver badges17 bronze badges

Đặt một vòng lặp xung quanh nó

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

Svend Feldtsvend FeldtFeb 17, 2016 at 13:20

7584 Huy hiệu bạc17 Huy hiệu đồngRndomcoder

Vì không có tuyên bố 'goto' trong Python, bạn có thể làm điều gì đó như thế này2 silver badges17 bronze badges

Đã trả lời ngày 17 tháng 2 năm 2016 lúc 13:20

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"

RndomCoderrndomCoderFeb 17, 2016 at 13:24

1582 Huy hiệu bạc17 Huy hiệu đồngErba Aitbayev

Thông thường, loại câu lệnh được sử dụng:11 gold badges45 silver badges79 bronze badges

Chủ Đề