这是怎么回事? Python 在函数中跳过行
当我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的函数定义为
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 iscounting(countingSubmenu, x, y, z, q)
.你没有提到你正在使用哪个版本的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()).
它似乎在 python 2.7 中工作(参见 Chris Phillips 的回答)
无论如何,您可以做一些改进
counting
从循环中取出,counting
四次,相反,只需调用counting(countingSubmenu, x, y, z, q)
counting
函数以不同的顺序获取参数,并且您以错误的顺序传递它,It seems to be working in python 2.7 (see Chris Phillips answer)
Anyway few improvements you can do
counting
out of the loopcounting
four times, instead just callcounting(countingSubmenu, x, y, z, q)
counting
function takes parameter in different order and you are passing it in wrong order您的问题是您以错误的顺序将参数传递给
counting()
。Your problem is you are passing the arguments to
counting()
in the wrong order.