阻止python转到最后一条语句
我目前正在写一个程序来解决毕达哥拉斯定理。然而,我在程序中有一个bug。每当我为长度a或b输入一个负数时,它就会打印出“a不能小于零”,但仍然会继续为C求解,并打印出C的长度,即使用户还没有输入b。当用户输入一个负数时,它打印出语句“a不能小于零”,然后再次循环输入边的长度,而不是在打印出语句后重定向到末尾的位置,我怎么能做到这一点 这是我的密码:阻止python转到最后一条语句,python,python-2.7,pythagorean,Python,Python 2.7,Pythagorean,我目前正在写一个程序来解决毕达哥拉斯定理。然而,我在程序中有一个bug。每当我为长度a或b输入一个负数时,它就会打印出“a不能小于零”,但仍然会继续为C求解,并打印出C的长度,即使用户还没有输入b。当用户输入一个负数时,它打印出语句“a不能小于零”,然后再次循环输入边的长度,而不是在打印出语句后重定向到末尾的位置,我怎么能做到这一点 这是我的密码: import math print"This program will solve the pythagorean theorem for
import math
print"This program will solve the pythagorean theorem for you"
unit=raw_input('Enter the unit you will be using')
a=float(raw_input('Enter the length of side a'))
if a<=0:
print"A cannot be less than zero"
else:
b=float(raw_input('Enter the length of side b'))
if b<=0:
print"B cannot be less than zero"
else:
c2=(a**2)+(b**2)
c=math.sqrt(c2)
c=str(c)
print "The length of side C is: "+ c + " " + unit + "."
导入数学
打印“此程序将为您解决毕达哥拉斯定理”
单位=原始输入('输入您将使用的单位')
a=浮动(原始输入(“输入a侧的长度”)
如果a你错过了一个缩进水平。试着这样做:
if a<0:
print"A cannot be less than zero"
else:
b=raw_input('Enter the length of side b')
b=float(b)
if b<0:
print"B cannot be less than zero"
else:
c2=(a**2)+(b**2)
c=math.sqrt(c2)
c=str(c)
print "The length of side C is: "+ c + " " + unit + "."
if a在设计程序流时,尽量避免使用嵌套的if
。它会导致这种错误(缺少一级缩进)。if
块中的大块代码和许多嵌套的if
块使程序更难理解和推理
相反,您可以再次询问,直到输入有效:
a = -1
while a < 0:
try:
a=float(raw_input('Enter the length of side a'))
except ValueError:
pass
if a<0:
print "A cannot be less than zero"
然后使用:
a = validate(
initial = -1,
prompt = 'Enter the length of side A',
test = lambda x: x >= 0,
message = "A cannot be less than zero",
typecast = float
)
b = validate(
initial = -1,
prompt = 'Enter the length of side B',
test = lambda x: x >= 0,
message = "B cannot be less than zero",
typecast = float,
)
首先,如果你想经常检查你的输入,你必须使用一个循环。如中所示,算法的psuedocode应为:
Loop Begin
Check the value of a and b
If a or b is less than 0 then ask for input again
Otherwise, continue
请注意,算法必须至少运行一次
这就是psuedocode的基本外观。因此,在这种情况下,可以使用do while
循环构造。在Python中,没有类似的东西,因此我们模拟它:
免责声明:我使用的是不同版本的Python,因此里程数可能会有所不同
import math
a = 0
b = 0
def py_theorem(a, b):
return(a**2 + b**2)
unit = raw_input('Enter the unit you will be using: ')
while a <= 0:
a = float(raw_input('Enter the length of side A: '))
if a <= 0:
print('A cannot be less than 0.')
while b <= 0:
b = float(raw_input('Enter the length of side B: '))
if b <= 0:
print('B cannot be less than 0.')
print('The length of the 3rd side C is %d %s') % (py_theorem(a,b), unit)
导入数学
a=0
b=0
def py_定理(a,b):
退货(a**2+b**2)
单位=原始输入('输入您将使用的单位:')
看看你是如何将语句链接在一起的;缩进很重要有一个想法对你来说:你可能想把你的输入和验证代码放进一个函数中,为a端和b端调用它。我想向你展示一种使用整洁函数的方法。我希望你能坚持住:)同样对于OP,如果边为零会发生什么?只是一个小的逻辑问题。您还可以通过调用float(raw_input('Enter the length of side a:'))IIRCI对输入进行强制转换。虽然这样做确实有效,但它不能帮助OP了解他做错了什么,以及将来如何避免,也就是说,它实际上不能解决问题,也不能回答OP的问题(严格地说)。添加更多信息将非常有用。@crownedzero为您解决了这个问题。使其成为同一行上的浮点数,并检查其是否小于或等于zero@mHurley:在回答中做了评论What does:if name==“main”:main()做什么?@Domecraft这个问题已经被问过好几次了。下面是一个例子。@GamesBrainiac哈哈,我们有点相同的想法来分解它
import math
def take_in():
a = raw_input("Enter the value of side a -> ")
b = raw_input("Enter the value of side b -> ")
# Trying to convert to a float
try:
a, b = float(a), float(b)
# If successfully converted, then we return
if a > 0 and b > 0:
return a, b
except ValueError:
pass
# If we cannot return, then we return false, with a nice comment
print "Invalid input"
return False
def main():
# Calling the function at least once
valid = take_in()
# While we are not getting valid input, we keep calling the function
while not valid:
# Assigning the value to valid
valid = take_in()
# Breaking the return tuple into a and b
a, b = valid
print math.sqrt(a ** 2 + b ** 2)
if __name__ == '__main__':
main()
import math
a = 0
b = 0
def py_theorem(a, b):
return(a**2 + b**2)
unit = raw_input('Enter the unit you will be using: ')
while a <= 0:
a = float(raw_input('Enter the length of side A: '))
if a <= 0:
print('A cannot be less than 0.')
while b <= 0:
b = float(raw_input('Enter the length of side B: '))
if b <= 0:
print('B cannot be less than 0.')
print('The length of the 3rd side C is %d %s') % (py_theorem(a,b), unit)