``````from __future__ import division
from math import sin
from math import cos
from math import tan
from math import radians
a = raw_input("enter 1 for sin, 2 for cos, 3 for tan")
if a == 1:
degree1 = int(raw_input("Type in angle value: ")
print sin
elif a == 2:
degree2 = int(raw_input("Type in angle value: ")
print cos
elif a == 3:
degree3 = int(raw_input("Type in angle value: ")
print tan``````

Erm... anyway to fix this? It says syntax error for the variables and print, and the raw_input under the if and elif is not shown.

## All 19 Replies

``````from __future__ import division
from math import sin
from math import cos
from math import tan
from math import radians
a = raw_input("enter 1 for sin, 2 for cos, 3 for tan")
if a == '1':  #most be string when compare(raw_input return string)
degree1 = int(raw_input("Type in angle value: "))  #missing )
print sin
elif a == '2':
degree2 = int(raw_input("Type in angle value: "))  #missing )
print cos
elif a == '3':
degree3 = int(raw_input("Type in angle value: "))  #missing )
print tan``````

1. I need to convert string to integer.
2. I was planning on doing sin instead of 1, cos instead of 2, etc. Possible?
3. Why does the print lines result in a syntax error?

1. I need to convert string to integer

``````>>> a = '5'
>>> type(a)
<type 'str'>
>>> a
'5'
>>> b = int(a)
>>> type(b)
<type 'int'>
>>> b
5

>>> a = raw_input()
5
>>> type(a)
<type 'str'>
>>> a = int(raw_input())
5
>>> type(a)
<type 'int'>
>>>``````

I was planning on doing sin instead of 1, cos instead of 2, etc. Possible?

``````a = raw_input("Enter sin,cos for calulate ")
if a == 'sin':
#do something
#and you should have a else statement
else:
print 'Input not correct'``````

3. Why does the print lines result in a syntax error?

Do you use python 2.7(advisable)
If python 3.x print has become a function so use print (tan)

2.7. And honestly, I'm a newb at this. Can you explain how the first 2 work?
When I test, it says syntax error, and points to the last letter of the variable/print.
Plus, unless the variables are gone, print ain't work.

Please re-post your problem code along with the entire (error) output. When I fixed the int casts and the top level dispatch (1,2,3), the code ran as expected for me.
At the top level, you can add this, to help diagnose trouble:

``````else:
print("Missed the expected options with value %s of type %s"%(a, type(a)))``````

When I fixed the int casts and the top level dispatch (1,2,3), the code ran as expected for me.

Er... that's all I need to know if it can fix the problem.

Sorry if I make you feel offensive.

Er... that's all I need to know if it can fix the problem.

Sorry if I make you feel offensive.

So how do we do it?

OK, here's some code as I would do it (approximately). This code works with python 2.x or python 3. It does not use if/elif/else since the right way to do a multi-way switch in Python is to use a dictionary. It does do a (very) little error checking, and tries to issue a useful diagnostic for some errors:

``````import math
import sys
if 3 > sys.version_info:
input = raw_input

lookup = {
'1': ('Sine',math.sin),
'sin': ('Sine',math.sin),
'2': ('Cosine',math.cos),
'cos': ('Cosine',math.cos),
'3': ('Tangent',math.tan),
'tan': ('Tangent',math.tan),
}

a = input("Enter 1 or sin, 2 or cos, 3 or tan: ").lower()[:3]
funcPair = lookup.get(a)
if not funcPair:
print("'%s' is not a legal function choice"%a)
sys.exit(1)
funcName, func = funcPair
try:
arg = int(input("Enter number of degrees for function %s: "%funcName))
print("%s of %s is %s"%(funcName,arg,val))
except:
print("Sorry, we can only calculate for integer degrees")
sys.exit(1)``````
commented: good code +12

griswolf soultion is ok.
You should not get error whit code i fixed over.

Testrun.

``````>>>
enter 1 for sin, 2 for cos, 3 for tan1
Type in angle value: 35
0.573576436351

enter 1 for sin, 2 for cos, 3 for tan3
Type in angle value: 45
1.0
>>>``````

snippsat: I agree, your fixed code works ok, but not as pythonic as it could be. (No blame: Neither is mine, completely; and you were starting from KrazyKitsune's code). I was responding to KrazyKitsune asking So how do we do it? Since he did not seem to have your fix working, I thought I would show something a little different.

Where mine is better is:

• better dispatch, not if/elif/else
• error messages * 2
• (I like it better) import math, not * from math

snippsat: I agree, your fixed code works ok, but not as pythonic as it could be. (No blame: Neither is mine, completely; and you were starting from KrazyKitsune's code). I was responding to KrazyKitsune asking So how do we do it? Since he did not seem to have your fix working, I thought I would show something a little different.

Where mine is better is:

• better dispatch, not if/elif/else
• error messages * 2
• (I like it better) import math, not * from math

I think you did very decent job. It is sometimes difficult to deside the appropriate style of reply though. Basically there is two response modes, do as little as possible to help OP to learn as much as possible himself, or show most Pythonic way possible and hope that OP catch some Zen of Python in trying to decipher the Oracle's answer.;)

Here is my look through of griswolfs code and little PEP8fying it, minimizing the try part to else: part and using 4 space standard tabs.

``````import math
import sys

if 3 > sys.version_info:
input = raw_input

lookup = {
'1': ('Sine',math.sin),
'sin': ('Sine',math.sin),
'2': ('Cosine',math.cos),
'cos': ('Cosine',math.cos),
'3': ('Tangent',math.tan),
'tan': ('Tangent',math.tan),
}

a = input("""
Enter
1 or sin,
2 or cos,
3 or tan: """).lower()[:3]

if a not in lookup:
print("'%s' is not a legal function choice"%a)
sys.exit(1)

funcName, func = lookup.get(a)

try:
arg = int(input("Enter number of degrees for function %s: "%funcName))
except:
print("Sorry, we can only calculate for integer degrees")
sys.exit(1)
else:
print("%s of %s is %s" %
(funcName,arg,val))``````

So, how do we add more parts? (like addition, for example)
And is it possible to remove certain %s in print without resulting in an error?

So, how do we add more parts? (like addition, for example)

You add more `key : (name,function)` to lookup.

And is it possible to remove certain %s in print without resulting in an error?

You must have a %something for every item in the tuple after the string and %; and you must have an item in the tuple for ever %something in the format string. So you can remove (or add) them in pairs. See http://docs.python.org/library/stdtypes.html#string-formatting-operations

In reply to tonyjv:

It is sometimes difficult to decide the appropriate style of reply though. Basically there is two response modes, do as little as possible to help OP to learn as much as possible himself, or show most Pythonic way possible and hope that OP catch some Zen of Python in trying to decipher the Oracle's answer.

Yes. I deliberately broke from the if/elif pattern KrazyKitsune was using in order to both show something that works and not write his "use if/elif/else" homework problem for him (if he had such a problem, of course).

I slightly disagree with your use of try/except/else, preferring to put the 'no exception' part directly into the try block. The advantage of my way: "it worked" code is all together in one block. The advantage of your way: just one thing in the try block, so no confusion about what exception happened.

Good marks for the nice prompt and showing use of triple quotes.

Mine (and yours) would be a little tighter if we handled the names, the functions and the prompt all from the same data structure. Might be good to zip that structure together too...

I tried, but it

``arg = int(input("add(Input value): "%fsum))``

So I assume we have to make another "try"?
And no, I did not have homework, but I couldn't think of any other way of trying to do this.

Oh my gawd... I just found out the problem in my if/elif calculator somehow; strings... But I'd like to keep continuing this thread so I can learn more about not using if/elif/else for a calculator. Hopefully this doesn't break any rules?

And tonyjv, nice script, but I'd prefer something somewhat original for now.

I tried, but it

``arg = int(input("add(Input value): "%fsum))``

So I assume we have to make another "try"?
And no, I did not have homework, but I couldn't think of any other way of trying to do this.

You have not put a "%s" part of the string to fill in with fsum. Drop the %fsum part and it will at least run, though I don't know if correctly.

You have not put a "%s" part of the string to fill in with fsum. Drop the %fsum part and it will at least run, though I don't know if correctly.

Assuming I did

``````'4': ('add',math.fsum),