must be str not int - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: must be str not int (/thread-5751.html) |
must be str not int - auting82 - Oct-19-2017 Trying to run this code, but getting the error: must be str not int? Need some help here Just to mention that i have tried to chabge the inputs to int, but then I get the error that only length-1 arrays can be converted to Python scalars N_0 = input('Give initial population size N_0: ') r = input('Give net growth rate r: ') dt = input('Give time step size: ') N_t = input('Give number of steps: ') from numpy import linspace, zeros t = linspace(0, (N_t+1)*dt, N_t+2) N = zeros(N_t+2) N[0] = N_0 for n in range(N_t+1): N[n+1] = N[n] + r*dt*N[n] from math import exp import matplotlib.pyplot as plt numerical_sol = 'bo' if N_t < 70 else 'b-' plt.plot(t, N, numerical_sol, t, N_0*exp(r*t), 'r-') plt.legend(['numerical', 'exact'], loc='upper left') plt.xlabel('t'); plt.ylabel('N(t)') filestem = 'growth1_%dsteps' % N_t plt.savefig('%s.png' % filestem); plt.savefig('%s.pdf' % filestem) RE: must be str not int - Larz60+ - Oct-19-2017 Please show full error traceback, it contains important information. RE: must be str not int - auting82 - Oct-20-2017 (Oct-19-2017, 08:57 PM)Larz60+ Wrote: Please show full error traceback, it contains important information. Hi again. Tested my code this morning in python and I kid you not I am able to generate a graph. However it is not correct as shown in the book I am learning from . Here is what I get: https://www.imageupload.co.uk/image/DId1 And here is what I should be getting: https://www.imageupload.co.uk/image/DIdg |