0

I have looked at other questions but I dont believe these apply to my situation - as I cannot defer what I need to do from them.

To sum up the bit of code with issue:

import numpy as np
noInvestments=int(input("How many types of savings will you have? "))
savingTypes=[np.empty(())]
interests=np.empty(())
years=np.empty(())
contributions=np.empty(())
balance=np.empty(())


for i in range(0,noInvestments):
    totalContributions=0
    totalYears=0
    p=float(input("What is your principal? "))
    name=str(input("What is the savings type name? "))
    #save savings type into array
    savingTypes[i]=name
    interest=float(input("What is the interest? (%) "))
    #save interest into array
    interests[i]=interest
    z=1+(interest/100)

I want to be able to pull the array, after the loop is done, and have corresponding names of the accounts and the interest rates. I have been trying different things, and last time I ran it, the savingTypes[i]=name worked perfectly, and the interest one came with the error again? I cannot see anything different apart from how they are stored and this hasn't changed it before?

Hopefully someone is able to help! I would be very grateful!

  • ```interests=[np.empty(())]``` will work for 1 ```noInvestments``` but not for more because the size of list never increases and you assign using index. it is better to append rather than assign index number. or define the size of list beforehand – Yash Oct 20 '20 at 12:56
  • In your own words, what do you think `np.empty()` means? What's your plan for using these objects, and why do you expect them to help you solve a problem? If you're trying to use them to have a dummy initial value for a variable, please note that [there is no such thing as variable declaration or initialization in Python](https://stackoverflow.com/questions/11007627/python-variable-declaration/11008311#11008311), and also that Python has a built-in "no value" object (called `None`) anyway. – Karl Knechtel Oct 20 '20 at 12:59
  • In fact, I don't understand why you are using Numpy at all. The built-ins are perfectly fine for doing ordinary arithmetic. – Karl Knechtel Oct 20 '20 at 13:01
  • That said, if you really "cannot see anything different apart from how they are stored" then you need to pay more attention. `savingTypes=[np.empty(())]`, `interests=np.empty(())` - nothing seems to be different about the right-hand sides of the two `=` signs? Really? – Karl Knechtel Oct 20 '20 at 13:02
  • @YashShah Thanks for your reply, do you know how I would increase the size of the list every time it loops? – louis george Oct 20 '20 at 13:12
  • I solved this by, instead of writing interests[i]=interest, and replacing it with interests.extend(interest). This now leads to the arrays being every single letter rather than the whole words – louis george Oct 20 '20 at 14:34
  • 1
    Use `interests=[]` and `interests.append(...)` (or `extend`). `np.empty(())` is useless, even harmful for your health! – hpaulj Oct 20 '20 at 17:13

0 Answers0