Search This Blog

Write the function definition for the function 'Integer_fun' which takes two parameters - Using int - Hacker Rank Questions and answers Hands on Python Course ID: 55193

 Python - Integer

Write the function definition for the function 'Integer_fun' which takes two parameters 'a' & 'b' where

- a is a FLOAT variable &

- b is a STRING variable which takes Integer input but as a String, Say b = "100".


Using type conversion,

- Convert 'b' into an INTEGER and store it in 'c'

- Convert 'b' into an INTEGER and store it in 'd'


The expected output is as below:

  • Print the Type of
  • Print the Type of b
  • Print c
  • Print d
  • Print the Type of c
  • Print the Type of d
Input Format for the Custom Testing
#In the first line, value for 'a'
#In the second line, value for 'b'

Sample Test Case 1:

Sample Input
STDIN      Function parameter
-----      ------------------
1.23   →   a
562    →   b
Sample Output
<class 'float'>
<class 'str'>
1
562
<class 'int'>
<class 'int'>

Answer:
#!/bin/python3

import math
import os
import random
import re
import sys


#
# Complete the 'Integer_fun' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. FLOAT a
#  2. STRING b
#

def Integer_fun(a, b):
    # Write your code here
    c = int(a)
    d = int(b)
    print(type(a))
    print(type(b))
    print(c)
    print(d)
    print(type(c))
    print(type(d))

if __name__ == '__main__':
    a = float(input().strip())

    b = input()

    Integer_fun(a, b)


No comments:

Post a Comment

If you have any doubts, Please let us know.