I'm trying to write an add() function using input(), however it seem that the input doesn't really work for me.
The module is as following:
def add (n1, n2):
return (n1+n2)
def main ():
print (add(input("Enter number one, please:"),input ("Enter number two, please:")))
if __name__ == "__main__":
main()
When ran, I just get the "Enter number one, please:" prompt, and when inputting an actual number and pressing ENTER, nothing really happens. I tried getting the "Sublime Input" package, but to no avail.
I'd like to get this to run without resorting to Ubuntu (I'm using Windows 8.1).
First off input in Python 3 no longer interprets the data type you are giving to it. This in essence means that everything you give it will be read as a string. This mean the add function will not work as you are expecting it to. To fix this change the code to the following:
def add (n1, n2):
return (n1+n2)
def main ():
print (add(float(input("Enter number one, please:")),float(input ("Enter number two, please:"))))
if __name__ == "__main__":
main()
The addition of the builtin float function ensures that the input in converted into a float and you can therefore to math operations on it.
Secondly, Sublime Text 3 is still in its beta development stage. This means that some features might not work as you would expect. Try using Sublime Text 2. Also I ran the above code in the command line using python add.py and it works perfectly. NOTE: I saved the file as add.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With