views:

165

answers:

3
+2  Q: 

Python variables

Hey, I'm trying to create a basic program that will generate a number based on variables entered by the user. The formula is

a = b / (c * d)

This is a formula for finding specific heat, whereas b=energy, c=mass, and d= change in temperature.

So my problem is that I'm not making this program for myself, otherwise I could just assign each variable a number- like:

b= 1200
c= 13
d= 2

And then do a = a = b / (c * d).

My goal though, is to create a program for others who don't already know the formula- so that they can simply enter the numbers themselves. Example- I want b = X. X is the number entered by the user of the program. However, I have to define X as a variable first- I want it to be unknown- or based on what a person enters. I don't want b, c, or d to have assigned values. It's a very basic scripting process I know, but I'm new to Python.

Thanks a lot.

A: 
b = float(raw_input("Please enter a value: "))
a = b / (c*d)
print a

raw_input() prompts the user for input, assuming you're running this in the console

float() attempts to convert the parameter (in this case, the user input) to a float. the rest of it should be pretty straightforward.

Give that a try. Welcome to Python :)

inkedmn
Thanks a lot, I tried this and I know the actual script itself must work. My problem is that when I enter b = float(raw_input("Enter a value for energy: "))I end up with:b = float(raw_input("Enter a value for energy: "))Enter a value for energy:So I can't actually put anything there or it will assign B the variable of whatever I put. I have a feeling I have to put something at the beginning of my script recognizing it as a program or something. Sorry for my ignorance, once again I am very new to Python. Thanks for all the help guys!
@Jordan: Your comment makes no sense at all. Please rewrite it so it makes sense and update your question with whatever your *real* problem is.
S.Lott
+4  A: 

The most simple approach is to precede the formula with the fragment

b = input("Enter b:")
c = input("Enter c:")
d = input("Enter d:")

A few things to note:

  • this will require console IO, so you best start the script from a console
  • input() causes the string entered to be eval()'ed, meaning that it gets processes as if it was a Python expression. This is useful for numbers, but may have confusing side effects; consider using raw_input(), along with float() instead.
Martin v. Löwis
It's extremely wise to warn novices to Python that `input()` is dangerous in Python 2.x and earlier. In Python3 they've finally fixed that, but it was a long standing wart int he language (from when Guido conceived of it from one which was focused on teaching programming). Python < 3 performs an *evaluation* of `input()` values. It's the equivalent of `eval(input())` in Python 3.x.Students should be warned to use `raw_input()` or even to start their code with `input = raw_input` to re-name them function before it's used in their code.
Jim Dennis
+6  A: 

I think you want something like this:

b = float(raw_input("Energy? "))
c = float(raw_input("Mass? "))
d = float(raw_input("Change in temperature? "))

print "Specific heat: %f" % (b / (c * d))
  • raw_input() prompts the user and returns the inputted value
  • float() converts the value to a float (if possible; if not, this will throw an exception and terminate the program)
  • the "%f" in the last line formats the argument as a floating-point value, where "the argument" is the value of the expression following the % outside of the string (i.e. '(b / (c * d))')
dcrosta
Of course it's even better if we wrap that in some nice exception handling: def getNum(prompt="Enter a number: "): result = None while result is None: try: result = float(raw_input(prompt)) except ValueError: print "Couldn't parse that. Please try again." else: break return result
Jim Dennis
Gah! Can't format comments. Sorry for ugliness. :(
Jim Dennis