Computer science/Python help!!!! someone please tell me how I would type this?

Attachment image

2 Answers

  • 10 months ago
    Favourite answer

    There's pretty much just one way to input a string:

    name = input("Hi. What's your name? ")

    Change input to raw_input for Python 2, and then change classes. Everyone should be teaching Python 3 these days.

    There's a boatload of ways to do the output, though:

    print("Hello", name, ", this is version 1.")

    print("Hello " + name + ", this is version 2.")

    print("Hello {}, this is version 3.".format(name))

    print(F"Hello {name}, this only works in Python 3.6 or later.")

    print("Hello %s, this is really old-fashioned, but still works." % (name))

    Try them all. If the next-to-last line gives you a syntax error, skip it. You need Python 3 or later to use "formatted string literals". I used quote(") characters instead of the usual apostrophes(') because of the ' in "What's", and the just kept on using them. Feel free to:

    print('Hello', name)

    ...if you prefer.

    When you run those (please do!), notice the ugly space between the name and the comma on version 1. The print() function converts each argument to a string and then prints them, putting a single space between them. You can change that using the sep= keyword argument:

    print('Hello ', name, ', this is version 1A.', sep='')

    There, I've used sep='' to make the separator an empty string instead of a single space, and put spaces I wanted in the surrounding test. (I also switched back to 'single-quote' strings.)

    The .format() method has a ton of options for formatting numbers, dates and more. This example just shows the simplest use of it.

    And the % operator in the 'really old' example is syntax that goes back to Python 1. Don't use it in new code, but you might see it in legacy code from others so it's worth knowing what that means.

    • ...Show all comments
    • Snezzy
      Lv 7
      10 months agoReport

      This answer reminds me of the elephant answer. "Mommy, tell me about elephants." "Why don't you go ask your father?" "Mommy, I don't want to know THAT MUCH about elephants!"

    • Commenter avatarLog in to reply to the answers
  • Anonymous
    10 months ago

    Sorry Billy bob, oh where's home plate?

    • Commenter avatarLog in to reply to the answers
Still have questions? Get answers by asking now.