error handing

    xiaoxiao2022-07-14  109

    # Define shout_echo def shout_echo(word1, echo=1): """Concatenate echo copies of word1 and three exclamation marks at the end of the string.""" # Initialize empty strings: echo_word, shout_words echo_word, shout_words = ('', '') # Add exception handling with try-except try: # Concatenate echo copies of word1 using *: echo_word echo_word = word1 * echo # Concatenate '!!!' to echo_word: shout_words shout_words = echo_word + '!!!' except: # Print error message print("word1 must be a string and echo must be an integer.") # Return shout_words return shout_words # Call shout_echo shout_echo("particle", echo="accelerator")

    <script.py> output: word1 must be a string and echo must be an integer.

    # Define shout_echo def shout_echo(word1, echo=1): """Concatenate echo copies of word1 and three exclamation marks at the end of the string.""" # Raise an error with raise if echo < 0: raise ValueError('echo must be greater than 0') # Concatenate echo copies of word1 using *: echo_word echo_word = word1 * echo # Concatenate '!!!' to echo_word: shout_word shout_word = echo_word + '!!!' # Return shout_word return shout_word # Call shout_echo shout_echo("particle", echo=5) # Define count_entries() def count_entries(df, col_name='lang'): """Return a dictionary with counts of occurrences as value for each key.""" # Initialize an empty dictionary: cols_count cols_count = {} # Add try block try: # Extract column from DataFrame: col col = df[col_name] # Iterate over the column in dataframe for entry in col: # If entry is in cols_count, add 1 if entry in cols_count.keys(): cols_count[entry] += 1 # Else add the entry to cols_count, set the value to 1 else: cols_count[entry] = 1 # Return the cols_count dictionary return cols_count # Add except block except: print('The DataFrame does not have a ' + col_name + ' columns.') # Call count_entries(): result1 result1 = count_entries(tweets_df, 'lang') # Print result1 print(result1) # Define count_entries() def count_entries(df, col_name='lang'): """Return a dictionary with counts of occurrences as value for each key.""" # Raise a ValueError if col_name is NOT in DataFrame if col_name not in df.columns: raise ValueError('The DataFrame does not have a ' + col_name + ' column.') # Initialize an empty dictionary: cols_count cols_count = {} # Extract column from DataFrame: col col = df[col_name] # Iterate over the column in DataFrame for entry in col: # If entry is in cols_count, add 1 if entry in cols_count.keys(): cols_count[entry] += 1 # Else add the entry to cols_count, set the value to 1 else: cols_count[entry] = 1 # Return the cols_count dictionary return cols_count # Call count_entries(): result1 result1 = count_entries(tweets_df, 'lang') # Print result1 print(result1)
    最新回复(0)