Join us at IRC!
Capitalism is an Island of wealth in a sea of poverty
Friday, May 25, 2012
Navigation
Members Online
Total Online: 31
Web Spiders: 13
Guests Online: 29
Members Online: 2

Registered Members: 70208
Newest Member: andresuran
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Need Some help with Python

Darkracer13
Member



Posts: 11
Location:
Joined: 24.10.09
Rank:
Wiseman
Posted on 24-02-10 17:58
Ok my friend has been playing a game on Facebook called Farmville and i wanted to make a program out of python to tell him which crops are more profitable. well i ran into problems with trying to substart 15 from a number the user puts in. If you can help here is the code soruce.
import string

e2 = raw_input("Enter Plants Name: >> ")
f2 = raw_input("Enter a Plants Cost: >> ")
g2 = raw_input("Enter The Reward Cost: >>")
h2 = raw_input("Enter The Number of crops you can plant up to 2 Days: >> ")
a2 = 15
b2 = g2
c2 = h2
t1 = f2 count-15

print "%d Cost %d.", profit (e2,t1)



Author

RE: Need Some help with Python

wolfmankurd
Member



Posts: 1519
Location: UK
Joined: 30.05.05
Rank:
God
Posted on 24-02-10 18:02
what? do you mean f2-15? whats the error you're getting.

If your post doesn't make sense how are we to help you?


BY READING MY POST, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE USE OF THIS (MIS)INFORMATION.


Widowmakr@hotmail.com http://LetsHackStuff.com
Author

RE: Need Some help with Python

Darkracer13
Member



Posts: 11
Location:
Joined: 24.10.09
Rank:
Wiseman
Posted on 24-02-10 18:09
when i do the f2-15 i get a
TypeError: unsupported operand type(s) for -: 'str' and 'int'

so i looked around and tried some things and none of them worked. count- was the last on i tried

im new to python and tring to figure the math parts out.




Edited by Darkracer13 on 24-02-10 18:17
Author

RE: Need Some help with Python

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 24-02-10 18:18
Darkracer13 wrote:
when i do the f2-15 i get a
TypeError: unsupported operand type(s) for -: 'str' and 'int'

so i looked around and try thing and none of them worked. count- was the last on i tryed

raw_input returns a string. You need to typecast it to an int or float (I don't know which you need in your case). Something like this:
e2 = raw_input("Enter Plants Name: >> ")
f2 = float( raw_input("Enter a Plants Cost: >> "))
g2 = float(raw_input("Enter The Reward Cost: >>"))
h2 = int(raw_input("Enter The Number of crops you can plant up to 2 Days: >> "))
a2 = 15
t1 = f2 count-15 #I don't know what count is supposed to be.

print "%s Cost %d.", profit (e2,t1)#note the change on this line. %d is a decimal number


And you really ought to think about giving your variables more meaningful names.






Edited by ynori7 on 24-02-10 18:23
ynori7 http://halls-of-valhalla.org
Author

RE: Need Some help with Python

stdio
Member

Posts: 375
Location: omnipresent
Joined: 06.04.08
Rank:
God
Posted on 24-02-10 18:20
raw_input always returns a string. Strings cannot be used in mathematical operations without being typecasted and example would be ...


f2 = raw_input("Enter a Plants Cost: >> ")
f2= int(f2)


Now f2 would be an integer instead of a string. Note this does not do error checking to make sure it is a valid input according to your specifications.

//
Love it when I make a post and its out done with a better response too prior to hitting the submit button =P Anyways you have your answer.



I'm sorry, I cant hear you over the sound of how awesome I am!


Edited by stdio on 24-02-10 18:23
www.thewebsiteisdown.com
Author

RE: Need Some help with Python

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 24-02-10 18:25
MoshBat wrote:
ynori beat us.

I'm a quick-draw. ;)

@OP - In case you didn't notice, I edited my post to point out a mistake in your print statement as well.




ynori7 http://halls-of-valhalla.org
Author

RE: Need Some help with Python

stranac
Member

Posts: 124
Location: Croatia
Joined: 15.11.08
Rank:
God
Posted on 24-02-10 18:26
ynori7 wrote:
raw_input returns a string. You need to typecast it to an int or float .


Or you could simply use input() which will return a number
Author

RE: Need Some help with Python

Darkracer13
Member



Posts: 11
Location:
Joined: 24.10.09
Rank:
Wiseman
Posted on 24-02-10 18:29
Thanks for the tips i will edit it. would it worked if i did
import string

Name = raw_input("Enter Plants Name: >> ")
Plots = raw_input("Enter Number of plots you are going to Plant on: >> ")
Cost = raw_input("Enter a Plants Cost: >> ")
Gain = raw_input("Enter The Amount Gained from a single Plant: >>")
Time = raw_input("Enter The Number of crops you can plant up to 2 Days: >> ")
Ploting = int(Plots)*15

profit = a2

print "Your Profit is:", a2

and then subtracting it from the total money gained. this is in the beta form with out all the other math stuff or.. would what you guys said require me not to do all of this?? like the int(Plots)*15


Author

RE: Need Some help with Python

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 24-02-10 18:43
Darkracer13 wrote:
would what you guys said require me not to do all of this?? like the int(Plots)*15

How hard is it to say int()? And I wouldn't recommend the input function because it's unsafe. It evaluates the input as a python command. For your case it'd probably be fine, but I'd get into the habit of not using it.

And why do you keep importing string?




ynori7 http://halls-of-valhalla.org
Author

RE: Need Some help with Python

Darkracer13
Member



Posts: 11
Location:
Joined: 24.10.09
Rank:
Wiseman
Posted on 24-02-10 18:46
if its the import string at the top i really dont know what to change it to. but i have changed the code around a little bit and it works so far.
import string

Name = raw_input("Enter Plants Name: >> ")
Plots = float( raw_input("Enter Number of plots you are going to Plant on: >> "))
Costs = float( raw_input("Enter a Plants Cost: >> "))
Gained = float( raw_input("Enter The Amount Gained: >>"))
Time = int(raw_input("Enter The Number of crops you can plant up to 2 Days: >> "))
a2 = Plots*15


print "The Cost of Plowing is:", (a2)



Author

RE: Need Some help with Python

stranac
Member

Posts: 124
Location: Croatia
Joined: 15.11.08
Rank:
God
Posted on 24-02-10 18:48
If you're not using the string module, don't import it. Importing a module is not something you must do.
Author

RE: Need Some help with Python

techb
Member



Posts: 384
Location:
Joined: 15.02.09
Rank:
Hacker Level 2
Posted on 24-02-10 19:01
you could also use atof from the locale module if you really feel you need to import something

import locale

string = raw_input("enter a number")

stringToFloatPoint = locale.atof(string)

print stringToFloatPoint


locale.atof() = pull float point from string
locale.atoi() = pull integer from string



Edited by techb on 24-02-10 19:03
kbcarte.wordpress.com
Author

RE: Need Some help with Python

Darkracer13
Member



Posts: 11
Location:
Joined: 24.10.09
Rank:
Wiseman
Posted on 24-02-10 19:29
Ok i got it. this is what i have ended up with with your guys help. its my first program with python. Thanks

money = float( raw_input("Enter how much money you have: >>"))
name = raw_input("Enter Plants Name: >> ")
plots = float( raw_input("Enter Number of plots you are going to Plant on: >> "))
costs = float( raw_input("Enter a Plants Cost: >> "))
earned = float( raw_input("Enter The Amount Earned from Sell: >>"))
time = int(raw_input("Enter The Number of crops you can plant up to 2 Days: >> "))


cost_of_plots = plots*15
cost_of_planting = plots*costs
total_cost = cost_of_plots+cost_of_planting
total_income = earned*plots
money_lost = money-total_cost
mod_income = total_income-total_cost
monetary_adjustment = mod_income+money_lost




print "The Cost of Plowing is:", (cost_of_plots)
print "The Cost of Planting is:", (cost_of_planting)
print "So far it Costs: ", (total_cost)
print "Which brings your money to: ", (money_lost)
print "Your Profits are: ", (mod_income)
print "Your Money with Profits is: ", (monetary_adjustment)
all the Profits weren't right. But i fixed it




Edited by Darkracer13 on 24-02-10 20:02
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.