简体   繁体   中英

Cannot print output on the same line

I want my output to print on one line and there should be gap after the input taken by the use.

 i = 1
  x = int(input("enter a number to loop" ))

  while(i<=x):
       print('\n',i, end= " ")
       i += 1

for my code, my output is not on the same line. how to have an output printed on one line.

You added a \n in your print function..

i = 1
x = int(input("enter a number to loop\n"))

while(i <= x):
    print(i, end=" ")
    i += 1

This should solve your issue

i = 1
x = int(input("enter a number to loop " ))
print("\n")
while(i<=x):
   print(i, end= " ")
   i += 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM