Jump to content

Very Simple Calculator Help

Creepz
Go to solution Solved by RedWulf,
4 minutes ago, Creepz said:


Traceback (most recent call last):
  File "C:/Users/Devin/test.py", line 15, in <module>
    operator = input('What do you want to do: multiply, divide, add, subtract?\n')
  File "<string>", line 1, in <module>
NameError: name 'divide' is not defined

Process finished with exit code 1

 

hmm, odd

It seems as if it is treating the input as a variable, the one thing I can think of is using raw_input instead of input. But that shouldn't be an issue if the other operators work.

I'm not sure why it only raises the error in divide so these suggestions are kinda hacky fixes.

Spoiler

You might try specifying str(input()) to set the input as a string.

 

You can try double parenthesis " instead of '

 

Perhaps set the divide option as the default else: 

 

Try putting the last part(after you enter divide) into a function

 I would think maybe an install or version issue but again, the others working rule that out. 

@fizzlesticks has more of a handle on python than I do

number = int(input("What is your first number you want to work with??\n"))
if(number%2>0):
    print("Your number is odd")
else:
    print("Your number is even")
number_2= int(input("What is another number you want to work with?\n"))
if(number_2%2>0):
    print("Your number is odd")
else:
    print("Your number is even")
operator = int(input('What do you want to do: multiply, divide, add, subtract?\n'))
if(operator == 1):
    print(number * number_2)
elif(operator == 'divide'):
    print(number / number_2)
elif(operator == 'add'):
    print(number + number_2)
elif(operator == 'subtract'):
    print(number - number_2)
else:
    print ("Okay")

How can I make my calculator divide when I type divide into the third question? It currently works if I use integers instead of strings. 

Link to comment
Share on other sites

Link to post
Share on other sites

Of course it doesn't work with strings since you're casting the input to an integer.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

29 minutes ago, Nineshadow said:

Of course it doesn't work with strings since you're casting the input to an integer.

It doesn't work either way. I changed it to an int after it not working to see if there was another problem. 

number = int(input("What is your first number you want to work with??\n"))
if(number%2>0):
    print("Your number is odd")
else:
    print("Your number is even")
number_2= int(input("What is another number you want to work with?\n"))
if(number_2%2>0):
    print("Your number is odd")
else:
    print("Your number is even")
operator = input('What do you want to do: multiply, divide, add, subtract?\n')
if(operator=='multiply'):
    print(number * number_2)
elif(operator=='divide'):
    print(number / number_2)
elif(operator=='add'):
    print(number + number_2)
elif(operator=='subtract'):
    print(number - number_2)
else:
    print ("Okay")

This doesn't work. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Creepz said:

It doesn't work either way. I changed it to an int after it not working to see if there was another problem. 


number = int(input("What is your first number you want to work with??\n"))
if(number%2>0):
    print("Your number is odd")
else:
    print("Your number is even")
number_2= int(input("What is another number you want to work with?\n"))
if(number_2%2>0):
    print("Your number is odd")
else:
    print("Your number is even")
operator = input('What do you want to do: multiply, divide, add, subtract?\n')
if(operator=='multiply'):
    print(number * number_2)
elif(operator=='divide'):
    print(number / number_2)
elif(operator=='add'):
    print(number + number_2)
elif(operator=='subtract'):
    print(number - number_2)
else:
    print ("Okay")

This doesn't work. 

I think I may have miss read the OP, whats the issue? Was long as the word input isn't taken as "int" it works for me. 

Int stands for integer which is numerical, the words offered are just that, words and are read as strings. 

the int input only works with whole numbers, by definition of an integer, so if you use 2.04 or 3.15 or 13/4 it won't work. Try the float input type instead.

screen1a.JPG

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[i7-7700k@5Ghz | MSI Z270 M7 | 16GB 3000 GEIL EVOX | STRIX ROG 1060 OC 6G | EVGA G2 650W | ROSEWILL B2 SPIRIT | SANDISK 256GB M2 | 4x 1TB Seagate Barracudas RAID 10 ]

[i3-4360 | mini-itx potato | 4gb DDR3-1600 | 8tb wd red | 250gb seagate| Debian 9 ]

[Dell Inspiron 15 5567] 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, RedWulf said:

...

number = int(input("What is your first number you want to work with??\n"))
if(number%2>0):
    print("Your number is odd")
else:
    print("Your number is even")
number_2= int(input("What is another number you want to work with?\n"))
if(number_2%2>0):
    print("Your number is odd")
else:
    print("Your number is even")
operator = input('What do you want to do: multiply, divide, add, subtract?\n')
if(operator=='multiply'):
    print(number * number_2)
elif(operator=='divide'):
    print(number / number_2)
elif(operator=='add'):
    print(number + number_2)
elif(operator=='subtract'):
    print(number - number_2)
else:
    print ("Okay")

Whenever I try to run mine, this happens:

 

What is another number you want to work with?
6
Your number is even
What do you want to do: multiply, divide, add, subtract?
divide
Traceback (most recent call last):
  File "C:/Users/Devin/test.py", line 15, in <module>
    operator = input('What do you want to do: multiply, divide, add, subtract?\n')
  File "<string>", line 1, in <module>
NameError: name 'divide' is not defined

Process finished with exit code 1

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Creepz said:


Traceback (most recent call last):
  File "C:/Users/Devin/test.py", line 15, in <module>
    operator = input('What do you want to do: multiply, divide, add, subtract?\n')
  File "<string>", line 1, in <module>
NameError: name 'divide' is not defined

Process finished with exit code 1

 

hmm, odd

It seems as if it is treating the input as a variable, the one thing I can think of is using raw_input instead of input. But that shouldn't be an issue if the other operators work.

I'm not sure why it only raises the error in divide so these suggestions are kinda hacky fixes.

Spoiler

You might try specifying str(input()) to set the input as a string.

 

You can try double parenthesis " instead of '

 

Perhaps set the divide option as the default else: 

 

Try putting the last part(after you enter divide) into a function

 I would think maybe an install or version issue but again, the others working rule that out. 

@fizzlesticks has more of a handle on python than I do

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[i7-7700k@5Ghz | MSI Z270 M7 | 16GB 3000 GEIL EVOX | STRIX ROG 1060 OC 6G | EVGA G2 650W | ROSEWILL B2 SPIRIT | SANDISK 256GB M2 | 4x 1TB Seagate Barracudas RAID 10 ]

[i3-4360 | mini-itx potato | 4gb DDR3-1600 | 8tb wd red | 250gb seagate| Debian 9 ]

[Dell Inspiron 15 5567] 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, RedWulf said:

hmm, odd

It seems as if it is treating the input as a variable, the one thing I can think of is using raw_input instead of input. But that shouldn't be an issue if the other operators work.

I'm not sure why it only raises the error in divide so these suggestions are kinda hacky fixes.

  Reveal hidden contents

You might try specifying str(input()) to set the input as a string.

 

You can try double parenthesis " instead of '

 

Perhaps set the divide option as the default else: 

 

Try putting the last part(after you enter divide) into a function

 I would think maybe an install or version issue but again, the others working rule that out. 

@fizzlesticks has more of a handle on python than I do

It worked, thank you!

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Creepz said:

It worked, thank you!

Just a PS if it happens again

the input() type tries to interpret the input as python, so in python 2 use raw_input() 

python3, where I dwell, doesn't have the same issue

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[i7-7700k@5Ghz | MSI Z270 M7 | 16GB 3000 GEIL EVOX | STRIX ROG 1060 OC 6G | EVGA G2 650W | ROSEWILL B2 SPIRIT | SANDISK 256GB M2 | 4x 1TB Seagate Barracudas RAID 10 ]

[i3-4360 | mini-itx potato | 4gb DDR3-1600 | 8tb wd red | 250gb seagate| Debian 9 ]

[Dell Inspiron 15 5567] 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×