print ("Enter the object you are tyring to find.")
print ("1 = Radius")
print ("2 = Arch Length")
print ("3 = Degree")
print ("4 = Area")
x = int(input("(1,2,3,4):"))
if x == 1:
print ("You are finding the Radius.")
ra = int(input("Enter the arch length: "))
rd = int(input("Enter the degree: "))
rr = ra/math.radians(rd)
print ("The Radius is:",rr)
if x == 2:
print ("You are finding the Arch Length.")
sr = int(input("Enter the radius: "))
sd = int(input("Enter the degree: "))
ss = math.radians(sd)*sr
print ("The Arch Length is:",ss)
我正在制作一个基本的数学课程,但我想让它无限重复。这不是完整的代码,但我想对其余的" if"做同样的事情。声明。我希望它在每个功能完成后结束并重复回到第一行。谢谢!
答案 0 :(得分:2)
放一个
while True:
在你想重新开始的地方;将所有后续行缩进四个空格。
在您想要在while
之后重新启动的每个位置,添加语句:
continue
当然也正确缩进。
如果您还想让用户有机会干净地结束程序(例如,除了您现在提供的4之外还有其他选择),那么在该位置有一个条件语句(再次正确缩进):
if whateverexitcondition:
break
答案 1 :(得分:2)
你需要添加一种让用户退出并打破循环的方法,但是只要你想要的话,True会循环。
while True:
# let user decide if they want to continue or quit
x = input("Pick a number from (1,2,3,4) or enter 'q' to quit:")
if x == "q":
print("Goodbye")
break
x = int(x)
if x == 1:
print ("You are finding the Radius.")
ra = int(input("Enter the arch length: "))
rd = int(input("Enter the degree: "))
rr = ra/math.radians(rd)
print ("The Radius is:",rr)
elif x == 2: # use elif, x cannot be 1 and 2
print ("You are finding the Arch Length.")
sr = int(input("Enter the radius: "))
sd = int(input("Enter the degree: "))
ss = math.radians(sd)*sr
print ("The Arch Length is:",ss)
elif x == 3:
.....
elif x == 4:
.....
如果您打算使用循环,您还可以使用try / except验证用户仅输入有效输入:
while True:
try:
x = int(input("(1,2,3,4):"))
except ValueError:
print("not a number")
continue