[英]Creating basic menu in Python
import circle
pi = 3.1415
def main():
area(radius)
circumference(radius)
def menu():
print("Type a for area of circle")
print("Type b for circumference of a circle")
print("Type c to END PROGRAM")
loop=True
while loop:
menu()
choice = input('Please enter your choice: ')
if choice=="a":
radius = float(input ("Input the radius of the circle : "))
print(circle.area(radius))
elif choice=="b":
radius = float(input ("Input the radius of the circle : "))
print(circle.circumference(radius))
else:
print("Goodbye!")
def area(radius):
return pi * radius**2
def circumference(radius):
return 2 * pi * radius
main()
I am trying to make a simple menu that either gives the user one of the instructed outputs if they type one of the three letters.我正在尝试制作一个简单的菜单,如果用户键入三个字母之一,则可以为用户提供一个指示输出。 So if I were to type the letter 'a', I would be asked to input the radius of the circle and then be given the area in return.所以如果我输入字母“a”,我会被要求输入圆的半径,然后给出面积作为回报。 I hope that makes sense.我希望这是有道理的。
However, when I try to test this menu, I am told none of the letters are defined, so I'm kind of confused why it's not working.然而,当我尝试测试这个菜单时,我被告知没有定义任何字母,所以我有点困惑为什么它不起作用。
You are very close, just a couple of things are derailing your program.您非常接近,只是有几件事使您的程序脱轨。
First: Because you have created functions, you don't need to import a 'circle' mod.第一:因为你已经创建了函数,所以你不需要导入'circle' mod。 Where does this come from?这是从哪里来的? Are you trying to define a Class object or modual?您是要定义 Class object 还是模态的?
Second: Your functions need to be defined in the program BEFORE they are called.第二:您的函数需要在调用它们之前在程序中定义。 You had them placed after, which was causing errors.您将它们放在后面,这会导致错误。
Third: You forgot to set loop to False when "c" is entered.第三:输入“c”时忘记将循环设置为 False。 Lacking this, then loop never ends.缺少这个,然后循环永远不会结束。
Hope this helps!希望这可以帮助!
def area(radius):
return pi * radius**2
def circumference(radius):
return 2 * pi * radius
#import circle
pi = 3.1415
def main():
area(radius)
circumference(radius)
def menu():
print("Type a for area of circle")
print("Type b for circumference of a circle")
print("Type c to END PROGRAM")
loop=True
while loop:
menu()
choice = input('Please enter your choice: ')
if choice=="a":
radius = float(input ("Input the radius of the circle : "))
print(area(radius))
elif choice=="b":
radius = float(input ("Input the radius of the circle : "))
print(circumference(radius))
else:
print("Goodbye!")
loop = False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.