所以我正在尝试创建一个脚本,用户可以输入一个值(现在为1,2或3),它提供了一个可用于查找答案的数学函数。但后来我想询问用户是想重启该数学函数还是返回菜单要求用户选择不同的函数。
我只学习了几个星期的python,所以我真的不知道怎么做,任何帮助都会受到赞赏。谢谢!
这是我的代码:
choice = int(input("What program would you like to run? \n"))
#chooses one of the programs
recarea = 1
circarea = 2
fah = 3
if choice == 1:
print("This program calculates the area and perimeter of a rectangle:")
length = float(input("Type in the length: \n"))
width = float(input("Type in the width: \n"))
area = length * width
perimeter = (2*length)+(2*width)
print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n')
reuse = str(input("Would you like to restart the script ?"))
if reuse == "Yes" or reuse == "yes" or reuse == "y" or reuse == "Y":
print("Restarting Script...\n")
else:
reuse == "No" or reuse == "no" or reuse == "n" or reuse == "N"
print("Returning to the menu...\n\n")
答案 0 :(得分:0)
您需要将代码分解为函数:
def main():
print("What program would you like to run? \n")
choice = int(input("1 - rectangle area\n2 - circle area\n3 - fah"))
if choice == 1:
print("This program calculates the area and perimeter of a rectangle:")
length = float(input("Type in the length: \n"))
width = float(input("Type in the width: \n"))
area = length * width
perimeter = (2*length)+(2*width)
print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n')
elif choice == 2:
print("Not implemented.")
elif choice == 3:
print("Not implemented.")
else:
print("I didn't understand that input please type 1, 2 or 3.")
main()
askRestart()
def askRestart():
reuse = str(input("Type 'Y' to restart or 'X' to exit()."))
if reuse.lower() == 'y':
main()
elif reuse.lower() == 'x':
pass
else:
print("I didn't understand that input please type 'Y' or 'X'")
askRestart()
然后调用该函数运行程序:
main()
答案 1 :(得分:0)
您也可以将代码简单地包装在while True:
块中。这将无限重复整个片段。
while True:
choice = int(input("What program would you like to run? \n"))
#chooses one of the programs
recarea = 1
circarea = 2
fah = 3
if choice == 1:
print("This program calculates the area and perimeter of a rectangle:")
length = float(input("Type in the length: \n"))
width = float(input("Type in the width: \n"))
area = length * width
perimeter = (2*length)+(2*width)
print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n')
reuse = str(input("Would you like to restart the script ?"))
if reuse == "Yes" or reuse == "yes" or reuse == "y" or reuse == "Y":
print("Restarting Script...\n")
else:
reuse == "No" or reuse == "no" or reuse == "n" or reuse == "N"
print("Returning to the menu...\n\n")
另一个,也许更聪明的选择是,如果输入某个值,则终止循环。例如,如果输入0
,则此版本将退出。
choice=None
while choice != 0:
choice = int(input("What program would you like to run? (enter 0 to exit) \n"))
#chooses one of the programs
recarea = 1
circarea = 2
fah = 3
if choice == 1:
print("This program calculates the area and perimeter of a rectangle:")
length = float(input("Type in the length: \n"))
width = float(input("Type in the width: \n"))
area = length * width
perimeter = (2*length)+(2*width)
print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n')
reuse = str(input("Would you like to restart the script ?"))
if reuse == "Yes" or reuse == "yes" or reuse == "y" or reuse == "Y":
print("Restarting Script...\n")
else:
reuse == "No" or reuse == "no" or reuse == "n" or reuse == "N"
print("Returning to the menu...\n\n")