python - 为什么 Python 不读取下一行(如果)?
问题描述
我写了关于毕达哥拉斯的程序我得到了答案,但是 python 在 (if) 之后没有读取 (break) 。((a+b+c=1000)&(a**2 + b**2 =c**2) )
我的程序标题:(特殊毕达哥拉斯三元组),我想找到一个b c存在一个答案。我知道(a=200, b=375, c=425)
,但是当程序启动时,它永远不会停止并继续。它还键入这三个数字的乘积。
import random as r
def pyth(b,d,c):
pyth = None
if b**2+c**2 == d**2 :
pyth = True
h=d*c*b
print(h)
return pyth
if b**2+d**2==c**2 :
pyth= True
h=d*c*b
print(h)
return pyth
if d**2 + c**2 == b**2:
pyth =True
h=d*c*b
print(h)
return pyth
else:
pyth = False
return
a = list(range (150,1000))
b=0
c=0
d=0
h = 0
for i in range(0,10000000):
b = r.choice(a)
c = r.choice(a)
d = 1000-b-c
e = b+c+d
if e == 1000 :
pyth(b,d,c)
if pyth == True:
break
else:
continue
解决方案
不需要pyth
变量。您可以只使用return True
或return False
。
该if
语句需要缩进,以便它在循环中。
您需要测试函数调用的值。
你不需要else: continue
。除非您中断循环,否则循环会自动继续。continue
仅当您想跳过循环体的其余部分并开始下一次迭代时才需要;在身体的末端不需要它。
import random as r
def pyth(b,d,c):
if b**2+c**2 == d**2 :
h=d*c*b
print(h)
return True
if b**2+d**2==c**2 :
h=d*c*b
print(h)
return True
if d**2 + c**2 == b**2:
h=d*c*b
print(h)
return True
else:
return False
a = list(range (150,1000))
for i in range(0,10000000):
b = r.choice(a)
c = r.choice(a)
d = 1000-b-c
e = b+c+d
if e == 1000 and pyth(b,d,c)
break
推荐阅读
- excel - 查找单元格内容并替换第二个匹配项
- c# - 从存储过程读取 C# XML 输出从 TEMP 表 SQL Server 返回 null
- prolog - 如何反转列表并将所有指定值替换为其他值?
- python - Python - 除非指定,否则使用 pytest 跳过测试
- django - 是否可以在 Django 中将第二个 slug 添加到 URL 路径?
- spring - 将对象发送到服务时出现 java.lang.NullPointerException
- javascript - ElectronJS - 在 Windows 之间共享 redux 存储?
- direct3d - 将 D3D11 Texture2D 复制到 D2D1 位图
- selenium-webdriver - 使用 xPath 查找文本包含“en”的所有元素:
- javascript - 在反应js中处理动态增长的列表