In [1]:
"""
IPython Notebook v4.0 para python 2.7
Librerías adicionales: Ninguna.
Contenido bajo licencia CC-BY 4.0. Código bajo licencia MIT. (c) Sebastian Flores.
"""
# Configuracion para recargar módulos y librerías
%reload_ext autoreload
%autoreload 2
from IPython.core.display import HTML
HTML(open("style/iwi131.css", "r").read())
Out[1]:
In [ ]:
Desarrolle una función que pregunte nombre, rol y promedio de estudiantes, hasta que se ingresa el texto "STOP" en alguna de las preguntas anteriores. Luego, debe imprimir en pantalla:
In [15]:
M = int(raw_input("Ingrese número: "))
suma = 0
j = 1
while (j<=M):
x = j
suma = suma + x
j = j +1
print suma
In [16]:
M = int(raw_input("Ingrese número: "))
suma = 0
j = 1
while (j<=M):
x = j**3
suma = suma + x
j = j +1
print suma
In [19]:
M = int(raw_input("Ingrese número: "))
suma = 0
j = 1
while (j<=M):
x = 1./j
suma = suma + x
j = j +1
print suma
In [26]:
M = int(raw_input("Ingrese número de términos: "))
suma = 0
j = 1
while (j<=M):
n = 2*j-1
signo = (-1)**(j+1)
x = 4*float(signo)/float(n)
#print signo, n
suma = suma + x
j = j +1
print suma
In [31]:
M = int(raw_input("Ingrese número M: "))
prod = 1
j = 1
while (j<=M):
x = j
prod = prod * x
j = j + 1
print prod
In [32]:
M = int(raw_input("Ingrese número M: "))
prod = 1
j = M
while (j>0):
x = j
prod = prod * x
j = j - 1
print prod
In [6]:
print("Ingrese 2 números N y M, con N<M")
N = int(raw_input("Ingrese numero N: "))
M = int(raw_input("Ingrese numero M: "))
j = N
count = 0
while j<=M:
if j%2==0:
count = count + 1
j = j +1
print "Hay", count, "numeros pares entre ", N, "y", M
In [49]:
def es_primo(n):
j = 2
while (j<n):
if n%j==0:
return False
j = j+1
return True
M = int(raw_input("Ingrese numero M: "))
j = 2
count = 0
while j<=M:
if es_primo(j):
count = count + 1
j = j +1
print "Hay", count, "numeros primos entre 2 y", M
In [7]:
def mayor(n):
print "Ingrese los números"
mayor = -1
c = 1
while c <= n:
a = int(raw_input())
if a > mayor:
mayor = a
c = c + 1
return mayor
M = int(raw_input('Cuantos datos: '))
mm = mayor(M)
print "El mayor es", mm
In [67]:
def mayor(n):
print "Ingrese los números"
mayor = -1
c = 1
while c <= n:
a = int(raw_input())
mayor = max(mayor, a)
c = c + 1
return mayor
M = int(raw_input('Cuantos datos: '))
mm = mayor(M)
print "El mayor es", mm
In [12]:
def menor():
print "Ingrese números"
num_menor = float('inf')
while True:
input_str = raw_input()
if input_str == "STOP":
break
else:
x = float(input_str)
num_menor = min(num_menor, x)
#continue
#print 1/0
return num_menor
m = menor()
print 'El menor es', m
In [13]:
def mayor_en_abs():
print "Ingrese números"
mayor_en_abs = 0
continuar = True
while continuar:
input_str = raw_input()
if input_str == "STOP":
continuar = False
else:
x = float(input_str)
if abs(x) > abs(mayor_en_abs):
mayor_en_abs = x
return mayor_en_abs
m = mayor_en_abs()
print 'El numero de mayor valor absoluto fue', m
In [15]:
i=1
puntaje = 1
while i<=6:
j=1
while j<=6:
if i + j > puntaje:
print i, "+", j, "=", i+j, "> ", puntaje
j+=1
i+=1
In [17]:
print("Ingrese 2 números altura>1 y ancho>1")
N = int(raw_input("Ingrese altura: "))
M = int(raw_input("Ingrese ancho: "))
i = 1
print ""
while i<=N:
j = 1
while j<=M:
# End of ascii art
if i==1 or i==N:
if j==1 or j==M:
print "*",
else:
print "-",
else:
if j==1 or j==M:
print "|",
else:
print " ",
# End of ascii art
j = j +1
print ""
i = i + 1
In [18]:
# Solucion 2
print("Ingrese 2 números N>1 y M>1")
N = int(raw_input("Ingrese altura: "))
M = int(raw_input("Ingrese ancho: "))
i = 1
while i<=N:
if (i==1) or (i==N):
print "*" + "-"*(M-2) + "*"
else:
print "|" + " "*(M-2) + "|"
i = i + 1
Desarrolle una función que pregunte nombre, rol y promedio de estudiantes, hasta que se ingresa el texto "STOP" en alguna de las preguntas anteriores. Luego, debe imprimir en pantalla:
In [ ]:
# BORRADOR
# Inicializacion variables
# CICLO WHILE
while ALGO:
LEER
ACTUALIZAR VARIABLES
# IMPRIMIR RESULTADOS
In [19]:
# Inicializacion
cantidad_notas = 0
suma_notas = 0
peor_alumno = ""
peor_rol = ""
peor_nota = 101
mejor_alumno = ""
mejor_rol = ""
mejor_nota = -1
while True:
alumno = raw_input("Nombre del alumno :")
if alumno == "STOP":
break
rol = raw_input("Rol del alumno :")
if rol == "STOP":
break
nota = raw_input("Nota del alumno :")
if nota == "STOP":
break
else:
nota = float(nota)
# Si llegamos aqui, alumno, rol y nota estan bien definidos
# Actualizar cantidad de notas
cantidad_notas += 1
# Actualizar suma de notas
suma_notas += nota
# Actualizar peor alumno
if nota < peor_nota:
peor_alumno = alumno
peor_rol = rol
peor_nota = nota
# Actualizar peor alumno
if nota > mejor_nota:
mejor_alumno = alumno
mejor_rol = rol
mejor_nota = nota
# Imprimir resultado
print ""
print "RESULTADOS:"
print "Se ingresaron", cantidad_notas, "alumnos"
print "El promedio de notas es ", suma_notas/cantidad_notas
print "El peor alumno es", peor_alumno, peor_rol, "con nota", peor_nota
print "El mejor alumno es", mejor_alumno, mejor_rol, "con nota", mejor_nota