Ejercicio.-39 Menú con ARGV en Python [90]
En este ejercicio vamos a crear un menú con ARGV en Python que pida argumentos y dependiendo de lo que se ingrese vamos a:
- -s o –suma = Va sumar n cantidad de números.
- -r o –resta = Va restar n cantidad de números.
- -m o –mul = Va multiplicar n cantidad de números.
- -d o –div = Va dividir n cantidad de números.
- python programa.py -s 1 2 3
- Resultado =6
Codigo: https://github.com/programadornovato/curso-python/commit/b7fb7636b9c55ac4a5b6d2c3371fc0ddbc6f8246
import sys
if __name__=="__main__":
tam=len(sys.argv)
for argumento in sys.argv:
if argumento=="-h" or argumento=="--help" or argumento=="" :
print("Bienvenido humano al programa x\
\n-h or --help: Ayuda\
\n-s or --suma: Sumar n cantidad de numeros\
\n-r or --resta: Restr n cantidad de numeros\
\n-m or --mul: Multiplicar n cantidad de numeros\
\n-d or --div: Dividir n cantidad de numeros")
if argumento=="-s" or argumento=="--suma":
i=2
if tam>3:
res=0
while i<tam:
res=res+int(sys.argv[i])
i=i+1
print("Resultado=",res)
else:
print("Faltan argumentos")
if argumento=="-r" or argumento=="--resta":
i=2
if tam>3:
res=0
while i<tam:
if i==2:
res=int(sys.argv[i])
else:
res=res-int(sys.argv[i])
i=i+1
print("Resultado=",res)
else:
print("Faltan argumentos")
if argumento=="-m" or argumento=="--mul":
i=2
if tam>3:
res=1
while i<tam:
res=res*int(sys.argv[i])
i=i+1
print("Resultado=",res)
else:
print("Faltan argumentos")
if argumento=="-d" or argumento=="--div":
i=2
if tam>3:
res=0
while i<tam:
if i==2:
res=int(sys.argv[i])
else:
res=res/int(sys.argv[i])
i=i+1
print("Resultado=",res)
else:
print("Faltan argumentos")
🎦[CURSO] PYTHON PARA PRINCIPIANTES🐍: https://www.youtube.com/playlist?list=PLCTD_CpMeEKRppvi86Dy8CPYZ8-XzN2qu
🔗 Gracias por apoyar este canal: https://www.patreon.com/programadornovato
🔗 Facebook: https://facebook.com/ProgramadorNovatoOficial
🔗 Twitter: https://twitter.com/programadornova
🔗 Linkedin: https://www.linkedin.com/in/programadornovato/
🔗 Instagram: https://www.instagram.com/programadornovato/
🔗 Pagina oficial: https://www.programadornovato.com
🎦 Canal de youtube: https://www.youtube.com/c/programadornovato
<<Anterior tutorial Siguiente tutorial >>