当我运行程序时,“无”一直显示

每当我运行我创建的计算器程序时,它都可以正常工作,但文本“ None”一直显示,我也不知道为什么。这是代码:


def add():

    print 'choose 2 numbers to add'

    a=input('add this')

    b=input('to this')

    print a+b

    return menu()

def sub():

    print 'choose 2 numbers to subract'

    a=input('subract this')

    b=input('from this')

    print b-a

    return menu()

def menu():

    print "hello, Welcome"

    print "these are your options"

    print "1. add"

    print "2. sub"

print menu()

loop=2

def sys():

    while loop==2:

        a=input("please choose")

        if a==1:

            print add()

        elif a==2:

            print sub()

        else:

            return menu(),sys()

print sys()

这是输出:


hello, Welcome

these are your options

1. add

2. sub

None    <-------------------------(this is what I'm talking about)

please choose

如果它对任何人都有帮助,那么这是我完成的计算器的代码(我粘贴它时看起来很混乱,但是当您复制和粘贴时它可以工作)


def add():

    print 'choose 2 numbers to add'

    a=input('add this')

    b=input('to this')

    print a+b

def sub():

    print 'choose 2 numbers to subract'

    a=input('subract this')

    b=input('from this')

    print b-a

def mul():

    print 'choose 2 numbers to multiply'

    a=input("multiply this")

    b=input("by this")

    print b*a

def div():

    print 'choose what numbers your want to divide'

    a=input('divide this')

    b=input('by this')

    print a/b

def exp():

    print 'choose your number you want to exponentiate'

    a=input('multiply this')

    b=input('by the power of this')

    print a**b

def menu():

    print "hello, Welcome"

    print "these are your options"

    print "1. add"

    print "2. sub"

    print "3. mul"

    print "4. div"

    print "5. expo"

    print "0. to end"

menu()


有只小跳蛙
浏览 150回答 1
1回答

一只甜甜圈

那是因为该函数menu()没有返回任何东西,默认情况下,python中的一个函数会返回None>>> def func():pass>>> print func()&nbsp; #use `print` only if you want to print the returned valueNone只需使用:menu() #no need of print as you're already printing inside the function body.从和sys()删除后的新版本。不必在每个函数内部使用,只需在其末尾调用该函数即可。return menu()add()sub()return menu()menu()while loopdef sys():&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; a = input("please choose")&nbsp; &nbsp; &nbsp; &nbsp; if a == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add()&nbsp; &nbsp; # call add(), no need of print as you're printing inside add() itself&nbsp; &nbsp; &nbsp; &nbsp; elif a==2:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub()&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; menu()&nbsp; &nbsp; &nbsp; &nbsp;# call menu() at the end of the loopwhile loop==2实际上loop==2首先计算表达式,如果是,True则while loop继续,否则立即中断。在您的情况下,因为您不更改loop变量的值,所以可以简单地使用while True。>>> loop = 2>>> loop == 2True
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python