0

By one line, what I mean is something like the output of this Ruby code:

def ask()
  puts("What is your name?")
  name = gets().chomp
  if (name == "Tilly")
    puts("Awesome name!")
  else
    print("#{name} is a ")
    index = 0
    while (index < 50)
      print("silly ")
      index += 1
    end
    puts("name!")
  end
end

ask()

Basically, I don't want a new line for each time the word silly is printed. Here's the Python code:

name = input("Welcome! Enter your name. ")
print(name, "is a")
i = 0
while (i < 50):
    print("silly")
    i = i + 1

print("name!")

How do I fix this? In Ruby, there is print() for situations like this, and puts() for everything else. Python doesn't seem to have anything like this. Please be gentle, this is my first ever Python program.

Stefan
  • 109,145
  • 14
  • 143
  • 218

1 Answers1

0

In python 3.0 onwards, print() statement adds a newline by default, you can pass an argument end=" " to suppress the newline and add a space instead.

So your code will become

name = input("Welcome! Enter your name. ")
print(name, "is a", end=" ")
i = 0
while (i < 50):
    print("silly", end=" ")
    i = i + 1

print("name!", end=" ")
justgoodin
  • 301
  • 1
  • 11