I would like to be able to print the values that you send me:
c=int(input("Ingrese un numero:"))
a=0
while(a<=c):
print(a)
a+=2
instead of one on each line:
> > > Enter a number: 9
0
2
4
6
8
.
that appears like this:
0,2,4,6,8
I would like to be able to print the values that you send me:
c=int(input("Ingrese un numero:"))
a=0
while(a<=c):
print(a)
a+=2
instead of one on each line:
> > > Enter a number: 9
0
2
4
6
8
.
that appears like this:
0,2,4,6,8
If you use Python 3.x, it is enough to specify the argument end
of the function print
suitably:
c=int(input("Ingrese un numero:"))
a=0
while(a<=c):
print(a, end=',')
a+=2
If you use Python 2.x using your code, you can do:
c=int(input("Ingrese un numero:"))
a=0
while(a<=c):
print str(a)+',',
a+=2
Output (with% input_co co):
0,2,4,6,8,
If you wish to eliminate the last comma you can do any of the following methods:
It's simpler, more efficient, and more pythonic to use a 9
with for
or directly range
to do something like that . This is valid if you use Python 3.x directly or 2.x but importing range
of print
:
c=int(input("Ingrese un numero:"))
print(*range(0,c+1,2), sep=',')
Output (with% input_co co):
0,2,4,6,8
Another option is to use intermediate variables (chains or lists), generators and the __future__
method of the chains. Everything below is valid for Python 2 and 3:
Using 9
, auxiliary list and join
(very inefficient):
c=int(input("Ingrese un numero:"))
aux=[]
a=0
while(a<=c):
aux.append(str(a))
a+=2
print(','.join(aux))
Use an auxiliary variable of type while
instead of a list (similar to the previous one):
c=int(input("Ingrese un numero:"))
aux=''
a=0
while(a<=c):
aux+=str(a)+','
a+=2
print(aux[:-1])
Using join
, auxiliary list and str
:
c=int(input("Ingrese un numero:"))
aux=[]
for a in range(0,c+1,2):
aux.append(str(a))
print(','.join(aux))
Using generator and for
(most efficient):
c=int(input("Ingrese un numero:"))
print(','.join(str(n)for n in range(0,c+1,2)))
Speaking in Python 2.7: A possible solution would be obtained through a string to which we are adding, in each pass through the while loop, the number obtained and a comma. When leaving the cycle we will have a comma that will be left over at the end however in python it is relatively easy to eliminate elements that are at the end of the string.
I leave you a possible solution:
c=int(input("Ingrese un numero:"))
a=0
numeros = "" #String que contendra los numeros
while(a<=c):
numeros = numeros + str(a) + "," #agregamos al string el numeros y una coma
a+=2
numeros = numeros[:-1] #Eliminamos la coma que nos sobra
print numeros #Mostramos por pantalla la salida
Another possible solution may be the following: Go saving each number in an array but at the time of adding them we will transform them to string. When the while cycle has ended it will only be enough to join our numbers, this is done thanks to the join method.
Join: The join () method returns a string in which the string elements of the sequence have been joined by str separator.
In Code:
c=int(input("Ingrese un numero:"))
numeros = [] #arreglo que contendra los numeros
a=0
while(a<=c):
numeros.append(str(a)) #Agregamos los numeros a nuestro arreglo pero estos los transformamos a string
a+=2
salida = ",".join(numeros) #Juntamos todos los numeros y los separamos por una coma
print salida
In short, it can be done in different ways, as far as your imagination goes. Greetings, I hope I have helped you.