Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Need Some help with Python


Darkracer13's Avatar
Member
0 0

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.```markupimport 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)```


ghost's Avatar
0 0

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?


Darkracer13's Avatar
Member
0 0

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.


ynori7's Avatar
Future Emperor of Earth
0 0

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:

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.

ghost's Avatar
0 0

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.


ynori7's Avatar
Future Emperor of Earth
0 0

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.


stranac's Avatar
Member
0 0

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


Darkracer13's Avatar
Member
0 0

Thanks for the tips i will edit it. would it worked if i did```markupimport 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


ynori7's Avatar
Future Emperor of Earth
0 0

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?


Darkracer13's Avatar
Member
0 0

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.


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)```

stranac's Avatar
Member
0 0

If you're not using the string module, don't import it. Importing a module is not something you must do.


techb's Avatar
Member
0 0

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


Darkracer13's Avatar
Member
0 0

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