Author Topic: Python Easy Examples help  (Read 14221 times)

0 Members and 2 Guests are viewing this topic.

Offline blankanon

  • Serf
  • *
  • Posts: 21
  • Cookies: -1
    • View Profile
Re: Python Easy Examples help
« Reply #30 on: August 17, 2011, 01:25:24 am »


Learning programming through videos seems like a bad idea to me. Just read a regular tutorial. Once you got a hang of python, then you could research specific things that interest you.




The videos are great since they are actually lecture videos. 


Code: [Select]
# Calculates the FV of an investment
# Compoud interest

def main():
    print("This program calculates FV of an investment")


    principal = eval(input("Enter the initial investment: "))
    apr = eval(input("Enter the apr: "))
    years = eval(input("Enter years of investment: "))


    for i in range(years):
        principal = principal * (1 + apr)


    print("The value in", years, "is:", principal)


I keep getting:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    main()
  File "C:/Python27/Projects/FV calculator.py", line 7, in main
    principal = eval(input("Enter the initial investment: "))
TypeError: eval() arg 1 must be a string or code object


This happens whenever I enter a number in.
« Last Edit: August 17, 2011, 01:26:40 am by blankanon »

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Python Easy Examples help
« Reply #31 on: August 17, 2011, 01:48:11 am »
why are u using eval?  and why aren't u using raw_input instead of input?
~Factionwars

Offline blankanon

  • Serf
  • *
  • Posts: 21
  • Cookies: -1
    • View Profile
Re: Python Easy Examples help
« Reply #32 on: August 17, 2011, 02:14:23 am »
The book I am using used that for this program.

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Python Easy Examples help
« Reply #33 on: August 17, 2011, 02:34:56 am »
Code: [Select]
# Calculates the FV of an investment
# Compoud interest

def main():
    print("This program calculates FV of an investment")


    principal = raw_input("Enter the initial investment: ")
    apr = raw_input("Enter the apr: ")
    years = raw_input("Enter years of investment: ")


    for i in range(years):
        principal = principal * (1 + apr)


    print("The value in", years, "is:", principal)
Use that ;)
~Factionwars

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: Python Easy Examples help
« Reply #34 on: August 17, 2011, 02:41:55 am »
Your code makes absolutely no sense. If you were able to get input in your R/P/S game, why'd you change it? Do you have any idea what input() & eval() do? Do you know what this means:

TypeError: eval() arg 1 must be a string or code object

and why python is telling you this. If not, find out. This is where learning starts... Not asking others to fix your code for you.

And yeah, your video lecture is a horrible idea.. Your time, not mine.


Offline blankanon

  • Serf
  • *
  • Posts: 21
  • Cookies: -1
    • View Profile
Re: Python Easy Examples help
« Reply #35 on: August 17, 2011, 03:22:21 am »
Your code makes absolutely no sense. If you were able to get input in your R/P/S game, why'd you change it? Do you have any idea what input() & eval() do? Do you know what this means:

TypeError: eval() arg 1 must be a string or code object

and why python is telling you this. If not, find out. This is where learning starts... Not asking others to fix your code for you.

And yeah, your video lecture is a horrible idea.. Your time, not mine.


First of all, I didn't ask anyone to fix my code.  I guess reading is optional on this forum.  I wanted to know why that error was occurring.  I was putting in numbers but it wasn't working. Furthermore, this problem I obtained from a book not the lectures.  If you read what I wrote, I stated that.


If you want to comment on my post, read first.  Type second.

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: Python Easy Examples help
« Reply #36 on: August 17, 2011, 03:47:28 am »
Quote
First of all, I didn't ask anyone to fix my code.

Then stop posting skiddy code here, google dipshit. You know when learning to program, I've never had to ask anyone on a forum for help.

Quote
I wanted to know why that error was occurring.  I was putting in numbers but it wasn't working.

Cus you're a moron.

Quote
I guess reading is optional on this forum.

yeah seems to be problem with you too, never said it was from lectures.. I was actually replying to this:
 
 
Quote
The videos are great since they are actually lecture videos.

ouch btw, -1 really hurts :'(
« Last Edit: August 17, 2011, 03:48:36 am by xzid »

Offline blankanon

  • Serf
  • *
  • Posts: 21
  • Cookies: -1
    • View Profile
Re: Python Easy Examples help
« Reply #37 on: August 17, 2011, 03:55:06 am »
Then stop posting skiddy code here, google dipshit. You know when learning to program, I've never had to ask anyone on a forum for help.

Cus you're a moron.

yeah seems to be problem with you too, never said it was from lectures.. I was actually replying to this:
 
 
ouch btw, -1 really hurts :'(


The feeble minded always resort to insults. 

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: Python Easy Examples help
« Reply #38 on: August 17, 2011, 03:55:38 am »

The feeble minded always resort to insults.

and a pussy never fights back.

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Python Easy Examples help
« Reply #39 on: August 18, 2011, 10:50:20 am »
Blankanon,    I am asking serveral times why are u using evals.
"Because the book tells me to"
NO why are u using evals?, google it, google what an eval is,  and come back later.
« Last Edit: August 18, 2011, 10:50:34 am by Factionwars »
~Factionwars

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: Python Easy Examples help
« Reply #40 on: February 05, 2012, 10:12:46 pm »
Ok,firstly I guess the reason why the book is using 'input()' is because the book is for Python 3.x
In Python 3.x 'input' is the same like 'raw_input' (<-- for strings) in 2.x, a thing like 'input' in 2.x for int does,as I know, not exist anymore.
Secondly 'eval' turns a string into an calculation.

The error is because you are using Python 2.x so 'input' will be an int and 'eval' needs a string.
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: Python Easy Examples help
« Reply #41 on: February 05, 2012, 10:22:46 pm »
And here the correct script:

Code: (python) [Select]
#Calculates the FV of an investment
#Compoud interest

def main():
    print 'This program calculates FV of an investment'

    principal = raw_input('Enter the initial investment: ')
    apr = raw_input('Enter the apr: ')
    years = input('Enter years of investment: ')

    for i in range(years):
        principal = eval(principal*(1+apr))
    print 'The value in ' + str(years) + ' is: ' + str(principal)
« Last Edit: May 13, 2012, 09:26:48 pm by Area_13 »
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Python Easy Examples help
« Reply #42 on: February 05, 2012, 11:03:27 pm »
And here the correct script:

#Calculates the FV of an investment
#Compoud interest

def main():
    print 'This program calculates FV of an investment'

    principal = raw_input('Enter the initial investment: ')
    apr = raw_input('Enter the apr: ')
    years = input('Enter years of investment: ')

    for i in range(years):
        principal = eval(principal*(1+apr))
    print 'The value in ' + str(years) + ' is: ' + str(principal)

This post of you is a real contribution, but next time try to evade old topics :).

Thanks
~Factionwars

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: Python Easy Examples help
« Reply #43 on: February 06, 2012, 05:42:53 pm »
I knew this was an old topic,but I thougt maybe someone other has the same problems with eval/input/raw_input and may look in this topic.
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow