简体   繁体   English

谁能告诉我我的功能出了什么问题?

[英]Can anyone tell me what's wrong with my function?

I'm working on this project and I keep getting this problem when the code tries to return the "guess" variable, instead of returning the value of "guess" it goes to line 9 which converts the value into a string for some reason, and then returns that. 我正在研究这个项目,当代码尝试返回“ guess”变量时,我一直遇到这个问题,而不是返回“ guess”的值,而是转到第9行,该行由于某种原因将值转换为字符串,然后返回。 when I go to use the returned value in something, Python says that the value is "NoneType". 当我在某些情况下使用返回的值时,Python表示该值是“ NoneType”。

def ask_question(max_length, rowOrCol, guessNumber):
    guess = raw_input("What is the "+rowOrCol+" number of your "+guessNumber+" guess? ")
    try:
        guess = int(guess)
        if guess <= max_length:
            return guess
        else:
            print "That number was too big, it must be no larger then " +str(max_length)
            ask_question(max_length, rowOrCol, guessNumber)
    except(TypeError):
        print "Only numbers are accepted, please try again!"
        ask_question(max_length, rowOrCol, guessNumber)

I call the function with this line: 我在这一行调用函数:

first_guess_row = ask_question(4, "row", "first")

Is there anything that I'm missing? 有什么我想念的吗?

of course all your branches need to return.... 当然,您所有的分支机构都需要退货。

...
        else:
            print "That number was too big, it must be no larger then " +str(max_length)
            return ask_question(max_length, rowOrCol, guessNumber)
    except(TypeError):
        print "Only numbers are accepted, please try again!"
        return ask_question(max_length, rowOrCol, guessNumber)

before you were recalling the function ... but you were throwing away its return value 在您调用该函数之前...但是您却丢弃了它的返回值

for lines 9 and 12, you are doing a recursive call. 对于第9行和第12行,您正在进行递归调用。

A recursive call is a brand new call to the function within the function. 递归调用是对函数内函数的全新调用。 The new call to ask_question, assuming line 6 is executed, is returning a value. 假设执行了第6行,对ask_question的新调用将返回一个值。 but it is returned within the original ask_question call. 但会在原始的ask_question调用中返回。

Therefore you need to change 因此,您需要更改

ask_question(max_length, rowOrCol, guessNumber)

on line 9 and 12 to 在第9和12行上

return ask_question(max_length, rowOrCol, guessNumber)

to retrieve that value. 检索该值。

Another Note : recursive calls use extra memory for each recursion, which can cause slowdowns or even crash python (if you recurse a lot, depending on the size of the function). 另一个注意事项 :递归调用每次递归都会占用额外的内存,这可能会导致速度变慢甚至使python崩溃(如果递归很多,则取决于函数的大小)。 I would recommend putting your code into a loop like this: 我建议将您的代码放入这样的循环中:

continue_asking = True
while continue_asking:
    guess = raw_input("What is the "+rowOrCol+" number of your "+guessNumber+" guess? ")
    try:
        # typecheck the guess value
        guess = int(guess)
    except (TypeError):
        print "Only numbers are accepted, please try again!"
        continue

    if guess <= max_length:
        return guess
    else:
        print "That number was too big, it must be no larger then " +str(max_length)

Here's another method without using recursive function calls. 这是不使用递归函数调用的另一种方法。

def ask_question(max_length, rowOrCol, guessNumber):
    while True:
        try:
            guess = int(raw_input("What is the "+rowOrCol+" number of your "+guessNumber+" guess? "))
            if guess <= max_length:
                return guess
            else:
                print "That number was too big, it must be no larger then " +str(max_length)
        except(ValueError):
            print "Only numbers are accepted, please try again!"

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

相关问题 谁能告诉我这里的代码有什么问题? - Can anyone tell me what's wrong with my code here? 谁能告诉我我的 dequeue function 有什么问题,它不会在运行后从队列或 output 中删除任何内容 - Can anyone tell me what's wrong with my dequeue function, it wont remove anything from the queue or output after running it 谁能告诉我Python中的合并排序有什么问题吗? - Can anyone tell me what's wrong with my Merge Sort in Python? 有人可以告诉我我的 animation 代码有什么问题吗? - Can someone tell me what's wrong with my animation code? 谁能告诉我这个列表理解有什么问题? - Can anyone tell me what is wrong with this list comprehension? 谁能帮我找出问题所在? 我的代码中出现索引错误 - Can anyone help me find out what's wrong? Index error in my code 您能告诉我代码有什么问题吗? - Could you tell me what's wrong with my code? 谁能告诉我快速排序代码中的错误是什么 - can anyone tell what's the bug in my quick sort code 有人能告诉我我的代码有什么问题吗 - Can someone tell me what is wrong with my code 谁能告诉我我做错了什么,字典与 Python 中的列表? - Can anyone tell me what I am doing wrong, dictionary vs list in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM