Python Forum
Newb: Simple Explicit Formula
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. :(
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python newb need help Fictile 1 194 Apr-02-2024, 03:28 AM
Last Post: buran
  NameError issue with daughter's newb code MrGonk 2 1,449 Sep-16-2021, 01:29 PM
Last Post: BashBedlam
  Python “Formula” Package: How do I parse Excel formula with a range of cells? JaneTan 1 2,676 Jul-12-2021, 11:09 AM
Last Post: jefsummers
  Being explicit about the items inside a tuple argument rudihammad 3 2,446 Dec-04-2019, 08:10 AM
Last Post: perfringo
  Simple newb string question Involute 2 2,208 Sep-08-2019, 12:50 AM
Last Post: Involute
  please help this newb install pygame iofhua 7 5,925 May-15-2019, 01:09 PM
Last Post: buran
  Newb question: Debugging + Linting Python in Visual Studio Code Drone4four 1 2,423 Apr-15-2019, 06:19 AM
Last Post: perfringo
  Newb question about %02d %04d bennylava 30 19,434 Mar-05-2019, 11:23 PM
Last Post: snippsat
  Pthyon 3 question (newb) bennylava 11 5,845 Feb-28-2019, 06:04 PM
Last Post: buran
  newb selfie PatM 5 3,604 Feb-19-2019, 12:20 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020