0

I'm trying to create a script that allows users to input numbers and then the numbers are used for to create an exponent. Here's what i'm writing. I'm in python 3.6 and using anaconda/spyder.

print ("Enter number x: ")
x = input()
print ("Enter number y: ")
y = input()
import numpy
numpy.exp2([x,y])

so what I want is for the user to enter a value for x. for example 2. then enter a value for y. for example 3. then i'd like (with the help of numpy) create the exponent 2**3 which equals 8. instead i get this.

Enter number x: 
2
Enter number y: 
3

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)
  File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
  File "/Users/Ameen/Desktop/assignment1.py", line 16, in <module>
    numpy.exp2([x,y])
TypeError: ufunc 'exp2' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

I tried print ('xy') but that printed xy. so i came across this site https://docs.scipy.org/doc/numpy/reference/generated/numpy.exp2.html#numpy.exp2 they suggested np.exp2. when I tried np.exp2([x**y]) it says np is not defined and an error message saying the numpy library is not imported. so now i'm trying numpy.exp2 and got the error above.

amnmustafa15
  • 121
  • 1
  • 14
  • 1
    Why are you on 3.7? That's not out yet. I don't think it's even had its first alpha release. – user2357112 Sep 14 '17 at 21:55
  • @user2357112 https://docs.python.org/3.7/whatsnew/3.7.html (Released today) – Artyer Sep 14 '17 at 21:56
  • @Artyer: That's not an actual release; it's pre-alpha documentation. I think the first alpha is [planned](https://www.python.org/dev/peps/pep-0537/) for the 18th. – user2357112 Sep 14 '17 at 22:05
  • Your stack trace says `/anaconda/lib/python3.6`, which doesn't sound like you're on 3.7. – user2357112 Sep 14 '17 at 22:06
  • (Also, the [Internet Archive](https://web.archive.org/web/20170225004620/https://docs.python.org/3.7/whatsnew/3.7.html) shows that the page has been around a lot longer than just today, and that the 3.7.0a0 "release date" keeps shifting around just based on when they last regenerated the docs.) – user2357112 Sep 14 '17 at 22:10
  • @user2357112 it's a typo. fixed it. – amnmustafa15 Sep 14 '17 at 22:25

1 Answers1

1

Convert strings to integers:

import numpy

print('Enter number x:')
x = int(input())

print('Enter number y:')
y = int(input())

print(numpy.exp2([x, y])) #=> [ 4.  8.]
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79