Turn For Loop into While or If

This is the place for queries that don't fit in any of the other categories.

Turn For Loop into While or If

Postby adrianvilsmaier » Thu Dec 05, 2013 6:59 pm

Hi Guys, i have some serious problems turning this code, which has a for loop, into a While or If condition.
Here's the Code:

Code: Select all
def criar_matriz(linhas, colunas, valor):
    requer linhas inteiro > 0
    requer colunas inteiro > 0
    requer valor qualquer
    devolve lista de tamanho linhas com cada elemento uma lista de
            tamanho colunas preenchida com valor
    >>> criar_matriz (1,1,0)
    >>> criar_matriz (3,3,1)
    [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
    >>> criar_matriz (1, 5, 0)
    [[0, 0, 0, 0, 0]]
    >>> criar_matriz (5, 1, 2)
    [[2], [2], [2], [2], [2]]
    for x in range(linhas):
        for y in range(colunas-1):
    return l

This is from a friend so that's why i need it with different loops.
Hope anyone can help me. Thanks. AV
Last edited by Yoriz on Thu Dec 05, 2013 7:09 pm, edited 1 time in total.
Reason: First post lock, Added code tags
Posts: 1
Joined: Wed Dec 04, 2013 7:56 pm

Re: Turn For Loop into While or If

Postby Yoriz » Thu Dec 05, 2013 7:11 pm

Hi, welcome to the forum, please read the 'new users read this' link in my signature.
I've added code tags to your code for you this time, please use them when posting any future code.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Turn For Loop into While or If

Postby tnknepp » Thu Dec 05, 2013 7:25 pm

while/if statements are dependent on some condition, which you have not specified. We cannot help until you explain what you really want to happen.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 12 guests