python NameError:name' xxx'没有定义

时间:2016-03-10 15:11:59

标签: python undefined nameerror

puzzle = [[' 1', ' 2', ' 3', ' 4'], [' 5', ' 6', ' 7', ' 8'],[ ' 9', '10', '11', '12'], ['13', '14', '15', ' X']]

def find_pos(alist, item):
    for i in alist:
        for j in range(4):
            if i[j] == item:
                row = alist.index(i)
                col = j

find_pos(puzzle,' X')

a = row
print(a)

我认为我通过运行row函数定义了名称find_pos,如果没有,如何修复它以获取row

请勿在{{1​​}}功能

中添加任何内容

1 个答案:

答案 0 :(得分:4)

只需返回函数中的值:

#import "AppDelegate.h"
@import CoreLocation;
@interface AppDelegate ()<CLLocationManagerDelegate, WCSessionDelegate>{
    CLLocationManager *locationManager;
    WCSession *session;
}
@end
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.
    if ([WCSession isSupported]) {
        [WCSession defaultSession].delegate = self;
        session = [WCSession defaultSession];
        session.delegate = self;
        NSLog(@"Sessiopn on phone starting");
        [[WCSession defaultSession] activateSession];
    }
    NSLog(@"iphone app starting");

 . . .
   return YES;
}
- (void)session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary<NSString *,id> *)message replyHandler:(nonnull void (^)(NSDictionary<NSString *,id> * __nonnull))replyHandler {

    // In this case, the message content being sent from the app is a simple begin message. This tells the app to wake up and begin sending location information to the watch.
    NSLog(@"Reached IOS APP");
}
-(void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message{
    NSLog(@"Reached IOS APP");
}

请注意,如果找不到该项,它将返回puzzle = [[' 1', ' 2', ' 3', ' 4'], [' 5', ' 6', ' 7', ' 8'],[ ' 9', '10', '11', '12'], ['13', '14', '15', ' X']] def find_pos(alist, item): for i in alist: for j in range(4): if i[j] == item: row = alist.index(i) col = j return row, col row, col = find_pos(puzzle,' X') print(row) (因为默认情况下,每个不返回任何内容的函数都返回None),在这种情况下代码将引发错误