Input and Output¶
-
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.31print("Hello, World")
2# or print('Hello, World')
3
(io1_a)
-
Write code to print
Hello
World
using 2 print statements.
21
2
(io2)
Callprint()
twice, on two consecutive lines.31print("Hello")
2print("World")
3
(io2_a)
-
Write code to print
Hello
leaving the cursor on the same line.
21
2
(io3)
You have to use the optional parameter,end
, in the print statement.21print("Hello", end='')
2
(io3_a)
-
Assume you have a variable
ranking
set to some integer value. Write a line of code to print the value ofranking
.31ranking = 99
2# Replace this comment with your code
3
(io4)
print()
evaluates each variable or expression before printing it.21print(ranking)
2
(io4_a)
-
Assume you have two variables
ranking
andaverage
set to some values. Write a line of code to print the values with a single space between.21# Replace this comment with your code.
2
(io5)
The comma inprint('x', 'y')
will automatically insert a space between the two values21print(ranking, average)
2
(io5_a)
-
Assume you have two variables
start
andinterval
. Write a line of code to print the sum of the two values.41start = 103233.1
2interval = 201787.33
3# Replace this comment with your code.
4
(io6)
You can put expressions, likex + y
into aprint
statement, too.21print(start + interval)
2
(io6_a)
-
Assume you have a variable
ranking
set to some integer value. Write a line of code to printRanking:
followed by the value thatranking
refers to. Note that there should be one space between the:
and the value. E.g., ifranking
was 7, the output would beRanking: 7
.31ranking = 7
2# Replace this comment with your code.
3
(io7)
Remember, the comma in aprint
adds a space, so be careful!21print('Ranking:', ranking)
2
(io7_a)
-
Assume you have a variable
name
set to a student’s name, and a variablescore
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.21# Replace this comment with your code.
2
(io8)
You can pass as many parameters as you want toprint
, separated by commas.21print('Name:', name, 'Score:', score)
2
(io8_a)
-
Write code to read in a string value from the user, storing the result in a variable
name
.21# Replace this comment with your code.
2
(io9)
Useinput()
. You do NOT have to convert the result to a string, becauseinput()
returns a string.31name = input()
2# or, better: name = input("Enter a name: ")
3
(io9_a)
-
Write code to prompt the user for a score, reading in the score into a variable
score
. The type of score should be afloat
.21# Replace this comment with your code.
2
(io10)
Useinput()
. You have to convert the result to a float, becauseinput()
returns a string.51score = float(input('Enter a score: '))
2# or,
3# print("Enter a score: ", end='')
4# score = float(input())
5
(io10_a)