# How data types work # If you give the computer numbers with decimals, you’ll see an error when it tries to add them. # To avoid this, we can tell the computer to treat them as floats. num_1 = input("Give me a number: ") num_2 = input("Give me a second number: ") print("At the moment, I am treating them as strings.") print(num_1, "+", num_2, "=", num_1 + num_2) print("But if I treat them as floats, I get this") print(num_1, "+", num_2, "=", float(num_1) + float(num_2)) # Here, “float(num_1)” tells the computer to treat the contents of num_1 as # a floating point number. Of course, if we didn’t enter a number when we # were asked, we’ll get an error! # ########################################### #An example of a error in the Python Shell# ########################################### # # >>> # >>> # Give me a number: I have no idea # Give me a second number: what to do # At the moment, I am treating them as strings. # I have no idea + what to do = I have no idea what to do # But if I treat them as floats, I get this. ############################################################### # Traceback (most recent call last): # # File"/root/my-documents/Py/float.py",line11, in <module> # # print("num_1+num_2, "=" float(num_1) + float(num_2)) # #ValueError: could not convert string to float: I have no idea# # >>> # ############################################################### # ################ #End Of Example# ################ # # The computer tries to convert the string of letters into a number, # but of course it can’t, so there’s an error. # # Just for the sake of it, here’s what happens if we enter integers, # but get the computer to treat them as floats (don’t worry, this won’t cause an error). # # # ###################### #Python Shell Example# ###################### # # >>> # >>> # Give me a number:67 # Give me a number:31 # At the moment, I am treating them as strings. # 67 + 31 = 6731 # But if I treat them as floats, I get this. # 67 + 31 = 98.0 # >>> # ################ #End Of Example# ################ # # # The computer is quite happy to convert these numbers into floats, # the answer is “98.0″ which shows that we are working with floating point numbers. # # # Before we leave this topic, I will show you what happens if you use # multiplication on strings. (You won’t be surprised to hear that you can’t divide or subtract strings!). # # # ###################### #Python Shell Example# ###################### # # >>> # >>> # Give me a number:7 # At the moment I am treating it as a string. # 7 times 5 = 77777 # But if I treat it as a float i get this. # 7 times 5 = 35.0 # >>> # ################ #End Of Example# ################ # # As a wise person once said, a computer does what you tell it to # do, not what you want it to do! # Happy Coding, Mr Hogg # #
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question