Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Newb: Simple Explicit Formula
#1
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. :(
Quote
#2
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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
Question Formula / Algorithm help? alpho 0 126 Mar-18-2020, 06:16 PM
Last Post: alpho
  Being explicit about the items inside a tuple argument rudihammad 3 238 Dec-04-2019, 08:10 AM
Last Post: perfringo
  pyxcel_ods formula eslavko 1 152 Nov-20-2019, 01:02 PM
Last Post: ChislaineWijdeven
  Simple newb string question Involute 2 352 Sep-08-2019, 12:50 AM
Last Post: Involute
  please help this newb install pygame iofhua 7 1,138 May-15-2019, 01:09 PM
Last Post: buran
  Python Formula prophet11 5 629 Apr-17-2019, 07:40 PM
Last Post: perfringo
  Newb question: Debugging + Linting Python in Visual Studio Code Drone4four 1 435 Apr-15-2019, 06:19 AM
Last Post: perfringo
  Newb question about %02d %04d bennylava 30 4,457 Mar-05-2019, 11:23 PM
Last Post: snippsat
  Pthyon 3 question (newb) bennylava 11 1,123 Feb-28-2019, 06:04 PM
Last Post: buran
  newb selfie PatM 5 619 Feb-19-2019, 12:20 AM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)