Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a function in Python to generate a pie chart. I added an else conditions which prints a message in case the chart doesn't get generated.

The pie chart is generated when there's no 'else' statement. But including the else statement in the code isn't giving any output even if the conditions are met. This means I've not written the else statement properly.

Please let me know where I've gone wrong.

What I have tried:

def Piechart(self, file_path, rows, columns):
        global fig
        df = file_reading(file_path, rows)
        if len(columns) == 2:

          column1 = columns[0]
          column2 = columns[1]

        # Pie chart
          pie_ch = px.pie(df, values=column1, names=column2)
          pie_ch.write_html("templates/visualization.html")
        
        elif len(columns)< 2 or len(columns)>2 or type(column1)!= int or 
        type(column1)!= float:
            print("Please select 2 features only & the first feature should 
            be numerical")
Posted
Updated 30-Dec-22 0:07am

Try this
Python
if len(columns) == 2 and (type(columns[0]) == int or type(columns[0]) == float):
    # Pie chart
    pie_ch = px.pie(df, values=columns[0], names=columns[1])
    pie_ch.write_html("templates/visualization.html")
else:
    print("Please select 2 features only & the first feature should be numerical")
 
Share this answer
 
We can't tell - firstly, there is no else st\tement in that code so we have no idea what might be causing the problem, secondly because we can't run your code under the same circumstances you do - we have no idea what values are being passed, and no access to your file system to test it if we did!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Python debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900