Pyhton is new to me and i'm having a little problem with the for loops,
Im used to for loop in java where you can set integers as you like in the loops but can't get it right in python.
the task i was given is to make a function that return True of False.
the function get 3 integers: short rope amount, long rope amount and wanted.
it's known the short rope length is 1 meter and the long rope length is 5 meters.
if the wanted length is in range of the possible lengths of the ropes the function will return True, else false,
for example, 1 short rope and 2 long ropes can get you the following length: [1, 5, 6, 10, 11] and if the wanted length that the function got is in this list of lengths it should return True.
here is my code:
def wantedLength(short_amount, long_amount, wanted_length):
short_rope_length = 1
long_rope_length = 5
for i in range(short_amount 1):
for j in range(long_amount 1):
my_length = [short_rope_length * i long_rope_length * j, ", "]
if wanted_length in my_length:
return True
else:
return False
but when I run the code I get the following error:
TypeError: argument of type 'int' is not iterable
what am I doing wrong in the for loop statement?
thanks in advance!
I tried to change the for loops with other commands like [short_amount] and etc
the traceback as requsted:
Traceback (most recent call last):
File "C:\Users\barva\PycharmProjects\Giraffe\Ariel-Exc\Exc_2.py", line 89, in <module>
print(wantedLength(a,b,c))
File "C:\Users\barva\PycharmProjects\Giraffe\Ariel-Exc\Exc_2.py", line 73, in wantedLength
if wanted_length in my_length:
TypeError: argument of type 'int' is not iterable
CodePudding user response:
The code you posted could not give you that error. On the other hand, the problem you have with the code is that in each iteration you create a new list with the current value (integer) and a string ",". You need to append values to the list:
def wantedLength(short_amount, long_amount, wanted_length):
short_rope_length = 1
long_rope_length = 5
my_length = list()
for i in range(short_amount 1):
for j in range(long_amount 1):
my_length.append(short_rope_length * i long_rope_length * j)
if wanted_length in my_length:
return True
else:
return False
CodePudding user response:
So, the thing is that you were assigning only the last item to length. If what you want is a array of all the possibilities, you can do something like:
def wantedLength(short_amount, long_amount, wanted_length):
short_rope_length = 1
long_rope_length = 5
my_length=[]
for i in range(short_amount 1):
for j in range(long_amount 1):
my_length.append(short_rope_length * i long_rope_length * j)
my_length.remove(0)
my_length.sort()
if wanted_length in my_length:
return True
else:
return False