How to enter a date by keyboard and use it in Python?

2

I have a query, I did the exercise in C but I do not know how to pass it to Python, especially with the subject of when I enter the full date .. Here is the code in C

#include <stdio.h>

main(){

int DiaN, MesN, AnioN;
int DiaH, MesH, AnioH;
int Dia, Mes, Anio;

printf("Ingrese Dia, Mes y Año de nacimiento (Separados por un espacio): ");
scanf("%d %d %d",&DiaN,&MesN,&AnioN);

printf("Ingrese Dia, Mes y Año del dia de hoy (Separados por un espacio): ");
scanf("%d %d %d",&DiaH,&MesH,&AnioH);

Anio = AnioH - AnioN;

if(MesN > MesH){
    Anio = Anio - 1;
}

Mes = MesH - MesN;

if(Mes < 0){
    Mes = 12 + Mes;
}

if(DiaN > DiaH){
    Mes = Mes - 1;
}

printf("\nAños: %d Meses: %d",Anio,Mes);

} 

Any help is much appreciated: (

    
asked by Wolf 22.09.2018 в 02:51
source

2 answers

3

There are many ways to do it, the simplest is to create the same number of variables as in C and then, asking for the value of each one, something like this would be:

# No es necesario el uso del ';' pero bue
print("Ingrese la fecha de nacimiento");
dia1=int(raw_input("Dia: "));
mes1=int(raw_input("Mes: "));
anio1=int(raw_input("Anio: "));
print("\n Ingrese el dia de hoy: ");
# ... y el codigo seguiria mas o menos de ese modo

Or you could use a str to read the complete date and then separate it into parts, for example, if I entered two numbers separated by a space it would be something like this:

cadena=str(raw_input("Dos numeros separados por un espacio: ")); # Aca tomo los dos numeros
numero1=0;
numero2=0;
cont=0;
while(cadena[cont]!=' '):
    numero1=numero1*10+int(cadena[cont]);
    cont+=1;
cont+=1; 
# Como el cont esta en la posicion de la cadena que tiene un espacio, le sumo 1
while(cont<len(cadena)):
    numero2=numero2*10+int(cadena[cont]);
    cont+=1;
# Aunque esto seria muy largo, puesto que necesitarias 6 bucles mas o menos

With respect to the other part of the code, it is not far from python, it is practically the same, except that the keys would not be and you would have to take into account the indentation and the two points. For my part, I recommend the first option that I put, the other is already complicated to the fart.

I saw something wrong before, but I fixed it.

    
answered by 22.09.2018 / 03:41
source
2

If you want to emulate similar user behavior to scanf you need three things:

  • raw_input() : to read the user's input, return a string str (ASCII in Python 2).

  • str.split() : allows you to divide the string using another string passed as an argument. If nothing happens, divide by using blank spaces, in addition to deleting them from the beginning and end of the chain. Returns a list of strings.

  • int() : to do an entire casting of the elements returned by str.split .

To apply the casting on each item you can use a for in :

entrada = raw_input("Ingrese Dia, Mes y Año de nacimiento"
                    "(Separados por un espacio): ")
dia_n, mes_n, anio_n = (int(item) for item in entrada.split())

or you can use map to use a functional approach:

entrada = raw_input("Ingrese Dia, Mes y Año de nacimiento"
                    "(Separados por un espacio): ")
dia_n, mes_n, anio_n = map(int, entrada.split())

In the case of printf you can use the old format with % similar to C:

print "Años: %d Meses: %d" % (anio, mes)

or use str.format :

print "Años: {} Meses: {}".format(anio, mes)

In Python 2 the default source code uses ASCII, so the appropriate encoding must be specified in the first or second line of the script if it is not. To use the ñ as such in the string literal ( print "Años: ..." ) you can use UTF-8:

#-*- coding: utf-8 -*-

and remember to save the script with this encoding.

The rest of the code does not imply any more problems, it can be translated as is to Python just by changing the keys by the appropriate indentation and eliminating the ; since they are unnecessary.

In Python 3 the idea is the same, only that you have to replace raw_input with input and print is a function. Also for Python> = 3.6 you can (and owe efficiency) using formatted string literals:

entrada = input("Ingrese Dia, Mes y Año de nacimiento"
                    "(Separados por un espacio): ")
dia_n, mes_n, anio_n = (int(item) for item in entrada.split())

...

print(f"Años: {anio} Meses: {mes}")

In Python 3 the interpreter uses UTF-8 by default for the source code, so it is not necessary to specify the encoding if UTF-8 is used in the file.

    
answered by 22.09.2018 в 19:26