繁体   English   中英

Python菜单功能无法正常工作

[英]Python Menu function not working properly

美好的一天,我有这个项目的问题,我正在做一个学校作业。 基本上,菜单功能按我的意愿显示,但它不能正确执行任何其他功能。 所以,如果我输入一个数字,它就会循环回到菜单。 即使退出选项也不起作用。 我真的无法弄清楚为什么。 它似乎完全遵循我所有示例代码的语法。

def Menu():
    print ("")
    print ("CALCULATIONS MENU")
    print ("1) AREA (SQUARE)")
    print ("2) AREA (RECTANGLE)")
    print ("3) AREA (CIRCLE)")
    print ("4) PERIMETER (SQUARE)")
    print ("5) PERIMETER (RECTANGLE)")
    print ("6) PERIMETER (CIRCLE)")
    print ("7) EXIT")
    Test = input ("Input menu option(1-7): ")
    return Test

def ASQ(Height):
    print ("")
    print ("The area of the square is:", Height * Height)

def AREC(Height, Width):
    print ("")
    print ("The area of the rectangle is:", Height * Width)

def ACIR(Radius):
    print ("")
    print ("The area of the circle is:", 3.1415 * Radius**2)

def PSQ(Height):
    print ("")
    print ("The perimeter of the square is:", Height * 4)

def PREC(Height, Width):
    print ("")
    print ("The perimeter of the rectangle is:", Width*2 + Height*2)

def PCIR(Diameter):
    print ("")
    print ("The perimeter of the circle is:", Diameter * 3.1415)

Loop = 1
Selection = 0
while Loop == 1:
    Selection = Menu()
    if Selection == 1:
        ASQ(input("Enter the length of one side: "))
    elif Selection == 2:
        AREC(input("Enter height: "), input("Enter width: "))
    elif Selection == 3:
        ACIR(input("Enter radius: "))
    elif Selection == 4:
        PSQ(input("Enter the length of one side: "))
    elif Selection == 5:
        PREC(input("Enter height: "), input("Enter width: "))
    elif Selection == 6:
        PCIR(input("Enter diameter: "))
    elif Selection == 7:
        Loop = 0

print ("Good bye")

问题:

Test = input ("Input menu option(1-7): ")

Test将用户输入包含为string ,因此当您在Selection保存Test的值并在if-statements比较它的值时,

if Selection == 1:

你正在将int与一个不起作用的string进行比较。

解:

通过更改将用户输入转换为int

Test = input("Input menu option(1-7): ")

Test = int(input ("Input menu option(1-7): "))

或者在if-statements中包含双引号""的数字

if Selection == "1":

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM