2

I attempted to write a program in python that calculates (likely horribly inefficiently, but I digress) using several different operations. However, there was an error I can't figure out when running it. I think it requires defining the type of a variable. The program:

import math
print('Select a number.')
y = input()
print('Select another number.')
x = input()
print('Select what operation you wish to perform. (e for exponentiation, d for division, m for multiplication, a for addition, s for subtraction, mo for modulo, l for log (the base is the first number you entered), r for root)')
z = input()
if z == 'e' or z == 'E':
 print('The answer is ' + y**x)
elif z == 'd' or z == 'D':
 print('The answer is ' + y/z)
elif z == 'm' or z == 'M':
 print('The answer is ' + y*x)
elif z == 'a' or z == 'A':
 print('The answer is ' + y+x)
elif z == 's' or z == 'S':
 print('The answer is ' + y-x)
elif z == 'mo' or z == 'Mo':
 print('The answer is ' + y%x)
elif z == 'l' or z == 'L':
 print('The answer is ' + math.log(x,y))
elif z == 'r' or z == 'R':
 print('The answer is ' + y**(1/x))

The error that showed up in the shell:

Traceback (most recent call last):
 File "C:/Users/UserNameOmitted/Downloads/Desktop/Python/Calculator.py", line 7, in <module>
 z = input()
 File "<string>", line 1, in <module>
NameError: name 'd' is not defined
asked Mar 25, 2016 at 11:45
2
  • Use raw_input instead of input. Commented Mar 25, 2016 at 11:49
  • 1
    Are you sure? That line number is weird – The error I get online is "Traceback (most recent call last): File "python", line 11, in <module> TypeError: unsupported operand type(s) for /: 'str' and 'str'". Which suggests this is a dup of stackoverflow.com/questions/24412000/… Commented Mar 25, 2016 at 11:50

3 Answers 3

1

You have several problems here:

  1. z should be inputted with raw_input, not input.
  2. In the division case, you're dividing y/z instead of y/x.
answered Mar 25, 2016 at 11:53
Sign up to request clarification or add additional context in comments.

1 Comment

OP is using python2.7. So input gives an int
1

You have to do

z = raw_input()

Also input returns as int in python2.x.So use raw_input to read as str Then everywhere like print('The answer is ' + y**x) into print('The answer is ' + str(y**x))

answered Mar 25, 2016 at 11:50

4 Comments

What about for math.log(x,y)? Would that become str(math.log(x,y))?
Also, this yields 16/5 = 3.
if you want decimal answer, do from __future__ import division at top of your file
Which is a better choice? print('a string ' + str(num_expr)) or print 'a string', num_expr?
1

Your code has errors

  1. use raw_input() to take string input instead of input()
  2. use int(raw_input()) to take integer input, its not an error but it is good practice.
  3. you tried to divide int with string.
  4. You tried to concatenate string and integer.

Your code should be something like this.

import math
print('Select a number.')
y = int(raw_input())
print('Select another number.')
x = int(raw_input())
print('Select what operation you wish to perform. (e for exponentiation, d for division, m for multiplication, a for addition, s for subtraction, mo for modulo, l for log (the base is the first number you entered), r for root)')
z = raw_input()
if z == 'e' or z == 'E':
 print('The answer is %d' %(y**x))
elif z == 'd' or z == 'D':
 print('The answer is %d' %(y/x))
elif z == 'm' or z == 'M':
 print('The answer is %d' %(y*x))
elif z == 'a' or z == 'A':
 print('The answer is %d' %(y+x))
elif z == 's' or z == 'S':
 print('The answer is %d' %(y-x))
elif z == 'mo' or z == 'Mo':
 print('The answer is %d' %(y%x))
elif z == 'l' or z == 'L':
 print('The answer is %d' %(math.log(x,y)))
elif z == 'r' or z == 'R':
 print('The answer is %d' %(y**(1/x)))
answered Mar 25, 2016 at 12:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.