if option == 'C':
radius = float(raw_input("Enter Radius: "))
area = pi * radius**2
print "Working..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))
elif option == 'T':
base = float(raw_input("Enter base: "))
height = float(raw_input("Enter height: "))
area = (0.5)*base*height
print "Working..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))
else:
print "Error: Could not process. The program will restart."
sleep(4.5)
我希望代码在底部重新启动,只有当用户在'else'下面输入错误的内容时才会重启。我不希望它要求他们重新启动,而是自动重启。谢谢!
另外,请表明,不要告诉。我需要看到它,我是Python的新手。
答案 0 :(得分:1)
在您提出任何问题之前,您应首先在线搜索。你学习这个教程吗?很明显,您已经确定了问题(如何重新启动它)。问问自己该怎么办?它就像一个循环。要创建循环,您需要“while”。然后如何满足这个以循环过程?让'while'条件始终为真。
from time import sleep
pi = 3.1415926
hint = 'what'
def function(option):
if option == 'C':
radius = float(input("Enter Radius: "))
area = pi * radius ** 2
print("Working...")
sleep(1)
print("Area: %.2f. \n%s" % (area, hint))
elif option == 'T':
base = float(input("Enter base: "))
height = float(input("Enter height: "))
area = (0.5) * base * height
print("Working...")
sleep(1)
print("Area: %.2f. \n%s" % (area, hint))
else:
print("Error: Could not process. The program will restart.")
sleep(4.5)
while True: # here is how you restart the program. it is just like C
option = input("\nwhat do you want ? 'C' or 'T'\n")
function(option)
答案 1 :(得分:1)
将代码包装在一个循环中将创建一个" restart"你想要的功能,即
while True:
option = raw_input()
if option == 'C':
radius = float(raw_input("Enter Radius: "))
area = pi * radius**2
print "Working..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))
break
elif option == 'T':
base = float(raw_input("Enter base: "))
height = float(raw_input("Enter height: "))
area = (0.5)*base*height
print "Working..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))
break
else:
print "Error: Could not process. The program will restart."
sleep(4.5)
如果选择了其中一个选项,这将结束循环,否则它将从头开始重新启动。
答案 2 :(得分:0)
有更好的方法来编写此代码。但要简单地解决您的问题,请将您的代码放入函数中并调用该函数。如果function为成功则返回True,否则返回False。
def my_fuction():
if option == 'C':
radius = float(raw_input("Enter Radius: "))
area = pi * radius**2
print "Working..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))
return True
elif option == 'T':
base = float(raw_input("Enter base: "))
height = float(raw_input("Enter height: "))
area = (0.5)*base*height
print "Working..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))
return True
else:
print "Error: Could not process. The program will restart."
sleep(4.5)
return False
# When function return false, call the function again.
success = False
while not success:
success = my_fuction()
答案 3 :(得分:0)
一个简单的可运行演示,处理无效输入和破坏选项:
from time import sleep
from math import pi
hint = 'your hint'
while True:
option = raw_input("Enter option: ")
try:
if option == 'C':
radius = float(raw_input("Enter Radius: "))
area = pi * radius ** 2
print "Working..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))
elif option == 'T':
base = float(raw_input("Enter base: "))
height = float(raw_input("Enter height: "))
area = 0.5 * base * height
print "Working..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))
elif option == 'N':
break
else:
print "Error: Could not process. The program will restart."
sleep(4.5)
except ValueError:
print "Error: Could not process. The program will restart."
sleep(4.5)
希望得到这个帮助。
答案 4 :(得分:0)
#is this better
put = ""
while put != "yes":
if option == 'C':
radius = float(raw_input("Enter Radius: "))
area = pi * radius**2
print "Working..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))
elif option == 'T':
base = float(raw_input("Enter base: "))
height = float(raw_input("Enter height: "))
area = (0.5)*base*height
print "Working..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))
else:
print "Error: Could not process. The program will restart."
put = "no"
sleep(4.5)
答案 5 :(得分:-1)
这是代码。
enter code here
def func():
if option == 'C':
radius = float(raw_input("Enter Radius: "))
area = pi * radius**2
print "Working..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))
elif option == 'T':
base = float(raw_input("Enter base: "))
height = float(raw_input("Enter height: "))
area = (0.5)*base*height
print "Working..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))
else:
print "Error: Could not process. The program will restart."
func()#place it where ever you want in else part
sleep(4.5)