-3

这是我的代码:

import random

min_num = 1
max_num = input("Pick your maximum number: ")
used_num = []

def rndnum(used_num):
    a = random.randint(min_num,max_num)
    b = random.randint(min_num,max_num)
    num_string = str(a) + "," + str(b)
    if num_string not in used_num:
        return a,b,num_string
    else:
        rndnum(used_num)

def to_continue(used_num):
    d = raw_input("Press [Enter] to Continue")
    ask(used_num)

def finished():
    print "Congratulations, you are finished!"

def ask(used_num): 
    a,b,num_string = rndnum(used_num)
    c = input(str(len(used_num)+1) + ". What is " + str(a) + " times " + str(b) + "? ")
    if c == a*b:
        used_num.append(num_string)
        print "That is correct! Only " + str(max_num**2 - len(used_num)) + " remaining!"
        if len(used_num) == max_num**2:
            finished()
        else:
        to_continue(used_num)
    else:
        print "That is incorrect."
        to_continue(used_num)

ask(used_num)

这是显示的错误:

Traceback (most recent call last):
   File "/home/philip/Documents/Aptana Studio 3 Workspace/Eli Tests/helloworld.py", line 37, in <module>
    ask(used_num)
   File "/home/philip/Documents/Aptana Studio 3 Workspace/Eli Tests/helloworld.py", line 32, in ask
   to_continue(used_num)
  File "/home/philip/Documents/Aptana Studio 3 Workspace/Eli Tests/helloworld.py", line 18, in to_continue
    ask(used_num)
  File "/home/philip/Documents/Aptana Studio 3 Workspace/Eli Tests/helloworld.py", line 32, in ask
    to_continue(used_num)
   File "/home/philip/Documents/Aptana Studio 3 Workspace/Eli Tests/helloworld.py", line 18, in to_continue
    ask(used_num)
  File "/home/philip/Documents/Aptana Studio 3 Workspace/Eli Tests/helloworld.py", line 32, in ask
     to_continue(used_num)
  File "/home/philip/Documents/Aptana Studio 3 Workspace/Eli Tests/helloworld.py", line 18, in to_continue
    ask(used_num)
   File "/home/philip/Documents/Aptana Studio 3 Workspace/Eli Tests/helloworld.py", line 32, in ask
    to_continue(used_num)
  File "/home/philip/Documents/Aptana Studio 3 Workspace/Eli Tests/helloworld.py", line 18, in to_continue
     ask(used_num)
   File "/home/philip/Documents/Aptana Studio 3 Workspace/Eli Tests/helloworld.py", line 24, in ask
    a,b,num_string = rndnum(used_num)
 TypeError: 'NoneType' object is not iterable

出了什么问题?谢谢!

4

1 回答 1

4

您在函数return的最后一行忘记了 a 。rndnum它不返回值,而是隐式返回None。下面的固定代码。

def rndnum(used_num):
    a = random.randint(min_num,max_num)
    b = random.randint(min_num,max_num)
    num_string = str(a) + "," + str(b)
    if num_string not in used_num:
        return a,b,num_string
    else:
        return rndnum(used_num)
于 2013-01-13T22:42:29.580 回答