开发者

Whats is going on? Python skipping over lines in function

When I run the following program in Python, the function takes the variables in, but completely skips over the rest and re-shows the main menu for the program without doing anything. Plus, it skips the qualifying "if" statements and asks for all the variables even if the first or second options are chosen (which don't need the third variable). BTW, It shouldn't be an indent error, I just indented to show it was code inside stackoverflow.

EDIT: NEVERMIND. I got it to work. The variables in the function parenthesis all have to be the same. DUH! smacks forehead

option = 1
while option !=0:
    print "\n\n\n************MENU************"
    print "1. Counting"
    print "2. Fibbonacci Sequence"
    print "0. GET ME OUTTA HERE!"
    print "*" * 28
    option = input("Please make a selection: ") #counting submenu
    if option == 1:

        print "\n\n*******Counting Submenu*******"
        print "1. Count up by one"
        print "2. Count down by one"
        print "3. Count up by different number"
        print "4. Count down by different number"
        print "*" * 28
        countingSubmenu = input("Please make a selection: ")
        x=0
        y=0
        z=0
        q=0
        def counting (x, y, z, countingSubmenu, q):
            x = input("Please choose your starting number: ")
            y = input("Please choose your ending number: ")
            if countingSubmenu == 1:
   开发者_如何转开发             for q in range (x, y+1, 1):
                    print q
            elif countingSubmenu == 2:
                for q in range (x, y, -1):
                    print q
            elif countingSubmenu == 3:
                z = input("Please choose an increment: ")
                for q in range (x, y+1, z):
                    print q
            else:
                z = input("Please choose an increment: ")
                for q in range (x, y, -z):
                    print q
            return x, y, z, q
        if countingSubmenu == 1:
            counting(countingSubmenu, x, y, z, q)
        if countingSubmenu == 2:
            counting(countingSubmenu, x, y, z, q)
        if countingSubmenu == 3:
            counting(countingSubmenu, x, y, z, q)
        if countingSubmenu == 4:
            counting(countingSubmenu, x, y, z, q)


Your function is defined as counting (x, y, z, countingSubmenu, q), but when you're calling it, you're argument list is counting(countingSubmenu, x, y, z, q).


You didn't mention which version of Python you are using, but I suspect it's from the 3.x series. Python 3 changed the behavior of input() to match what was previously raw_input() in the 2.x series.

So, input() now always returns a string. So you either need to call int() or eval() on the result (personally, I suggest int()).


It seems to be working in python 2.7 (see Chris Phillips answer)

Anyway few improvements you can do

  • take out function counting out of the loop
  • you need not call counting four times, instead just call counting(countingSubmenu, x, y, z, q)
  • counting function takes parameter in different order and you are passing it in wrong order
  • you need not pass x, y, z as you are asking that from user


Your problem is you are passing the arguments to counting() in the wrong order.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜