resistance calculator saying index out of range - python

I'm currently trying to write a program that gathers all resistors, calculates, then gets voltage and then calculates current. Here is what I have so far:
resistors = [0]
def ObtainResistors(resistors):
for i in range(1, 8):
value = int(input('please enter resistor %d:' % i))
resistors.append(value)
return
def TotalResistance(resistors):
rt1 = ((resistors[2] * resistors[3]) / (resistors[2] + resistors[3]))
print(rt1, 'ohms')
rt2 = (rt1 + resistors[4])
print(rt2, 'ohms')
rt3 = ((rt2 * resistors[5]) / (rt2 + resistors[5]))
print(rt3, 'ohms')
rt4 = (rt3 + resistors[7])
print(rt4, 'ohms')
rt5 = ((rt4 * resistors[6]) / (rt4 + resistors[6]))
print(rt5, 'ohms')
rt6 = (rt5 + resistors[1])
print(rt6, 'ohms')
rt = (rt1 + rt2 + rt3 + rt4 + rt5 + rt6)
print(rt, 'ohms')
return
ObtainResistors(resistors)
print(resistors)
TotalResistance(resistors)
print(resistors)
This is what i get when i run
please enter resistor 1:1
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/Introduction_to_Python/RESISTOR COURSEWORK.py", line 31, in <module>
TotalResistance(resistors)
File "C:/Users/User/PycharmProjects/Introduction_to_Python/RESISTOR COURSEWORK.py", line 12, in TotalResistance
rt1 = ((resistors[2] * resistors[3]) / (resistors[2] + resistors[3]))
IndexError: list index out of range
[0, 1]
Process finished with exit code 1

You are only reading 1 resistor value if you return in loop.
Move the return out of loop, or remove it since Python doesn't require explicit return and you're not returning a value:
def ObtainResistors(resistors):
for i in range(1, 8):
value = int(input('please enter resistor %d:' % i))
resistors.append(value)
# return

Related

Python multiprocessing shared memory without copy

I'm currently working on a project for fun, involving calculating way too many numbers of the Fibonacci sequence. Thing is, I want it to go fast and I also don't want to loose too much progress if I have to do an update or have a computer issues....
So I over-engineered that, and I am currently looking to implement it with multiprocessing, but everywhere I look the only way to share memory in that case is to make a duplicate of it and that takes about an hour for (Yeah I have very big numbers)
Is there any way to share a dictionary with multiprocessing without making a copy of it?
Here is the code, currently with my last implementation using threading instead of using multiprocessing. (ignore the bad timekeeping of this, i'll fix it later)
import threading
import time
def printProgressBar(iteration, total, prefix="", suffix="", decimals=1, length=100, fill="█", printEnd="\r"):
"""
Call in a loop to create terminal progress bar
#params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + "-" * (length - filledLength)
print(
f"\r{prefix} |{bar}| {percent}% {suffix} {time.strftime('%dd %H:%M:%S', time.gmtime(time.time() - start_time - 86400)).replace('31d', '0d')}",
end=printEnd,
)
# Print New Line on Complete
if iteration == total:
print()
dict47 = {0: 0, 1: 1, 2: 1}
dict47[0] = 2
def fibSequence(n):
if n not in dict47:
dict47[n] = fibSequence(n - 1) + fibSequence(n - 2)
if n > dict47[0]:
dict47[0] = n
if n > 5 and not ((n - 3) % (iterations // 100) == 0) and not ((n - 2) % (iterations // 100) == 0):
dict47.pop(n - 3, None)
return dict47[n]
def makeBackup(start, num, total):
x = threading.Thread(target=writeBackup, args=(num,), daemon=False)
y = threading.Thread(target=writeBackup, args=(num - 1,), daemon=False)
x.start()
y.start()
x.join()
y.join()
time.sleep(1)
print(
f'{num/10000000}% done after {time.strftime("%dd %H:%M:%S", time.gmtime(time.time() - start - 86400)).replace("31d", "0d")}'
)
timings = open("times.txt", "a")
timings.write(str(int(time.time() - start)) + "\n")
timings.close()
def writeBackup(num):
file = open(f".temp/fib{num}.txt", "a")
file.write(str(num) + " : " + str(dict47[num]))
file.close()
dict47.pop(num, None)
def loadDict():
from pathlib import Path
maximum = 0
for n in range(1, 100):
if Path(f".temp/fib{n*10000000}.txt").is_file():
maximum = n * 10000000
print("Maximum number found:", maximum)
if maximum != 0:
file = open(f".temp/fib{maximum}.txt", "r")
temp = "".join(file.readlines())
dict47[maximum] = int(temp.lstrip(str(maximum) + " : "))
file.close()
file = open(f".temp/fib{maximum - 1}.txt", "r")
temp = "".join(file.readlines())
dict47[maximum - 1] = int(temp.lstrip(str(maximum - 1) + " : "))
file.close()
dict47[0] = maximum
print("Dictionary loaded at ", maximum)
else:
print("No dictionary found, starting from scratch")
if __name__ == "__main__":
try:
timings = open("times.txt", "r")
lastTime = int(timings.readlines()[-1])
start_time = time.time() - lastTime
timings.close()
except:
start_time = time.time() + 86400
print("Start duration:", time.strftime("%dd %H:%M:%S", time.gmtime(time.time() - start_time)).replace("31d", "0d"))
try:
iterations = int(input("Enter the number of iterations: "))
except:
iterations = 1000000000
print(iterations, "iterations will be performed")
loadDict()
num = dict47[0]
while num < iterations:
if num == 2:
num += 248
else:
num += 250
fibSequence(num)
if num % 1000 == 0:
printProgressBar(num, iterations, prefix="Progress:", suffix="Complete", length=100)
if num % (iterations // 100) == 0:
save = threading.Thread(
target=makeBackup,
args=(start_time, num, iterations),
daemon=False,
)
save.start()
file = open("fib.txt", "a")
file.write(str(iterations) + " : " + str(fibSequence(iterations)) + "\n")
file.close()
try:
save.join()
except:
print("No save thread running, exiting...")

IndexError: invalid index to scalar variable.- PSO algorithm

While trying to solve this problem using PSO algorithm I got the error message as "IndexError: invalid index to scalar variable". Since I am new to PSO algorithm, I am unable to figure out the error. Kindly help me to solve the issue.
import random
import numpy as np
import math
import matplotlib.pyplot as plt
def fitness_function(position):
s1=0.014109786*position[0] + 0.004596846*position[1] + 0.01603721*position[2] + 0.029275618*position[3] + 0.007085358*position[4] + 0.013234328*position[5] + 0.012554958*position[6] + 0.012447232*position[7] + 0.007867602*position[8] + 0.011312568*position[9] + 0.003087858*position[10] + 0.016566954*position[11] + 0.008428942*position[12] + 0.008477444*position[13] + 0.004357354*position[14]
s2=0.016566954*position[0] + 0.00585045*position[1] + 0.053172638*position[2] + 0.113404042*position[3] + 0.028190744*position[4] + 0.046330688*position[5] + 0.05629084*position[6] + 0.047796486*position[7] + 0.025793022*position[8] + 0.046164518*position[9] + 0.026696192*position[10] + 0.080422654*position[11] + 0.029074508*position[12] + 0.039611624*position[13] + 0.044835566*position[14]
return s1+s2
#Some variables to calculate the velocity
W = 0.5
c1 = 0.8
c2 = 0.9
target = 1
n_iterations = int(input("Inform the number of iterations: "))
target_error = float(input("Inform the target error: "))
n_particles = int(input("Inform the number of particles: "))
particle_position_vector=np.array([np.random.uniform(low=0.025, high=0.035) for i in range (n_particles)])
print(particle_position_vector)
pbest_position = particle_position_vector
pbest_fitness_value = float('inf')
gbest_fitness_value = float('inf')
gbest_position = np.array([float('inf')for _ in range(n_particles)])
velocity_vector =np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0])
print(velocity_vector)
iteration = 0
while iteration < n_iterations:
for i in range(n_particles):
fitness_cadidate = fitness_function(particle_position_vector[i])
print(fitness_cadidate, ' ', particle_position_vector[i])
if(pbest_fitness_value[i] > fitness_cadidate):
pbest_fitness_value[i] = fitness_cadidate
pbest_position[i] = particle_position_vector[i]
if(gbest_fitness_value > fitness_cadidate):
gbest_fitness_value = fitness_cadidate
gbest_position = particle_position_vector[i]
if(abs(gbest_fitness_value - target) < target_error):
break
for i in range(n_particles):
new_velocity = (W*velocity_vector[i]) + (c1*random.random()) * (pbest_position[i] - particle_position_vector[i]) + (c2*random.random()) * (gbest_position-particle_position_vector[i])
new_position = new_velocity + particle_position_vector[i]
particle_position_vector[i] = new_position
iteration = iteration + 1
print("The best position is ", gbest_position, "in iteration number ", iteration)```
```IndexError Traceback (most recent call last)
<ipython-input-35-5610603d3302> in <module>
32 while iteration < n_iterations:
33 for i in range(n_particles):
---> 34 fitness_cadidate = fitness_function(particle_position_vector[i])
35 print(fitness_cadidate, ' ', particle_position_vector[i])
36
<ipython-input-35-5610603d3302> in fitness_function(position)
5
6 def fitness_function(position):
----> 7 s1=0.014109786*position[0] + 0.004596846*position[1] + 0.01603721*position[2] + 0.029275618*position[3] + 0.007085358*position[4] + 0.013234328*position[5] + 0.012554958*position[6] + 0.012447232*position[7] + 0.007867602*position[8] + 0.011312568*position[9] + 0.003087858*position[10] + 0.016566954*position[11] + 0.008428942*position[12] + 0.008477444*position[13] + 0.004357354*position[14]
8 s2=0.016566954*position[0] + 0.00585045*position[1] + 0.053172638*position[2] + 0.113404042*position[3] + 0.028190744*position[4] + 0.046330688*position[5] + 0.05629084*position[6] + 0.047796486*position[7] + 0.025793022*position[8] + 0.046164518*position[9] + 0.026696192*position[10] + 0.080422654*position[11] + 0.029074508*position[12] + 0.039611624*position[13] + 0.044835566*position[14]
9 return s1+s2
IndexError: invalid index to scalar variable.
Moreover, I want to plot a graph of fitness value with the iteration numbers. Please help me.

Unwanted line break is occuring in python

I am want to replace a specific part of the string while using textwrap. But I am getting an unwanted line break. I have tried a lot of ways but still, it is not working. here is my code.
import textwrap
import math
def wrap(string, max_width):
wrapper = textwrap.TextWrapper(width = max_width)
word_list = wrapper.fill(text = string)
return word_list
height, width = map(int,input().split())
square = height * width
i = 0
x = []
while i < square:
x.append("-")
i += 1
k = 0
while k < math.floor(height / 2):
line = math.floor(width / 2) + width * k
x[line] = '|'
x[line + 1]= "."
x[line - 1] = "."
for h in range(1, k + 1):
f = h * 3
line_plus = line + f
line_minus = line - f
x[line_plus] = '|'
x[line_minus] = '|'
x[line_minus - 1] = '.'
x[line_plus - 1] = '.'
x[line_minus + 1] = '.'
q = line_plus + 1
x[q] = '.'
k += 1
a = 0
while a < math.floor(height / 2):
line = math.floor(width / 2) + width * a
line_end = (math.floor(width / 2) + width * a) * (-1)
x[line_end - 1] = '|'
if line > width:
x[line_end + 2] = '|'
x[line_end - 4] = '|'
a += 1
listToStr = ''.join([str(elem) for elem in x])
welcome_pos = math.floor(height / 2) * width + (math.floor(width / 2) - math.floor(7 / 2))
s = listToStr[ 0: welcome_pos] + "welcome" + listToStr[welcome_pos + 7:]
print(wrap(listToStr, width) + "\n")
print(wrap(s, width))
my input is 7 21
Output:
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.
----------welcome----
----------|--|--|----
----------|--|--|----
-------------|-------
---
this is my output. But it is giving a space at line 3 which I don't want. I don't know why this is happening. Please help me.
Maybe this is what you want?
code:
#(above the same)
print(wrap(listToStr,width*height))
print(wrap(s,width*height))
result:
5 4
-..|..|..||--|--||--
-..|..|welcome--||--

Global variables aren't working

My global variables are not working in my code. I'm fairly new to this and I can't seem to figure this out: I have set variables (only showing gna for this), which can be manipulated by an entry field, triggered by a corresponding button. For some reason, it's not taking the changes within the loop. I'm trying to make it to where the changed variable can be graphed as well, but it gives me the following error:
Exception in Tkinter callback Traceback (most recent call last):
File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args) File "G:/PYTHON/Eulers.py", line 64, in graph
v[i + 1] = 1 / c * (gna * f[i] - gk * u[i]) * del_t + v[i]
TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('< U32') dtype('< U32') dtype('< U32')
Here is the code:
gna = 0.9
gnalabel = Label(topFrame, text="gna = %s" % gna)
gnalabel.pack()
gnaEntry = Entry(topFrame, justify=CENTER)
gnaEntry.pack()
def gnacallback():
global gna
gna = gnaEntry.get()
gnalabel.config(text="C = %s" % gna)
gnaButton = Button(topFrame, text="Change", width=10, command=gnacallback)
gnaButton.pack()
def graph():
global c, gna, gk, beta, gamma
for i in range(0, len(t)-1):
stinum = np.floor(i / 3000)
stimt = 3000 + 3000 * (stinum - 1)
f[i] = v[i] * (1 - (((v[i]) ** 2) / 3))
v[i + 1] = 1 / c * (gna * f[i] - gk * u[i]) * del_t + v[i]
if(i == stimt):
v[i + 1] = v[i + 1] + v_stim
u[i + 1] = (v[i] + beta - gamma * u[i]) * del_t + u[i]
plt.plot(v)
plt.show()
gna = gnaEntry.get()
Entry.get returns a string, which is probably an unsuitable type for the arithmetic you're doing in graph. Try converting to a number first.
gna = float(gnaEntry.get()) #or perhaps `int` if it's always an integer

Index out of range error Python greedy method

Hello I am currently getting an index out of range error from the following code: (I will post the code first and then the error)
Main File:
import Knapsack_Test
size = 10
W = 2*size
knapsack = Knapsack_Test.Knapsack_Test()
for i in range(1, 10):
knapsack.greedy_knapsack_test(size, W)
size = size + 10*i
W = 2*size
Class File (Only the greedy function):
def greedy_knap(self, v, w, W):
knap_array = []
for i in range(1, len(v)):
#The index out of range error occurs here:
knap_array[i] = [v[i],w[i]]
sort_order = self.sort.merge_sort(knap_array)
weight = 0
value = 0
knap_sac= []
n = len(knap_array)
j = 0
profit = 0
while weight < W and j < n:
if weight + knap_array[i][1] <= W:
knap_sac.append(knap_array[i])
weight = weight + knap_array[i][1]
profit = profit + knap_array[i][0]
j = j + 1
return profit
The test File (for greedy function):
def greedy_knapsack_test(self, size, W):
v = []
w = []
for i in range(1,size):
v.append(random.randint(1,1000))
for i in range(1,size):
w.append(random.randint(1,1000))
start = time.time()
self.knapsack.greedy_knap(v, w, W)
end = time.time() - start
return end
The Error:
Traceback (most recent call last):
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\Knapsack_Main.py", line 10, in <module>
knapsack.greedy_knapsack_test(size, W)
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\Knapsack_Test.py", line 31, in greedy_knapsack_test
self.knapsack.greedy_knap(v, w, W)
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 30, in greedy_knap
knap_array[i] = [v[i],w[i]]
IndexError: list assignment index out of range
knap_array = []
for i in range(1, len(v)): #The index out of range error occurs here:
knap_array.append([v[i],w[i]])
you can't create list element by referencing them.

Categories

Resources