[Tutor] if/else option for making a choice
alan.gauld at btinternet.com
Mon Feb 18 21:08:14 CET 2013
On 18/02/13 18:22, Niclas Rautenhaus wrote:
> But unfortunately I am stuck with giving the user the choice to select
> an additional option!
> The extra items have got set values, but at the moment they all get
> summarized, but i want to choose.
> AFAIK I already tried:
> Print raw_input ((“Would you like to have leather?“) à then yes or no
> If raw_input == yes
> Print Leather (Leather) ß displaying the variables value
This isn't Python code and nothing like it appears in the code you
posted below. There are several problems with the code above:
- print does not start with uppercase P.
- You have two (( at the raw_input
- if starts lower case i
- raw_input is a function so you shouldn't compare it to yes
- you don't assign the input value to a variable(although you
do in the code below so you know you should...)
- you compare to yes but it probably should be "yes" - a string.
- The Print Leather line makes no sense at all.
If you are going to post code post real code.
Or else make it clear its pseudo code.
Now on to the code you did post...
> I hope it is clear where my problem is.
Did you try running it? Did you get an error?
If so what?
> #Car price calculator
> # Set variables for additional items
> Leather = (500)
Why in ()? you don;t need them
> int (Leather)
And this does nothing - its already an int - and you ignore the result.
> Clima = (1500)
> int (Clima)
> Hifi = (250)
> int (Hifi)
> Electrics = (750)
> int (Electrics)
> Comfort = (2500)
> int (Comfort)
See above for all of these.
> print "\t\t\tWelcome to the car price calculator!"
> print "\t\t\tCredits belong to Niclas Rautenhaus"
> print "\n\nPlease enter the basic price: "
> basic = int (raw_input())
Now the last line is what your code above should have looked like.
> # Tax is a percentage of the basic car price
> Tax = basic * 5 / 100
> int (Tax)
The int() gets thrown away but thats probably good because you
don't want to lose the fractional part of the tax value, which
is what int() would do.
> print "\nAdded items:"
> # Here the user should have the possibility to pick either yes or no
> print "\n\nLeather",(Leather)
> print "Clima",(Clima)
> print "Hifi", (Hifi)
> print "Electrics", (Electrics)
> print "Comfort", (Comfort)
> print "Tax", (Tax)
Again you don't need those parentheses
> # Last step: the picked items shall be summarized
> print "\n\nTotal grand:", basic + Leather + Clima + Hifi \
> + Electrics + Comfort + Tax
> raw_input ("\nPress the enter key to exit!")
Personally I'd have split it at the comma:
print "\n\nTotal grand:", \
basic + Leather + Clima + Hifi + Electrics + Comfort + Tax
But that's a minor quibble.
Ironically I might also have used parentheses to group the additions,
print "\n\nTotal grand:", (basic + Leather +
Clima + Hifi +
Electrics + Comfort +
Or perhaps better still put them in a list and used sum()
items = [basic, Leather, Clima, Hifi, Electrics, Comfort, Tax]
print "\n\nTotal grand:", sum(items)
Author of the Learn to Program web site
More information about the Tutor