0

How do I append a single element preferable a floating value from a user input to a numpy array. The code I have written below just prints out an empty array each time and I am having trouble understanding why nothing is being appended.

import numpy as np

start=0
start_prompt = int(input("Start press 1"))
while start_prompt > start:
   x=np.array([])
   y = float(input("Please input number:  "))
   if y > 0: 
       np.append(x,y)
   print(x)
DavidG
  • 24,279
  • 14
  • 89
  • 82
D.Bhula
  • 1
  • 2
  • 5
    I would just use python lists for this, there is no need for a numpy array here. – roganjosh Oct 30 '17 at 10:16
  • 1
    This is an infinite loop too.... – DavidG Oct 30 '17 at 10:19
  • Do not use np.append - especially without reading its documentation. – hpaulj Oct 30 '17 at 10:27
  • 1
    @roganjosh is correct. Appending to a list is much faster than appending to an array. If you really want to use arrays preallocate the elements, see some rough benchmarks I put together here: https://stackoverflow.com/questions/46860970/why-use-numpy-over-list-based-on-speed/46868693#46868693 – user2699 Oct 30 '17 at 12:24

2 Answers2

0

You have to change your array, right? You'll have to write it as :

x = np.append(x, y)

>>> while start_prompt > start:
...     x=np.array([])
...     y = float(input("Please input number:  "))
...     if y > 0: 
...         x = np.append(x, y)
...     print(x)

Also, firstly you're running an infinite loop because start_prompt will always be greater than start. You made your x array inside the loop and it will be reinitialized on each iteration. If you want it to work as expected then declare it outside the while loop. Secondly, there are a lot better ways to do what you're trying to do.

pissall
  • 7,109
  • 2
  • 25
  • 45
  • 1
    You will need to move `x=np.array([])` outside of the loop. – DavidG Oct 30 '17 at 10:22
  • I know it's an infinite loop and all, but the value of x will still change. – pissall Oct 30 '17 at 10:23
  • @pissall but you keep undoing your work by redefining the array as an empty array on every iteration of the loop. There is no meaningful output of your code. – roganjosh Oct 30 '17 at 10:23
  • My comment on your answer was not to do with the infinite loop. You keep initialising `x` to an empty array – DavidG Oct 30 '17 at 10:24
  • I know. But you're forgetting the question asked. The context is, why his array is not changing. Which I have answered. I've got nothing to do with what all happens otherwise. – pissall Oct 30 '17 at 10:25
  • @pissall fair enough. You could put a little comment at the bottom of your answer saying this? – DavidG Oct 30 '17 at 10:25
0

Hi all thanks for the replies. I have taken what you have suggested and went through a bit of trial and error with all the suggestion and got it working the way I intended so a big thanks to all the commenters. Here is the code that works (Hopefully theres no errors during the copy and paste)

def get_user_values1(x):
    x = np.array([])
    initial = float(input("Input the cup weight in grams:"))
    while initial <= 0:
        #print ("Invalid")
        initial = float(input("Input the cup weight in grams:"))
        x=np.append(x,initial)
    else:
        x=np.append(x,initial)
    return (x)



def main():
    x = np.array([])
    start = 1 #1 = yes stasrt script
    start_prompt = int(input("To start press 1, To Close press 0: "))
    while start_prompt == start:    
        get_user1 = get_user_values1 (x)
        x = np.append(x,get_user1)




main()
D.Bhula
  • 1
  • 2