Re: [Tutor] variable * raw_input
Dewight Kramer wrote: > Hello, > > So i am new to Python, and really to programming. I picked up book > and so far I like it. right now I am trying to figure out a problem > that I cant. > > It is a tipping program and I have assigned certain words to be a > certain % and then I ask the user to type raw input of one of those > names. After which I am trying to mutiply the Bill by the Service > quality. But I dont understand something below is code. > > Thanks > D > > # Tip Calulator - Ver > # Challenges Ch2 - Question 3 > # Developed by, Dewight > # Developed on, 8/7/2007 > > > # establish variables > bill = float(0.0) > bad = float (0.0) > ok = float(0.10) > good = float (0.15) > great = float (0.20) > Speaking as a former waiter, this should probably read: great = float (0.25) good = float (0.20) ok = float(0.15) bad = float (0.10) eatathomeyoucheapskatebagofcrap = float(0.0) ;-) I now return you to your regularly scheduled Python discussion. > service="nothing" > tip = float () > total = float () > > print "This is a tipping calculator." > > bill = raw_input ("\nWhat was the total bill amount? ") > service = raw_input("Please input one of the following to"+ > " discribe the service:"+"\nbad\nok\ngood\ngreat > \n") > > tip = bill * service > total = bill + tip > print "Then you should leave a" + tip + "tip. This will bring the > total to" + total +"." > > > raw_input("\n\nPlease press enter to exit.") > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable * raw_input
"Dewight Kramer" <[EMAIL PROTECTED]> wrote > quality. But I dont understand something below is code. Since you don't tell us what exactly you don't undertand I'll make some general comments on the code... > # establish variables > bill = float(0.0) > bad = float (0.0) > ok = float(0.10) > good = float (0.15) > great = float (0.20) You don;t need the float() call because the values are already floats by dint of having a decimal point in them. float is really for converting non float values(integer or string) to floats. > service="nothing" > tip = float () > total = float () these lsat two will be the same as tip = 0.0 total = 0.0 > print "This is a tipping calculator." > > bill = raw_input ("\nWhat was the total bill amount? ") raw_input returns a string, you probably want to convert that to a float, so here you do want to use float(), like so: > bill = float(raw_input ("\nWhat was the total bill amount? ")) > service = raw_input("Please input one of the following to"+ > " describe the service:"+"\nbad\nok\ngood\ngreat > \n") You could use a triple quoted string here for clearer layout: service = raw_input( """Please input one of the following to describe the service: bad ok good great """) > tip = bill * service But now you are trying to multiply a number(bill) by a string ('ok', 'bad' etc) Thsat won;t work, instead you need to convert the string into some kind of number. The common way to do that would be using a dictionary: serviceVal = {'bad': 0, 'ok': 0.1, 'good': 0.15, 'great':0.2} and now the tip line looks like: tip = bill * serviceVal[service] > total = bill + tip > print "Then you should leave a" + tip + "tip. This will bring the > total to" + total +"." You might find this easier using string formatting print "Then you should leave a $%.2f tip. This will bring the total to $%.2f." % (tip, total) Notice how that lets you control what the output looks like more precisely. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] variable * raw_input
Hello, So i am new to Python, and really to programming. I picked up book and so far I like it. right now I am trying to figure out a problem that I cant. It is a tipping program and I have assigned certain words to be a certain % and then I ask the user to type raw input of one of those names. After which I am trying to mutiply the Bill by the Service quality. But I dont understand something below is code. Thanks D # Tip Calulator - Ver # Challenges Ch2 - Question 3 # Developed by, Dewight # Developed on, 8/7/2007 # establish variables bill = float(0.0) bad = float (0.0) ok = float(0.10) good = float (0.15) great = float (0.20) service="nothing" tip = float () total = float () print "This is a tipping calculator." bill = raw_input ("\nWhat was the total bill amount? ") service = raw_input("Please input one of the following to"+ " discribe the service:"+"\nbad\nok\ngood\ngreat \n") tip = bill * service total = bill + tip print "Then you should leave a" + tip + "tip. This will bring the total to" + total +"." raw_input("\n\nPlease press enter to exit.") ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor