Ric*_*ope 11 python loops boolean while-loop
所以,我最近进入python编程并决定编写一个简单的代码,运行一些简单的数学,例如计算三角形中的缺失角度以及其他类似的简单事物.在我制作了这个程序和其他一些程序后,我想也许我知道的其他人可以使用它,所以我决定尝试尽可能简单.代码可以在下面找到:
a = int(input("What's one of the angles?"))
b = int(input("What's the other angle in the triangle?"))
c = (a + b)
d = 180
f = int(180 - c)
print(f)
Run Code Online (Sandbox Code Playgroud)
代码本身确实有效,但唯一的问题是如果你有一个以上的问题,那么持续加载Python并点击F5就变得单调乏味而且相当繁琐,我的想法是让它循环无数次直到你决定关闭该计划.每当我尝试寻找一种方法来执行此操作时,所有的True语句都是针对更大更复杂的代码片段而这可能是我的第五或第十段代码,我无法理解其中的一些编码.
如果有人愿意提供帮助,我会很感激这个主题的任何帮助或建议.
Leb*_*Leb 14
while True:
a = int(input("What's one of the angles?" + '\n'))
b = int(input("What's the other angle in the triangle?"+ '\n'))
c = (a + b)
f = int(180 - c)
print(f)
if input("Would you like to do another? 'y' or 'n'"+ '\n').lower() == 'y':
pass
else:
break
Run Code Online (Sandbox Code Playgroud)
你可以问他们是否想再去一次.y将重新启动循环,n将结束它.的.lower()是在他们输入的情况下Y或N.
正如@ Two-BitAlchemist所提到的那样d=180是不必要的.
您可以将代码放在一个函数中,例如:
def simple():
a = int(input("What's one of the angles?"))
b = int(input("What's the other angle in the triangle?"))
c = (a + b)
d = 180
f = int(180 - c)
print(f)
Run Code Online (Sandbox Code Playgroud)
然后只需输入:
simple()
Run Code Online (Sandbox Code Playgroud)
每次使用它.
while True 对于这个剧本来说已经足够了,为什么放弃呢?
while True:
a = int(input("What's one of the angles?"))
b = int(input("What's the other angle in the triangle?"))
c = (a + b)
d = 180
f = int(180 - c)
print(f)
Run Code Online (Sandbox Code Playgroud)