[Solved] How to name a file using a value in a list in a dictionary?

Something like this: dictionary = {‘results’: [{‘name’: ‘sarah’, ‘age’: ’18’}]} name = dictionary[‘results’][0][‘names’] + ‘.txt’ open(name, “w”) Opening a file in the write mode, if it doesn’t exist already, will create a new file with that name. 2 solved How to name a file using a value in a list in a dictionary?

[Solved] How do I repeat the program? [closed]

Use while like example. Don’t use int for input – maybe it will not number: while 1: math = input(“What Is 8 x 4: “) if not math.isdigit(): print(“It’s not number”) elif math == “32”: print(“You Got The Question Correct”) break else: print(“Sorry You Got The Question Wrong Try Again”) 2 solved How do I … Read more

[Solved] take elements of dictionary to create another dictionary

You could do this: a = {‘vladimirputin’: {‘milk’: 2.87, ‘parsley’: 1.33, ‘bread’: 0.66}, ‘barakobama’:{‘parsley’: 0.76, ‘sugar’: 1.98, ‘crisps’: 1.09, ‘potatoes’: 2.67, ‘cereal’: 9.21}} b = {} for prez in a: for food in a[prez]: if food not in b: b[food] = {prez: a[prez][food]} else: b[food][prez] = a[prez][food] This gives: {‘bread’: {‘vladimirputin’: 0.66}, ‘cereal’: {‘barakobama’: 9.21}, … Read more

[Solved] How can I fix this program? (python) [closed]

Try this: # -*- coding: utf-8 -*- max_value = 5 #The main function, it only summons the other functions and gets an input for num1. #If the value entered in the functions is not ok, a loop happens in the main function. def main(): global max_value #This line requests from you to put an input … Read more

[Solved] intersection of two or more lists of dicts

You can convert the list of dicts to set of tuples of dict items so that you can use functools.reduce to perform set.intersection on all the sets, and then convert the resulting sequence of sets to a list of dicts by mapping the sequence to the dict constructor: from functools import reduce def intersection(*lists): return … Read more

[Solved] How to print the input string as a float (1 into 1.0) python

Try print(‘{} rods.’.format(num_rods)) or print(‘rods: ‘,num_rods) Also, be careful with statements of this type. Check what happens to the code if, instead of inputting an int or float, you pass the character ‘a’ for example. 2 solved How to print the input string as a float (1 into 1.0) python

[Solved] ValueError: math domain error

Your traceback indicates you are passing a negative number to the math.sqrt() function: >>> from math import sqrt >>> sqrt(4.5 – 5.0) Traceback (most recent call last): File “<stdin>”, line 1, in <module> ValueError: math domain error >>> sqrt(-1.0) Traceback (most recent call last): File “<stdin>”, line 1, in <module> ValueError: math domain error Don’t … Read more

[Solved] Can someone help me rewrite this code (fig with multiple histograms) with a for loop or function?

You could create a list with the years and iterate over it: years = [‘1990’, ‘1995’, ‘2000’, ‘2005’, ‘2010’, ‘2015’] plot_i = 1 for y in years: data = mmr[mmr[‘Year’]==y] ax = fig.add_subplot(2,3,plot_i) ax.hist(data[‘MMR’], color=”darkred”, edgecolor=”white”) ax.set_title(y + ‘ MMR Distribution’, fontweight=”bold”, loc=”left”) ax.spines[‘left’].set_visible(False) ax.spines[‘top’].set_visible(False) ax.spines[‘right’].set_visible(False) ax.spines[‘bottom’].set_visible(False) ax.tick_params(length=0) ax.set_xlim(0,3000,500) plot_i = plot_i + 1 1 … Read more

[Solved] Python and setting arguments [closed]

Without knowing how the python file works, we cannot tell you how to use it, but it actually tells you in the error message already quite a bit. It looks like you should run something like: python tcp_deadlock.py {server,client} 123.123.123.123 where you use a hostname (to connect to?) instead of 123.123.123.123. solved Python and setting … Read more

[Solved] How to copy linked list in Python?

this should be working: import copy class _ListNode: def __init__(self, value, next_): self._value = copy.deepcopy(value) self._next = next_ return class List: def __init__(self): self._front = None self._count = 0 return def addToFront(self, value): if self._front == None: self._front = _ListNode(value, None) else: buffer = _ListNode(value, self._front) self._front = buffer def addToEnd(self, value): current = self._front … Read more

[Solved] How to convert this tuple to a dict in python

You need only the first element of the tuple. Split this element at the ‘: ‘ and you’ll get a list with the two parts you need. data = (‘”https://www.dzone.com”: “Z”\n\n’, ”)[0].split(‘: ‘) Now construct your dictionary by using strip to remove the unwanted characters at the beginning and end of each string. result = … Read more