我试图做到这一点,这样我的程序会不断地要求用户输入某个值,如果用户不输入,它会一直询问直到他们输入
我试着用“while”而不是“if”,但我知道我可能遗漏了什么
def terrain(surface):
surface = raw_input("What surface will you be driving on? ")
if surface == "ice":
u = raw_input("what is the velocity of the car in meters per second? ")
u = int(u)
if u < 0:
u = raw_input("Velocity must be greater than 0")
return
if u == 0:
u = raw_input("Velocty must be a number greater than zero")
return
a = raw_input("How quickly is the vehicle decelerating? ")
a = int(a)
if a > 0:
print ("Deceleration cannot be a positive integer")
return
else:
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))
terrain("ice")
问题是,在检查导致函数返回
None
的条件之后,您使用的是return
,必须使用break
而不是带有while循环的return
来实现这一点。下面是验证和获取数据的更好方法上面的代码所做的是对
params
列表中的每一个param
执行while循环检查在其验证器中定义的每一个验证条件,如果有效,它将中断while循环并继续到下一个param
并再次重复相同的过程相关问题 更多 >
编程相关推荐