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.