Indentation Error from main() in terminal

Question:

I’m new to using the main() part in Python3.

Error thrown:

python3 Artificial_snake_anti_venom.py
  File "Artificial_snake_anti_venom.py", line 67
    def main(): snake_families = get_all_snake_families() all_antivenoms = create_all_antivenoms(snake_families) save_antivenoms_file(all_antivenoms.txt) show_user_message() if __name__ == "__main__":
                                                          ^
SyntaxError: invalid syntax

Code:

#!/usr/bin/python

# Creating Synthetic Snake Antivenom 
import random 

# Create a function that can generate antivenom serum from snake venom 
def make_antivenom(snake_venom): 
  # Create a loop that can iterate through the snake venom and be used to create the antivenom 
  antivenom_serum = [] 
  for element in snake_venom: 
    # Create a sample of each element in the snake venom 
    sample = random.sample(element, len(element)) 
    # Append each sample to a serum stored in the list 
    antivenom_serum.append(sample) 
  
  # Return the complete antivenom serum once all samples have been appended 
  return antivenom_serum
  
# Call the function 
antivenom_serum = make_antivenom()

def make_antivenom():
    antivenom_serum = []
    # Append polyvalent antivenom
    polyvalent_serum = create_polyvalent_antivenom()
    antivenom_serum.append(polyvalent_serum)
    
    # Append a range of species specific antivenom
    for snake_species in get_all_snake_species():
        species_specific_serum = create_species_specific_antivenom(snake_species)
        antivenom_serum.append(species_specific_serum)
    
    # Append a range of polyspecific antivenom
    for snake_family in get_all_snake_families():
        polyspecific_serum = create_polyspecific_antivenom(snake_family)
        antivenom_serum.append(polyspecific_serum)
        
    return antivenom_serum
   
# Create a polyvalent antivenom serum
def create_polyvalent_antivenom():
    # code to create polyvalent antivenom

# Create a species specific antivenom serum
    def create_species_specific_antivenom(snake_species):
    # code to create species specific antivenom
 
# Create a polyspecific antivenom serum
        def create_polyspecific_antivenom(snake_family):
    # code to create polyspcecific antivenom
 
# Get a list of all snake species
            def get_all_snake_species():
    # code to get all snake species

# Get a list of all snake families
                def get_all_snake_families():
    # code to get list of all snake families here 
                    pass
    # Create dictionary with all antivenoms for snake species 
                    def create_all_antivenoms(): # code to generate antivenoms based on snake families pass # Loop over created antivenoms and save to file 
                            def save_antivenoms_file(): # loop over antivenom dict and save to file 
                                pass 
    # Display message to user 
                                def show_user_message(): print("All snake antivenoms have been generated!") 
    # Call main function 
                                def main(): snake_families = get_all_snake_families() all_antivenoms = create_all_antivenoms(snake_families) save_antivenoms_file(all_antivenoms.txt) show_user_message() if __name__ == "__main__":
                                main()

What I have done so far.

  • checked for empty spaces.
  • moved main() to new line.
  • tried changing all_antivenoms to all antivenoms.txt with and without quotes.

It is in the right directory; has been chmod +x.

All results are the same.

python3 Artificial_snake_anti_venom.py
  File "Artificial_snake_anti_venom.py", line 67
    def main(): snake_families = get_all_snake_families() all_antivenoms = create_all_antivenoms(snake_families) save_antivenoms_file(all_antivenoms.txt) show_user_message() if __name__ == "__main__":
                                                          ^
SyntaxError: invalid syntax
Asked By: Jay

||

Answers:

Did you intend to have the progressively increasing indentation?

Try first of all having all the def statements aligned to the left.

For example, this:

# Create a polyvalent antivenom serum
def create_polyvalent_antivenom():
    # code to create polyvalent antivenom

# Create a species specific antivenom serum
    def create_species_specific_antivenom(snake_species):
    # code to create species specific antivenom
 
# Create a polyspecific antivenom serum
        def create_polyspecific_antivenom(snake_family):
    # code to create polyspcecific antivenom

Did you intend to write this:

def create_polyvalent_antivenom():
    pass

def create_species_specific_antivenom(snake_species):
    pass
 
def create_polyspecific_antivenom(snake_family):
    pass

There is no need to write a comment describing what a function will do, when it is little more than restating the name of the function. That is what a good function name is aimed at achieving.

Usually we write a pass statement, which does nothing, for any function where we have not yet written the code.

It is conventional not to squash statements onto the end of the line that defines a function

Instead of:

                            def main(): snake_families = get_all_snake_families() all_antivenoms = create_all_antivenoms(snake_families) save_antivenoms_file(all_antivenoms.txt) show_user_message() if __name__ == "__main__":
                            main()

Try

def main(): 
    snake_families = get_all_snake_families() 
    all_antivenoms = create_all_antivenoms(snake_families)
    save_antivenoms_file(all_antivenoms.txt) 
    show_user_message() 


if __name__ == "__main__":
    main()
Answered By: Eureka
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.