python 获取语法错误:语法无效jupyter实验室[已关闭]

eimct9ow  于 2022-12-02  发布在  Python
关注(0)|答案(2)|浏览(144)

这个问题是由一个打字错误或一个无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
42分钟前关门了。
Improve this question
将这批代码拧成一股绳,并获取语法exxor,看起来有什么问题文件"",第2行if color == 1:color_sq =^语法错误:无效语法

def calc_color(data, color=None):
        if color   == 1: color_sq =  
                        ['#dadaebFF','#bcbddcF0','#9e9ac8F0',
                        '#807dbaF0','#6a51a3F0','#54278fF0']; 
                        colors = 'Purples';
        elif color == 2: color_sq = 
                        ['#c7e9b4','#7fcdbb','#41b6c4',
                        '#1d91c0','#225ea8','#253494']; 
                        colors = 'YlGnBu';
        elif color == 3: color_sq = 
                        ['#f7f7f7','#d9d9d9','#bdbdbd',
                        '#969696','#636363','#252525']; 
                        colors = 'Greys';
        elif color == 9: color_sq = 
                        ['#ff0000','#ff0000','#ff0000',
                        '#ff0000','#ff0000','#ff0000']
        else:            color_sq = 
                        ['#ffffd4','#fee391','#fec44f',
                        '#fe9929','#d95f0e','#993404']; 
                        colors = 'YlOrBr';
        new_data, bins = pd.qcut(data, 6, retbins=True, 
        labels=list(range(6)))
        color_ton = []
        for val in new_data:
            color_ton.append(color_sq[val]) 
        if color != 9:
            colors = sns.color_palette(colors, n_colors=6)
            sns.palplot(colors, 0.6);
            for i in range(6):
                print ("\n"+str(i+1)+': '+str(int(bins[i]))+
                       " => "+str(int(bins[i+1])-1), end =" ")
            print("\n\n   1   2   3   4   5   6")    
        return color_ton, bins;
bis0qfac

bis0qfac1#

关于python语法的几件事:

  • 压痕是必需的并且需要一致
  • :之后需要一个新行
  • ;

试试这个:

def calc_color(data, color=None):
    if color   == 1: 
      color_sq =  ['#dadaebFF','#bcbddcF0','#9e9ac8F0',
                    '#807dbaF0','#6a51a3F0','#54278fF0'] 
      colors = 'Purples'
    elif color == 2: 
      color_sq = ['#c7e9b4','#7fcdbb','#41b6c4',
                    '#1d91c0','#225ea8','#253494'] 
      colors = 'YlGnBu'
    elif color == 3: 
      color_sq = ['#f7f7f7','#d9d9d9','#bdbdbd',
                    '#969696','#636363','#252525'] 
      colors = 'Greys'
    elif color == 9: 
      color_sq = ['#ff0000','#ff0000','#ff0000',
                    '#ff0000','#ff0000','#ff0000']
    else:            
      color_sq = ['#ffffd4','#fee391','#fec44f',
                    '#fe9929','#d95f0e','#993404'] 
      colors = 'YlOrBr'
    new_data, bins = pd.qcut(data, 6, retbins=True, labels=list(range(6)))
    color_ton = []
    for val in new_data:
        color_ton.append(color_sq[val]) 
    if color != 9:
        colors = sns.color_palette(colors, n_colors=6)
        sns.palplot(colors, 0.6)
        for i in range(6):
            print ("\n"+str(i+1)+': '+str(int(bins[i]))+
                   " => "+str(int(bins[i+1])-1), end =" ")
        print("\n\n   1   2   3   4   5   6")    
    return color_ton, bins

我建议你看看像this这样的东西。

f87krz0w

f87krz0w2#

你以前写过python吗?:D
我重新格式化了你的代码,现在没有任何语法错误:

def calc_color(data, color=None):
    if color   == 1: 
        color_sq = ['#dadaebFF','#bcbddcF0','#9e9ac8F0',
                    '#807dbaF0','#6a51a3F0','#54278fF0']
        colors = 'Purples'
    elif color == 2: 
        color_sq = ['#c7e9b4','#7fcdbb','#41b6c4',
                    '#1d91c0','#225ea8','#253494']
        colors = 'YlGnBu'
    elif color == 3: 
        color_sq = ['#f7f7f7','#d9d9d9','#bdbdbd',
                    '#969696','#636363','#252525']
        colors = 'Greys'
    elif color == 9: color_sq = ['#ff0000','#ff0000','#ff0000',
                    '#ff0000','#ff0000','#ff0000']
    else: 
        color_sq = ['#ffffd4','#fee391','#fec44f',
                    '#fe9929','#d95f0e','#993404']
    colors = 'YlOrBr'
    new_data, bins = pd.qcut(data, 6, retbins=True, 
    labels=list(range(6)))
    color_ton = []
    for val in new_data:
        color_ton.append(color_sq[val]) 
    if color != 9:
        colors = sns.color_palette(colors, n_colors=6)
        sns.palplot(colors, 0.6);
        for i in range(6):
            print ("\n"+str(i+1)+': '+str(int(bins[i]))+
                   " => "+str(int(bins[i+1])-1), end =" ")
        print("\n\n   1   2   3   4   5   6")    
    return color_ton, bins;

相关问题