Posted by : at

Category : 30_Days_of_Code


This challenges asks you to:

  1. Declare 3 variables: one of type int, one of type double, and one of type String.
  2. Read 3 lines of input from stdin (according to the sequence given in the Input Format section below) and initialize your 3 variables.
  3. Use the + operator to perform the following operations:
    * Print the sum of i plus your int variable on a new line.
    * Print the sum of d plus your double variable to a scale of one decimal place on a new line.
    * Concatenate s with the string you read as input and print the result on a new line.

Sample Input:

12
4.0
is the best place to learn and practice coding!

Sample Output:

16
8.0
HackerRank is the best place to learn and practice coding!

Recall from the last challenge that the input() function reads in a user input. However, the input() function only returns strings.

int_input = input() #user enters a 4
dbl_input = input() #user enters a 4.5
str_input = input() #user enters Hello
'4'
'4.5'
'Hello'

Notice that even when a number was entered, it is converted to a string. To avoid this, you can put convert the input into a integer/float type, use int() or float(). Keep in mind that if you use int() and the user enters a string like Hello, it will error out. This is why input() defaults to a string data type. A float, integer, or bool (True/False) can easily be converted to strings by putting quotes around them.

int_input = int(input()) #user enters a 4
dbl_input = float(input()) #user enters a 4.5
str_input = input() #user enters Hello
4
4.5
'Hello'

The + operator is used to sum numbers or concatenate (merge) strings.

print(3 + 4)
print(-1 + 5)
print(4 + 4.5)
a = 5
b = 6
print(a + b)
print('Hello' + 'Sara')
print('Hello ' + 'Sara')
print('Good' + ' ' + 'Morning')
7
4
9.5  #int + float = float
11
'HelloSara'
'Hello Sara'
'Good Morning'

Sample Solution:

# Declare second integer, double, and String variables.
# Read and save an integer, double, and String to your variables.
new_int = int(input())
new_dbl = float(input())
new_str = input()

# Print the sum of both integer variables on a new line.
print(i + new_int)

# Print the sum of the double variables on a new line.
print(d + new_dbl)

# Concatenate and print the String variables on a new line
# The 's' variable above should be printed first.
print(s + new_str)
About

Data Scientist with B.S. Statistics (3.8 GPA, Cum Laude) from UCLA. Programming knowledge includes R, Python, SQL, Tableau, SAS and other data analysis tools.

Star