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)
    [[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]]
    """
    l=[]
    for x in range(linhas):
        l.append([valor])
        for y in range(colunas-1):
            l[x]+=[valor]
    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
adrianvilsmaier
 
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.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 725
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
tnknepp
 
Posts: 119
Joined: Mon Mar 11, 2013 7:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: Larz60+, snippsat and 3 guests