Input and Output

  1. Write code to print Hello, World
     
    1
    2

    (io1)

    Use the print() function, and remember that strings are surrounded by double-quotes or single-quotes.
    3
     
    1
    print("Hello, World")
    2
    # or print('Hello, World')
    3

    (io1_a)

  1. Write code to print

    Hello

    World

    using 2 print statements.

    2
     
    1
    2

    (io2)

    Call print() twice, on two consecutive lines.
    3
     
    1
    print("Hello")
    2
    print("World")
    3

    (io2_a)

  1. Write code to print

    Hello

    leaving the cursor on the same line.

    2
     
    1
    2

    (io3)

    You have to use the optional parameter, end, in the print statement.
    2
     
    1
    print("Hello", end='')
    2

    (io3_a)

  1. Assume you have a variable ranking set to some integer value. Write a line of code to print the value of ranking.
    3
     
    1
    ranking = 99
    2
    # Replace this comment with your code
    3

    (io4)

    print() evaluates each variable or expression before printing it.
    2
     
    1
    print(ranking)
    2

    (io4_a)

3
ranking = 32

(io5_pre)

  1. Assume you have two variables ranking and average set to some values. Write a line of code to print the values with a single space between.
    2
     
    1
    # Replace this comment with your code.
    2

    (io5)

    The comma in print('x', 'y') will automatically insert a space between the two values
    2
     
    1
    print(ranking, average)
    2

    (io5_a)

  1. Assume you have two variables start and interval. Write a line of code to print the sum of the two values.
    4
     
    1
    start = 103233.1
    2
    interval = 201787.33
    3
    # Replace this comment with your code.
    4

    (io6)

    You can put expressions, like x + y into a print statement, too.
    2
     
    1
    print(start + interval)
    2

    (io6_a)

  1. Assume you have a variable ranking set to some integer value. Write a line of code to print Ranking: followed by the value that ranking refers to. Note that there should be one space between the : and the value. E.g., if ranking was 7, the output would be Ranking: 7.
    3
     
    1
    ranking = 7
    2
    # Replace this comment with your code.
    3

    (io7)

    Remember, the comma in a print adds a space, so be careful!
    2
     
    1
    print('Ranking:', ranking)
    2

    (io7_a)

3
name = 'Joe McDonald'

(io8_pre)

  1. Assume you have a variable name set to a student’s name, and a variable score set to the score the student got on the last quiz. Write a line of code to print out the student’s name and the student’s score, in the following format. Name: Joe McDonald Score: 7 where the student’s name is Joe McDonald and the student’s score is 7.
    2
     
    1
    # Replace this comment with your code.
    2

    (io8)

    You can pass as many parameters as you want to print, separated by commas.
    2
     
    1
    print('Name:', name, 'Score:', score)
    2

    (io8_a)

  1. Write code to read in a string value from the user, storing the result in a variable name.
    2
     
    1
    # Replace this comment with your code.
    2

    (io9)

    Use input(). You do NOT have to convert the result to a string, because input() returns a string.
    3
     
    1
    name = input()
    2
    # or, better: name = input("Enter a name: ")
    3

    (io9_a)

  1. Write code to prompt the user for a score, reading in the score into a variable score. The type of score should be a float.
    2
     
    1
    # Replace this comment with your code.
    2

    (io10)

    Use input(). You have to convert the result to a float, because input() returns a string.
    5
     
    1
    score = float(input('Enter a score: '))
    2
    # or,
    3
    # print("Enter a score: ", end='')
    4
    # score = float(input())
    5

    (io10_a)

Next Section - Expressions