[Solved] Why do I get this error in Python?

return print(char) This will first print the value of char (as in print(char)) and then try to return the return value of the print function. But since the print function always returns None—it only prints the value, it does not return it—your function will always return None causing the error when trying to concatenate None … Read more

[Solved] KeyError in String Formatting of Raw Input

You need to use named arguments to use their name in the template: “…{name}….”.format(name=name, quest=quest, color=color) If you use positional arguments, then you need to use index in template: “…{0}…”.format(name, quest, color) Documentation: https://docs.python.org/2/library/string.html#formatstrings solved KeyError in String Formatting of Raw Input

[Solved] Conditional Statement in python [closed]

Multiple conditional statements in Python can be done as follows: if condition1: statement elif condition2: statement elif condition2: statement else: statement Or if you wanted nested conditional statements: if condition1: if condition2: if condition3: statement else: statement elif condition4: if condition5: statement else: statement (This is just one example of some nested conditional statements) 0 … Read more

[Solved] Codecademy Battleship! Python

There is only a very small error in your code, from what I can see. The last if statement, which includes your “Game Over” string, is indented incorrectly. Move it left one block: else: print “You missed my battleship!” board[guess_row][guess_col] = “X” if turn == 3: print “Game Over” # Print (turn + 1) here! … Read more

[Solved] How to sort this list by the highest score?

You can use the sorted function with the key parameter to look at the 2nd item, and reverse set to True to sort in descending order. >>> sorted(data_list, key = lambda i : i[1], reverse = True) [[‘Jimmy’, [8]], [‘Reece’, [8]], [‘Zerg’, [5]], [‘Bob’, [4]]] 2 solved How to sort this list by the highest … Read more

[Solved] Can not convert 13 digit UNIX timestamp in python

#!python2 # Epoch time needs to be converted to a human readable format. # Also, epoch time uses 10 digits, yours has 13, the last 3 are milliseconds import datetime, time epoch_time = 1520912901432 # truncate last 3 digits because they’re milliseconds epoch_time = str(epoch_time)[0: 10] # print timestamp without milliseconds print datetime.datetime.fromtimestamp(float(epoch_time)).strftime(‘%m/%d/%Y — %H:%M:%S’) … Read more

[Solved] Imported but unused in python [closed]

In python, when you import a library, it is expected to be utilized in the code blocks. Hence, as it obviously states, you may have imported these but have not used all of them. Besides that being the obvious warning, your imports should have been import numpy as np NOT bumpy. And remove the unnecessary … Read more

[Solved] How to write this code without using pandas?

You can simply use the csv module from the standard library: import csv with open(‘__.csv’, ‘r’, newline=””) as f: reader = csv.reader(f) _ , *header = next(reader) d = {} for k, *row in reader: d[k] = dict(zip(header, row)) print(d) {‘reviews’: {‘JOURNAL_IMPACT_FACTOR’: 27.324, ‘IMPACT_FACTOR_LABEL’: ‘Highest’, ‘IMPACT_FACTOR_WEIGHT’: 37.62548387}, ‘hairdoos’: {‘JOURNAL_IMPACT_FACTOR’: 40.0, ‘IMPACT_FACTOR_LABEL’: ‘middle’, ‘IMPACT_FACTOR_WEIGHT’: 50.0}, ‘skidoos’: … Read more

[Solved] Why is Python strict about indentation? [closed]

Indentation is essential in Python; it replaces curly braces, semi-colons, etc. in C-like syntax. Consider this code snippet, for example: def f(x): if x == 0: print(“x is zero”) print(“where am i?”) If it weren’t for indentation, we would have no way of indicating what code block the second print statement belongs to. Is it … Read more

[Solved] Python: Reading marks from a file then using marks to get class

import csv # function to process data def process_data(data): for item in data: marks = int(item[2]) mclass=”incorrect data entry try again” if marks == 0: mclass = “10.9” elif marks >= 0 and marks <= 100: mclass = “10.{}”.format(10 – int(item[2][0])) yield ‘”{}”,”{}”,{},{}’.format(item[0],item[1],item[2],mclass) # read data to memory data = [] with open(“Maths_Mark.txt”, “r”) as … Read more

[Solved] How to get rid of “\n” and ” ‘ ‘ ” in my json file

The fundamental issue is that you’re creating multiple JSON strings (json_data = (json.dumps(response, indent=2))) and then adding them to an array (json_arr.append(json_data)), and then converting that array to JSON (json.dump(json_arr,outline)). So the result is a JSON array of strings (containing JSON). You don’t want to create a string before adding to the array, just include … Read more

[Solved] How to plot a math function from string?

You can turn a string into code by using pythons eval function, but this is dangerous and generally considered bad style, See this: https://stackoverflow.com/a/661128/3838691. If user can input the string, they could input something like import subprocess; subprocess.check_call([‘rm’, ‘-rf’, ‘*’], shell=True). So be sure that you build in reasonable security into this. You can define … Read more