✍️ Print & Input functions
- What is the output of the following snippet if the user enters two lines containing 2 and 4 respectively?
x = input()
y = input()
print(x + y)
What is the type of data returned by the input() function, and how does the type affect the behavior of the + operator?
24
- What is the output of the following snippet if the user enters two lines containing 2 and 4 respectively?
x = int(input())
y = int(input())
print(x + y)
How does the int() function change the data, and how does that affect the + operator?
6
- What is the output of the following snippet if the user enters two lines containing 2 and 4 respectively?
x = input()
y = int(input())
print(x * y)Pay attention to the operator being used, and the data types on each side of the operator.
2222
- Fix the mistakes in the programs below
num1 = input("Please enter a number: ")
num3 = input(3)
print(num1 + num2)Example of Expected output:
Please enter a number: 2.5
Please enter another number: 3.5
6.0
There is more than one issue to fix in the code above. Pay attention to the prompt parameter provided to the input function – what should it be on each line? Also, pay attention to the type of data required in the example output.
num1 = float(input("Please enter a number: "))
num2 = float(input("Please enter another number: "))
print(num1+ num2)- Remove unnecessary calls to the
input()function. This program simply displays the sum of 10 and 15:
num1 = input(10)
num2 = input(15)
print(num1 + num2)If you’re struggling, this problem is simpler than you think.
- Fix the mistakes in the program below, which is suppose to calculate the average of three grades (24, 25, 20) without asking input from the user:
grade1 = input(24)
grade2 = input(25)
grade3 = input(20)
average = input((grade1 + grade2 + grade3)/3)If you’re struggling, this problem is simpler than you think.
- Fix the mistakes in the program below:
num = 123
print The number is numExpected output:
The number is 123
This is a syntax problem – how do you tell the print function what statements it should print?
- Fix the mistakes in the program below:
num1 = 123
num2 = 456
num3 = 789.0
print("The numbers are " + num1 + num2 + num3)Expected output:
The numbers are 123 456 789.0
How do you put the values of variables directly into a print statement?
- Fix the mistakes in the program below:
num1 = 1
num2 = 2
average = print((num1+num2)/2)
Remember that the print function doesn’t return any values!
- Fix the mistakes in the program below which is suppose to print the salary of a person before bonus and the salary after the bonus:
salary = 70_000
print("The salary before bonus: ") + print(salary)
bonus = 1_500
print("The salary after bonus: ") + print(bonus) + print(salary)
Remember that the print function does not return any values, and so you cannot add the result of calling the print function