我的循环缩进看起来很奇怪,这正常吗? (python 2)
My indents for my loops look strange, is this normal? (python 2)
我正在尝试在满足某些条件时创建循环,到目前为止它可以工作,但是当我尝试创建更多条件时,我必须进一步缩进它们以使我的程序工作。
def terrain(surface):
surface = raw_input("What surface will you be driving on? ")
while surface != "ice" and surface != "concrete" and surface != "soil" and surface != "asphalt" :
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? ")
u = int(u)
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: ")
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")
我只是希望能够创建更多没有乱七八糟的缩进的循环
以下是我的理解,如有错误请评论:
1.获取表面值
2. if (surface is invalid) 要求另一个值
3. 如果(表面是冰):
一种。获得你的价值
b.获取 a
的价值
C。使用公式
计算 s
s1 = u**2
s2 = 2*.08*9.8
s = s1/s2
d.显示
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: ")
如果您需要接受浮动(例如 1.25)值,您可能需要更改某些内容,例如将 variable = int(variable)
更改为 variable = float(variable)
。
你的代码有很多问题,我会指出其中的一些。你也应该开始使用 python3.x 而不是 python2
您可以直接将从用户那里获取的输入解析为 int(raw_input("xyz"))
牢记以上所有内容,下面的代码应该可以工作
import sys
def terrain():
surface = raw_input("What surface will you be driving on? ")
vel = -1
while surface != "ice" and surface != "concrete" and surface != "soil" and surface != "asphalt" :
surface = raw_input("Surface not valid. Please enter a valid surface: ")
if surface == "ice":
vel = int(raw_input("what is the velocity of the car in meters per second? "))
while vel < 0:
vel = int(raw_input("Velocity must be a positive integer: "))
while vel == 0:
vel = int(raw_input("Velocty must be a number greater than zero: "))
while vel > 72:
vel = int(raw_input("This vehicle cannot reach this speed: "))
acc = int(raw_input("How quickly is the vehicle decelerating? "))
while acc > 0:
acc = int(raw_input("Deceleration cannot be a positive integer: "))
s1 = vel**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()
了解缩进在 python 中的工作原理,因为它们是开始新代码块的方式,并尝试在我为您格式化的代码之上添加更多条件,然后您就会明白更好。
- 无论何时缩进,都应始终添加相同数量的缩进。没有必要在一个地方使用 4 个空格的缩进,而在其他地方使用 12 个空格。最常见的约定是使用 4 个空格。
- 仅在开始新块时添加缩进级别,即以冒号结尾的行之后的行。
- 结束一个块时,以下行必须与开始该块的关键字对齐。当然,如果不止一个块同时结束,这可能意味着缩进不止一层。
如果没有与缩进相关的语法错误,您的代码可能如下所示:
def terrain(surface):
surface = raw_input("What surface will you be driving on? ")
while surface != "ice" and surface != "concrete" and surface != "soil" and surface != "asphalt" :
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? ")
u = int(u)
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: ")
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")
我正在尝试在满足某些条件时创建循环,到目前为止它可以工作,但是当我尝试创建更多条件时,我必须进一步缩进它们以使我的程序工作。
def terrain(surface):
surface = raw_input("What surface will you be driving on? ")
while surface != "ice" and surface != "concrete" and surface != "soil" and surface != "asphalt" :
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? ")
u = int(u)
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: ")
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")
我只是希望能够创建更多没有乱七八糟的缩进的循环
以下是我的理解,如有错误请评论:
1.获取表面值
2. if (surface is invalid) 要求另一个值
3. 如果(表面是冰):
一种。获得你的价值
b.获取 a
的价值
C。使用公式
计算 s
s1 = u**2
s2 = 2*.08*9.8
s = s1/s2
d.显示
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: ")
如果您需要接受浮动(例如 1.25)值,您可能需要更改某些内容,例如将 variable = int(variable)
更改为 variable = float(variable)
。
你的代码有很多问题,我会指出其中的一些。你也应该开始使用 python3.x 而不是 python2
您可以直接将从用户那里获取的输入解析为 int(raw_input("xyz"))
牢记以上所有内容,下面的代码应该可以工作
import sys
def terrain():
surface = raw_input("What surface will you be driving on? ")
vel = -1
while surface != "ice" and surface != "concrete" and surface != "soil" and surface != "asphalt" :
surface = raw_input("Surface not valid. Please enter a valid surface: ")
if surface == "ice":
vel = int(raw_input("what is the velocity of the car in meters per second? "))
while vel < 0:
vel = int(raw_input("Velocity must be a positive integer: "))
while vel == 0:
vel = int(raw_input("Velocty must be a number greater than zero: "))
while vel > 72:
vel = int(raw_input("This vehicle cannot reach this speed: "))
acc = int(raw_input("How quickly is the vehicle decelerating? "))
while acc > 0:
acc = int(raw_input("Deceleration cannot be a positive integer: "))
s1 = vel**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()
了解缩进在 python 中的工作原理,因为它们是开始新代码块的方式,并尝试在我为您格式化的代码之上添加更多条件,然后您就会明白更好。
- 无论何时缩进,都应始终添加相同数量的缩进。没有必要在一个地方使用 4 个空格的缩进,而在其他地方使用 12 个空格。最常见的约定是使用 4 个空格。
- 仅在开始新块时添加缩进级别,即以冒号结尾的行之后的行。
- 结束一个块时,以下行必须与开始该块的关键字对齐。当然,如果不止一个块同时结束,这可能意味着缩进不止一层。
如果没有与缩进相关的语法错误,您的代码可能如下所示:
def terrain(surface):
surface = raw_input("What surface will you be driving on? ")
while surface != "ice" and surface != "concrete" and surface != "soil" and surface != "asphalt" :
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? ")
u = int(u)
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: ")
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")