Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Newb: Simple Explicit Formula Duplicitous Unladen Swallow Posts: 1 Threads: 1 Joined: May 2017 Reputation: 0 Likes received: 0 #1 May-05-2017, 06:48 PM (This post was last modified: May-05-2017, 06:49 PM by Duplicitous. Edited 2 times in total.) 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     answer = eval('aone + (nterm - 1) * diff')   File "", line 1, in 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. :( buran Posts: 5,547 Threads: 98 Joined: Sep 2016 Reputation: 415 Likes received: 1188 #2 May-05-2017, 07:03 PM (This post was last modified: May-05-2017, 07:03 PM by buran. Edited 1 time in total.) `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) >>> int(x) 3 >>> float(x) 3.0 ``` « Next Oldest | Next Newest »

Top Page

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

Forum Jump:

Users browsing this thread: 1 Guest(s)