3

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).

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Ben
  • 51
  • 1
  • 4
  • 1
    I'm not familiar with sublime text and the way it takes input in its interactive mode, but you should read http://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers-in-python because that's not going to do what you want it to do. – Morgan Thrapp Jun 07 '16 at 14:27
  • Your code worked when I ran it on Windows 7, it might not be the best way to do that task, but it works. – Brian Jun 07 '16 at 14:28
  • 2
    Related: http://stackoverflow.com/questions/10604409/sublime-text-2-console-input – OneCricketeer Jun 07 '16 at 14:29
  • Even if that worked, it might not do what you expect it to do. For 1 and 2 you will get '12' because `input` returns a string. – DeepSpace Jun 07 '16 at 14:31

1 Answers1

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

Neill Herbst
  • 2,072
  • 1
  • 13
  • 23
  • I did as you said, the module looks like this now: 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() I also tried building a module using String since you said it's the default for input(), but it still didn't work. This was it: def main(): person = input("Give me a word:") print (person) if __name__ == "__main__": main() – Ben Jun 07 '16 at 15:43