how to use = and >= in for and while loops

202
March 05, 2018, at 04:20 AM

I am new to python and I am try to understand why the following breaks and how to do this better:

x = [5,5,5,5,6,7,87,8,9,10]
for i in range(len(x)):
    while i == 5:
        print('5')
    else:
        print('no')

-This code runs forever producing 5's

for i in range(len(x)):
    while i = 5:
        print('5')
    else:
        print('no')

-This breaks

for i in range(len(x)):
    while i => 5:
        print('5')
    else:
        print('no')

-This breaks

How do I program a simple loop to print '5' if the value of i is 5 and print 'no' in the other instances? how do I use signs like = and >= in loops? Python seems not to recognize them

Answer 1
x = [5,5,5,5,6,7,87,8,9,10]
for c in x:
    if c == 5:
        print('5')
    else:
        print('no')

while i==5 is always True because the value of i never changes inside the body of the for loop.

READ ALSO
Python folder recursion

Python folder recursion

I'm getting lost

237
PageRank: construct the tranfer matrix without using for loops?

PageRank: construct the tranfer matrix without using for loops?

I got a helper numpy array with 3 columns: from, to, probabilityI want to construct the transfer matrix in PageRank, but in a vectorization way

162
Running Neural Networks Takes Longer Time

Running Neural Networks Takes Longer Time

I'm running a series of neural networks (Keras library using a Tensorflow backend), and I have the following results for the time it took to train each neural network in Jupyter Notebook:

236
update object with function call

update object with function call

I'm working with a 3rd party package that defines a result object from an expensive method call on a source object: result_object = source_objectmethod(input_value)

202