Questions about functions in Python

3

I am a beginner in the programming area and am having doubts in the questions below. I will post the questions and my attempts (all are presenting error in the broker). If anyone can help me from now on thanks.

  

1 - Write the Rectangle function by printing the unfilled rectangles so that the characters that are not on the edge of the rectangle are spaces.

largura = int(input("Digite a largura: "))
altura = int(input("Digite a altura: "))
caractere = "#"


def retângulo(largura, altura, caractere):

    linha = caractere * largura

    for i in range(altura):
        print(linha)

retângulo(caractere, altura, largura)
  

2 - Write the function n_primos that receives an integer greater than or equal to 2 as a parameter and returns the number of prime numbers that exist between 2 and n (including 2 and, if applicable, n ).

def èPrimo(x):
    fator = 2

    while x % fator !=0 and fator < x/2:
        fator = fator + 1
    if x % fator ==0:
        return False
    else:
        return True


def n_primos(n):

    lista_primos = []
    for i in range(2,n):
        if èPrimo(i):
            lista_primos.append(i);


    return len(lista_primos)
  

3 - Write a program that receives a sequence of integers ending with 0 and prints all values in reverse order. (Show sequence vertically)

seq = []
num = 1


while True:
    num = int(input("Digite o número: "))
    if num == 0:
        break
    seq.append(num)
seq.reverse()
print(seq)
    
asked by anonymous 02.02.2017 / 18:23

2 answers

1

Your question is a bit old, but come on.

Program 1

The first program has the following error:

def retângulo(largura, altura, caractere):
# ...
retângulo(caractere, altura, largura)

Note the parameters passed. They are in the wrong order! It should be retângulo(largura, altura, caractere) .

The second problem is that you will paint a full rectangle, not just the edges. To fix this, the process is a bit more complicated:

largura = int(input("Digite a largura: "))
print("")
altura = int(input("Digite a altura: "))
print("")
caractere = "#"

def retângulo(largura, altura, caractere):

    linha_cheia = caractere * largura
    if largura > 2:
        linha_vazia = caractere + (" " * (largura - 2)) + caractere
    else:
        linha_vazia = linha_cheia

    if altura >= 1:
        print(linha_cheia)
    for i in range(altura - 2):
        print(linha_vazia)
    if altura >= 2:
        print(linha_cheia)

retângulo(largura, altura, caractere)

The idea here is to compute linha_cheia which is the line with largura characters # and linha_vazia which is the line that has largura - 2 spaces in the middle followed by # and preceded by a # . Then I put a full line,% w / o% empty lines and another full line.

I use% s of% s to deal with special cases where the width might be too small so that a line may have spaces in the middle or the height might be too small in case you can not have one (or both ) full lines at the beginning and end.

See here working on ideone.

Program 2

First, altura - 2 should have been written as if . Or better yet, èPrimo ( see Python naming conventions ).

The second point is that in the éPrimo function, you do not need to keep a list of cousins and then tell the size of the list. Just go counting the elements that are prime, without having to store them.

The third point is that in é_primo , you do not have to follow n_primos to é_primo , just go to the square root of while . A simple and easy way to know if you have reached the square root without having to invoke a function that calculates the square root is to use x / 2 condition.

The fourth point is that you can put x that has fator * fator <= x within if , so you do not have to repeat the return False condition in both while and % .

The fifth point is that in your while function, you have to use if in your n_primo instead of n + 1 . This is because the second range parameter would be the first value that is out of range, not the last value inside it.

So, here's how your code looks:

def é_primo(x):
    fator = 2
    while fator * fator <= x:
        if x % fator == 0:
            return False
        fator = fator + 1
    return True

def n_primos(n):
    contagem_primos = 0
    for i in range(2, n + 1):
        if é_primo(i):
            contagem_primos = contagem_primos + 1
    return contagem_primos

# Teste
for t in range(0, 100):
    print("p(" + str(t) + ") = " + str(n_primos(t)))

See here working on ideone. Obviously, the given test is not part of the program, it's just to make sure it works. / p>

Program 3

In this program here, the only thing missing is to print vertically. This is easy, just iterate the sequence with a range and give n in each element. Here is the code:

seq = []
num = 1

while True:
    num = int(input("Digite o número: "))
    if num == 0:
        break
    seq.append(num)
seq.reverse()
print("")
for i in seq:
    print(i)

See here working on ideone.

Final remarks

Some brokers do not like to see strings like for , print and "Digite a largura: " on output. If this is your case, just read the entry without speaking to the user using only "Digite a altura: " and remove the lines that have only "Digite o número: " from programs 1 and 3.

    
11.02.2017 / 04:20
0

Program 1: You fill in the filled-in rectangle, not the inside of the blank rectangle

Program 2: Your check function does not test itself for number "n" - will go wrong when n is prime.

Program 3: The statement asks to print "vertically" - I understand this as a number on each line - alone (without commas or other symbols) - you are printing the Python list - the list representation is in one single line and includes brackets and commas.

    
03.02.2017 / 12:51