Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
must be str not int
#1
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)
Reply
#2
Please show full error traceback, it contains important information.
Reply
#3
(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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020