这是怎么回事? Python 在函数中跳过行

发布于 2024-12-03 02:36:57 字数 1939 浏览 1 评论 0原文

当我在 Python 中运行以下程序时,该函数接受变量,但完全跳过其余部分并重新显示程序的主菜单,而不执行任何操作。另外,它会跳过限定的“if”语句并询问所有变量,即使选择了第一个或第二个选项(不需要第三个变量)。顺便说一句,这不应该是缩进错误,我只是缩进以显示它是 stackoverflow 内的代码。

编辑:没关系。我让它工作了。函数括号中的变量必须全部相同。呃! 拍拍额头

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

反目相谮 2024-12-10 02:36:57

您的函数定义为 counting (x, y, z,countingSubmenu, q),但是当您调用它时,您的参数列表是 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).

孤寂小茶 2024-12-10 02:36:57

你没有提到你正在使用哪个版本的Python,但我怀疑它来自3.x系列。 Python 3 更改了 input() 的行为 以匹配之前的 raw_input( )在 2.x 系列中。

因此,input() 现在总是返回一个字符串。因此,您需要对结果调用 int() 或 eval() (就我个人而言,我建议 int())。

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

离去的眼神 2024-12-10 02:36:57

它似乎在 python 2.7 中工作(参见 Chris Phillips 的回答)

无论如何,您可以做一些改进

  • ,将函数 counting 从循环中取出,
  • 您不需要调用 counting 四次,相反,只需调用 counting(countingSubmenu, x, y, z, q)
  • counting 函数以不同的顺序获取参数,并且您以错误的顺序传递它,
  • 您无需传递 x, y、z 如你所愿向用户询问

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
红颜悴 2024-12-10 02:36:57

您的问题是您以错误的顺序将参数传递给 counting()

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文