Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中创建基本菜单_Python - Fatal编程技术网

在Python中创建基本菜单

在Python中创建基本菜单,python,Python,我试着制作一个简单的菜单,如果用户输入三个字母中的一个,它会给用户一个指示输出。因此,如果我输入字母“a”,我会被要求输入圆的半径,然后得到面积作为回报。我希望这是有道理的 然而,当我尝试测试此菜单时,我被告知没有定义任何字母,因此我有点困惑它为什么不起作用。您非常接近,只是有几件事情正在破坏您的程序 第一:因为您已经创建了函数,所以不需要导入“圆”模型。这是从哪里来的?您正在尝试定义类对象或模型吗 第二:在调用函数之前,需要在程序中定义它们。您将它们放在后面,这会导致错误 第三:当输入“c”时

我试着制作一个简单的菜单,如果用户输入三个字母中的一个,它会给用户一个指示输出。因此,如果我输入字母“a”,我会被要求输入圆的半径,然后得到面积作为回报。我希望这是有道理的


然而,当我尝试测试此菜单时,我被告知没有定义任何字母,因此我有点困惑它为什么不起作用。

您非常接近,只是有几件事情正在破坏您的程序

第一:因为您已经创建了函数,所以不需要导入“圆”模型。这是从哪里来的?您正在尝试定义类对象或模型吗

第二:在调用函数之前,需要在程序中定义它们。您将它们放在后面,这会导致错误

第三:当输入“c”时,您忘记将循环设置为False。缺少这一点,循环就永远不会结束

希望这有帮助

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()

这些“字母”被视为变量名。如果要查看用户输入是否与值“a”相等,应使用
choice==“a”
My choices now working!但是,当我输入半径时,我收到了以下错误:“”AttributeError:部分初始化的模块“circle”没有属性“Percentage”(很可能是由于圆形导入)“'lines
a=print(…)
没有意义。它们只是将
a
b
c
设置为
None
,这是
print
的返回值,并且它们只在该方法中定义。正如其他人所说的,检查<代码>选择= =“a”<代码>等。此外,您应该在<代码> c>代码>选项中从循环中执行<代码>中断> /代码>。如果您还有其他问题,请考虑问一个新问题。请不要用后续问题更新现有问题。请提供一个,以及整个错误输出。
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