[Tutor] if/else option for making a choice
steve at alchemy.com
Mon Feb 18 20:15:23 CET 2013
On Mon, Feb 18, 2013 at 07:22:54PM +0100, Niclas Rautenhaus wrote:
> Hello folks,
> I hope it is clear where my problem is.
Not completely, but let's take a look at what you have so far.
> # Set variables for additional items
> Leather = (500)
> int (Leather)
> Clima = (1500)
> int (Clima)
You don't need to put parens around the values, but more importantly
note that the "int" lines don't do anything here. When you say:
> Hifi = (250)
> int (Hifi)
You set Hifi to the value 250 in the first line, which is fine, but
then you call int() to turn the integer 250 into the integer 250, and
then promptly discard that value. If you had, for example, a string
"250" and wanted to turn it into an integer, you could do something
Hifi = int("250")
But simply saying
accomplishes nothing. Do you see why?
> print "\n\nPlease enter the basic price: "
> basic = int (raw_input())
I'm curious why the price of the car has to be an integer.
Couldn't it be something like 15999.95?
> # Tax is a percentage of the basic car price
> Tax = basic * 5 / 100
You would be better off using floats here if you want a real number
as the answer. You're working in integers.
> int (Tax)
Or maybe you really want integers only to keep the numbers easier
or something. Fair enough, but again this line does absolutely
nothing. You truncate Tax to an integer but then discard that integer
value as soon as it's created.
> print "\nAdded items:"
> # Here the user should have the possibility to pick either yes or no
> print "\n\nLeather",(Leather)
The parentheses are unnecessary here.
To have them choose, you need some kind of "if" statement. Maybe something
choice = raw_input("Would you like windows?")
if choice == 'yes':
windows = 150
There are certainly more sophisticated ways to do this as well, when you get
past the basics of how conditionals (if statements) work. For example, making
a function that asks each question and handles all the different ways the user
might answer other than typing precisely "yes". Or putting the choices in a
list instead of repeating the choice-asking code over and over.
Steve Willoughby | Using billion-dollar satellites
steve at alchemy.com | to hunt for Tupperware.
More information about the Tutor