Python Forum

Full Version: Newb: Simple Explicit Formula
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am incredibly new to python (I'm officially on my fourth day of learning) and as a test of what I've learned so far, I wanted to make a simple Explicit Formula calculator. The formula is:

a(n) = a(1) + (n - 1)d

This is what I came up with:

aone = raw_input("Enter the first term: ")
n = raw_input("Enter the n term: ")
d = raw_input("Enter the difference: ")

answer = n - 1
answer = answer * d
answer = answer + aone

print answer
The printout from the terminal is this: 
Output:
python expForm.py Enter the first term: 2 Enter the n term: 100 Enter the difference: 5 Traceback (most recent call last):   File "expForm.py", line 5, in <module>     answer = eval('aone + (nterm - 1) * diff')   File "<string>", line 1, in <module> TypeError: unsupported operand type(s) for -: 'str' and 'int'
I'm sorry if I look like the biggest imbecile on the planet, but this is beyond me at this stage in my coding abilities. Sorry. :(
raw_input (in Python2) and input (the equivalent in Python3) return str. you need to convert that to int or float to use in calculations

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = raw_input('some number: ')
some number: 3
>>> x
'3'
>>> type(x)
<type 'str'>
>>> int(x)
3
>>> float(x)
3.0