循环以添加更多输入

我还有3辆车,但我需要知道如何循环诸如汽车输入之类的东西,这样如果你对附加功能做了错误的输入,它将允许你再次输入它,所以它们必须是1或0。


print("===================================================") 

print("==============Car Finance Calculations=============") 

print(" Choose your veicle: ")

print(" SUV type 1 ")

print(" Hatch type 2 ")

print(" Sedan type 3 ")

print("===================================================")

caaaaaaaar = int(input(" Please enter Which car you want to finance: "))

years = int(input(" enter years of loan either 3 or 4 or 5 Years: "))      

if caaaaaaaar == 1:                                             

    carsvalue = int(input("Please enter you cars value: "))    

    residual = carsvalue * 0.30                                               

    financing = carsvalue - residual                            

    print(" financing value for the car is: ", round(financing,2))      

    print(" Intrest rate is 9% ")                               

    n = years * 12                                              

    r = 9 / (100 * 12)                                         

    Financingcost = financing * ((r*((r+1)**n))/(((r+1)**n)-1)) 

    print(" Your Montly financing rate is: ", round(Financingcost,2))    

    print("================================================================================")

    print("Please Choose extras: ")                                   

    print("======================================================")

    print(" If you would like fuel type 1 if not type 0")

    print(" If you would like insurance type 1 if not type 0")

    print(" if you would like Maintenance type 1 if not type 0")




LEATH
浏览 113回答 4
4回答

梵蒂冈之花

您希望使用 while 循环。喜欢这个:carsEntered = 0while (carsEntered <= 4):&nbsp; &nbsp; caaaaaaaar = int(input(" Please enter Which car you want to finance: "))&nbsp; &nbsp; years = int(input(" enter years of loan either 3 or 4 or 5 Years: "))&nbsp; &nbsp; carsEntered += 1您也可以改用循环,但这取决于您要执行的操作。for

摇曳的蔷薇

根据我对问题的理解,您希望运行迭代,直到用户给出正确的答案。在这种情况下,可以在 中使用 标志变量。whileflag = Falsewhile(flag is False):&nbsp; &nbsp; if(condition_statisfied):&nbsp; &nbsp; &nbsp; &nbsp; flag = True

杨魅力

我的建议是采取一种功能性方法来解决这个问题。您正在多次调用,并希望在每次继续下一行之前验证用户输入。您可以在一个函数中执行此操作,该函数将继续循环,直到确认输入有效。验证输入后,可以通过使用关键字返回值来退出函数的帧。int(input())return# This is the function definition.&nbsp;# The code in this function does not execute until the function is called.def prompt_int(&nbsp; &nbsp; &nbsp; &nbsp; text,&nbsp; &nbsp; &nbsp; &nbsp; minimum=0,&nbsp; &nbsp; &nbsp; &nbsp; maximum=1,&nbsp; &nbsp; &nbsp; &nbsp; error="The value you entered is not valid try again."):&nbsp; &nbsp; # when the function is called it will start a second while loop that will keep looping&nbsp; &nbsp; # until valid input is entered&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val = int(input(text))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if minimum <= val <= maximum:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return val&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise ValueError&nbsp; &nbsp; &nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(error)# This is the outer while loop. It will keep looping forever if the user so chooses.while True:&nbsp; &nbsp; # instead of calling print() a bunch one option is to assign a docstring to a variable&nbsp; &nbsp; car_prompt = '''=================================================================Car Finance Calculations=============Choose your veicle:SUV: 1Hatch: 2Sedan: 3===================================================Please enter the number for the car you want to finance or 0 to exit:\n'''&nbsp; &nbsp; # This is where we first call the function.&nbsp;&nbsp; &nbsp; # Our docstring will be passed into the function to be used as a prompt.&nbsp; &nbsp; # We also pass in some args for maximum and minimum params&nbsp; &nbsp; caaaaaaaar = prompt_int(car_prompt, 0, 3)&nbsp; &nbsp; # 0 is false so we can exit the while loop if the user enters 0&nbsp; &nbsp; if not caaaaaaaar:&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; year_prompt = "Enter years of the car loan (3, 4 or 5):\n"&nbsp; &nbsp; years = prompt_int(year_prompt, 3, 5)&nbsp; &nbsp; if caaaaaaaar == 1:&nbsp; &nbsp; &nbsp; &nbsp; val_prompt = "Please enter you cars value:\n"&nbsp; &nbsp; &nbsp; &nbsp; carsvalue = prompt_int(val_prompt, 0, 2147483647)&nbsp; &nbsp; &nbsp; &nbsp; residual = carsvalue * 0.30&nbsp; &nbsp; &nbsp; &nbsp; financing = carsvalue - residual&nbsp; &nbsp; &nbsp; &nbsp; print("Financing value for the car is: ", round(financing, 2))&nbsp; &nbsp; &nbsp; &nbsp; print("Intrest rate is 9% ")&nbsp; &nbsp; &nbsp; &nbsp; n = years * 12&nbsp; &nbsp; &nbsp; &nbsp; r = 9 / (100 * 12)&nbsp; &nbsp; &nbsp; &nbsp; Financingcost = financing * ((r * ((r + 1) ** n)) / (((r + 1) ** n) - 1))&nbsp; &nbsp; &nbsp; &nbsp; print(" Your Montly financing rate is: ", round(Financingcost, 2))&nbsp; &nbsp; &nbsp; &nbsp; print("================================================================================")&nbsp; &nbsp; &nbsp; &nbsp; print("Please Choose extras: ")&nbsp; &nbsp; &nbsp; &nbsp; print("======================================================")&nbsp; &nbsp; &nbsp; &nbsp; x = prompt_int("If you would like fuel type 1 else type 0:\n")&nbsp; &nbsp; &nbsp; &nbsp; y = prompt_int("If you would like insurance type 1 else type 0:\n")&nbsp; &nbsp; &nbsp; &nbsp; z = prompt_int("If you would like Maintenance type 1 else type 0:\n")&nbsp; &nbsp; &nbsp; &nbsp; print("======================================================")&nbsp; &nbsp; &nbsp; &nbsp; if x == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Yes you want fuel")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fuelcost = 80 * 4.33&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("fuel cost is", round(fuelcost, 2))&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("you dont want fuel as an extra")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fuelcost = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Fuel cost is: ", fuelcost)&nbsp; &nbsp; &nbsp; &nbsp; print("=======================================")&nbsp; &nbsp; &nbsp; &nbsp; if y == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("yes you want insurance")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; insurancecost = (1200 / 12)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Insurance cost is: ", round(insurancecost, 2))&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("you dont want insurance")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; insurancecost = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("insurance cost is: ", insurancecost)&nbsp; &nbsp; &nbsp; &nbsp; print("=======================================")&nbsp; &nbsp; &nbsp; &nbsp; if z == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("yes you want maintenance")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maintenancecost = (100 * 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Maintenance cost is: ", round(maintenancecost, 2))&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("you dont want maintenance")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maintenancecost = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("maintenance cost is: ", maintenancecost)&nbsp; &nbsp; &nbsp; &nbsp; print("=======================================")&nbsp; &nbsp; &nbsp; &nbsp; total_cost_for_extras = fuelcost + maintenancecost + insurancecost&nbsp; &nbsp; &nbsp; &nbsp; print("Total cost for the selected extras is: ", round(total_cost_for_extras, 2))&nbsp; &nbsp; &nbsp; &nbsp; TOTALFOREVERYTHING = total_cost_for_extras + Financingcost&nbsp; &nbsp; &nbsp; &nbsp; print("Total monthly financing rate is: ", round(TOTALFOREVERYTHING, 2))&nbsp; &nbsp; elif caaaaaaaar == 2:&nbsp; &nbsp; &nbsp; &nbsp; # Put your code for car 2 in this block.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; # If it is like car 1 code except the value of a few variables&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; # then make another func&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; # Car 3 code...&nbsp; &nbsp; &nbsp; &nbsp; pass

神不在的星期二

我建议你把你的车辆类型放在字典里:vehicle_type_dict = {&nbsp; &nbsp; 1: "SUV type",&nbsp; &nbsp; 2: "Hatch type",&nbsp; &nbsp; 3: "Sedan type"}并执行 while 循环以检查您的输入是否在字典中:while True:&nbsp; &nbsp; caaaaaaaar = int(input(" Please enter Which car you want to finance: "))&nbsp; &nbsp; if caaaaaaaar not in vehicle_type_dict:&nbsp; &nbsp; &nbsp; &nbsp; continue #loop again if input not in the dictionary&nbsp; &nbsp; #read next line of code if input is in the dictionary&nbsp; &nbsp; #do something below if input is correct&nbsp; &nbsp; years = int(input(" enter years of loan either 3 or 4 or 5 Years: "))&nbsp; &nbsp; break #end loop
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python