0
try:
    target = int(input("Please enter the ammount you are looking for :"))
except ValueError:
    print("wrong value please enter a number")
    target = int(input("Please enter the ammount you are looking for :"))
found = False
location = [] # I want to use this list as position argument for another array passed from another function. is it possible?


for pos in range(0, len(wydatki)):
    if wydatki[pos] == target:
        found=True
        location.append(pos)

if found==True:
    #prints the locations in the list that the target was found
    print (target,"\nappears in the following locations: ",months[location])
else:
    print (target,"\nwas not found in the list.")

months[location] <------ I would like to use list called location that holds more than one variable to print onto screen values assigned to positions in list called months is it possible?

As normally you can only use single variable to point to position in array?

petezurich
  • 9,280
  • 9
  • 43
  • 57
ChefJ
  • 27
  • 7
  • 1
    I'm having trouble understanding your question. Would you like to be able to print the location of items from an array, using indices from a list? Like if you had a list `a = ['apple', 'banana', 'carrot']` and another list of indices `b = [0, 2]`, you would get `apple, carrot`? – Samantha Oct 24 '18 at 20:09

2 Answers2

1

As you noticed, you can't pass a list and use it as the index.

You'd have to loop over each index, or build one complete string and print that.

For example

print(target,"\nappears in the following locations: ", end="")
for index in location:
    print(months[index], end=" ")
print("")

the end="" means that print will add an empty string at the end, instead of the usual new line.

Also, you could improve your code in two other ways.

The boolean found could correspond to the list location having any values in it, so

if location: # an empty list evaluates to False
  print("Found")
else:
  print("Not found")

And your input could look like this instead

target = None
done = False
while not done:
  try:
    target = int( input("Please enter the amount you are looking for:") )
    done = True
  except ValueError:
    print("Wrong value, input a number.")

so the user can fail multiple times and the program won't proceed.

IAmBullsaw
  • 93
  • 6
0

You can if you change your output a bit:

target = 42
location = []
# 42 occures at positions 3,6,8
#           0 1 2 3  4 5 6  7 8  9
wydatki = [ 1,2,3,42,4,5,42,6,42,8]

#         pos: 01234567890
months = list("abcdefghijk")

# get the positions unsing enumerate
for pos,value in enumerate(wydatki):
    if value == target:
        location.append(pos)

if location: # empty lists are False, lists with elements are True
    #prints the locations in the list that the target was found
    print (target,"\nappears in the following locations: ", 
           ','.join( (months[a] for a in location) ) )
else:
    print (target,"\nwas not found in the list.")

Output:

42 
appears in the following locations:  d,g,i

Essentially you need to plug and join all month-entries into a string - f.e. using a generator expression inside a ","join( ... ) statement.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Quick question Patrick why changing for pos in range(0, len(wydatki)): to : for pos,value in enumerate(wydatki): is it just making code more efficient? or does it serve any other purpouse? – ChefJ Oct 24 '18 at 20:46
  • @ChefJ: because it feels clumsy to do it. If I need positions while iterating an iterable I use `enumerate(iterable)` wich gives me position AND value without need to index back into my iterable usign the index. Same for `found=True` - its simply not needed - you declare a variable that captures if something was put into `location` - you can instead simply use `location` itself - if there is anything in it, it is `True`, else it is `False`: see [truth-value-testing](https://docs.python.org/3/library/stdtypes.html#truth-value-testing) – Patrick Artner Oct 24 '18 at 20:50
  • appreciate it just starting off with python 5 weeks in so all is new. – ChefJ Oct 24 '18 at 20:55
  • @ChefJ if you want to read about input validation as well, read up on [Asking user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) - if you are new you might want to go to the [python] tab and switch to **Votes** (https://stackoverflow.com/questions/tagged/python?sort=votes&pageSize=50) - the first 100 or so are a really great read of does and donts - same for the python-3.x tag : https://stackoverflow.com/questions/tagged/python-3.x?sort=votes&pageSize=50 - they are highest ranked Q&A topics – Patrick Artner Oct 24 '18 at 20:55