Q. While taking input from input(), you need to convert the input into float and int types, if you are reading numbers.


(a) What built-in function do you use to convert a string into a floating-point number? Demonstrate this function by converting string '85' into a floating-point number and assigning it to a variable x.
(b) What built-in function do you use to convert a string into an integer?
(c) What happens when you try to convert a string into an integer but the string is not a number?


Answer :-

(a)
Program :-
val = input ("Enter Number :- ")
x = float (val)
print (x)
print (type (x))
Output :-

Enter Number :- 85
85.0
<class 'float'>
>>>
Enter Number :- 53.26
53.26
<class 'float'>
>>>

(b) We can use int() function to convert a string into an integer.
Program :-
val = input ("Enter Number :- ")
x = int (val)
print (x)
print (type (x))
Output :-

Enter Number :- 65
65
<class 'int'>
>>>

(C) It will give an Error : 

Enter a Number:- Path Walla

Traceback (most recent call last):
  File "\Desktop\temp.py", line 1, in <module>
    sen = int(input("Enter a Number :-"))
ValueError: invalid literal for int() with base 10: 'Path Walla'

3 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post