Вот что я понимаю, если я ошибаюсь:
1. получить значение для поверхности
2. если (поверхность недействительна) запросить другое значение
3. если (поверхность ледяная):
а. получить значение для u
б. получить значение для
с. рассчитать с помощью формулы
s1 = u**2
s2 = 2*.08*9.8
s = s1/s2
д. дисплей с
4. Если поверхность не айс, ничего не делайте (согласно опубликованному коду, но ее можно отредактировать так, чтобы в ней были похожие блоки, например if surface == "soil"
)
Если так, то
def terrain():
surface = raw_input("What surface will you be driving on? ")
valid_surface = ["ice", "concrete", "soil", "asphalt"]
while surface not in valid_surface:
surface = raw_input("Surface not valid. Please enter a valid surface: ")
if surface == "ice":
u = raw_input("what is the velocity of the car in meters per second?")
while int(u) < 0:
u = raw_input("Velocity must be a positive integer: ")
while int(u) == 0:
u = raw_input("Velocty must be a number greater than zero: ")
while int(u) > 72:
u = raw_input("This vehicle cannot reach this speed: ")
a = raw_input("How quickly is the vehicle decelerating? ")
a = int(a)
while int(a) > 0:
a = raw_input("Deceleration cannot be a positive integer: ")
while int(a) < -55: #note all while blocks are at same level
#Do stuff #contents inside while is indented by 4 spaces
a = raw_input("This vehicle cannot have this deceleration. Please input another value: ")
s1 = u**2
s2 = 2*.08*9.8
s = s1/s2
print "This is how far the vehicle will travel on ice: "
print ("The vehicle will travel %i meters before coming to a complete stop" % (s))
Я обычно определяю функцию как:
def get_int(prompt, error):
variable = input(prompt)
while True:
try:
variable = int(variable)
except ValueError:
variable = input(error)
continue
break
return variable
и используйте его как:
v1 = get_int("Enter value for accn: ", "Please input integer: ")
v2 = get_int("Enter value for velocity: ", "Please input integer: ")
Вам может потребоваться изменить некоторые вещи, например, изменить variable = int(variable)
на variable = float(variable)
, если вам нужно принять плавающее (например, 1,25) значение.