Please help if you can!

Joshua Landau joshua.landau.ws at gmail.com
Wed Dec 26 18:57:39 EST 2012


THIS IS A LONG POST, BUT IF YOU WANT TO LEARN YOU SHOULD READ IT. SERIOUSLY.

UNLIKE Mitya Sirenef's THIS DOES NOT ASSUME MORE KNOWLEDGE THAN IS IN YOUR
POST ALREADY, ALTHOUGH HIS IS DEFINITELY BETTER OVERALL. AS SUCH, THERE ARE
NO FUNCTIONS.

OK. There are several small errors in here, but there's nothing too large
or worth much worry.

On 26 December 2012 21:40, <bobflipperdoodle at gmail.com> wrote:

> I really hope you can help!
>
> I need to create a program where the user can order any combination and
> quantity of 3 products. I then offer a 10% discount if the customer
> correctly answers a trivia question.  After that, there are 3 choices for
> shipping.
>
> I have most of the program completed but I'm struggling with the most
> important parts :/  I get the total of multiple orders of the same item,
> but we can't figure out how to total the entire order - before discounts
> and shipping - and then where to put any code referring back to the trivia
> question. Can somebody please help me with this? I would really appreciate
> it!
>

You write that you "need" to do this, which may hint that this is some sort
of homework. If so, it's generally a nice thing to say as much. That said,
as long as you've given a good shot at it it's normally fine.


> This is the code:
>

My *very first* thought about this code is that it's really badly spaced.
Don't put lines together so much! [https://gist.github.com/4383950] shows
how much nicer things look when they're partitioned more. You may not
agree, but it took about 10 seconds and I prefer it.


> shop_again = 'y'
>

Hold on! Hold on!
shop_again should have a True/False value. It is screaming to be a boolean.
"y" is a letter, not a boolean. Thus:

shop_again = True

This is important because you don't really want to get confused with all
your types. What if shop_again was later changed to be True when a button
was clicked. Why on earth would you set it to "y"? You'd set it to True.
Thus, the sensible option is to have your types right from the very start.

print("Welcome to the Star Wars Shop!")
> customer = eval(input("Is there a customer in line? (1 = yes, 2 = no)> "))
>

eval(input(TEXT)) is a *bad* idea.

First of all, eval is really dangerous. Answer "yes" instead and it'll just
crash. Answer True and it'll run... BUT do *neither* the if or the elif!
That's *bad*.
Secondly, you don't need it. Your:

"if(customer == 1)" could be "if(customer == '1')", which would work
without the eval.

And then you've got the standard of "Y/N". So a better question would be:

customer = input("Is there a customer in line? [Y/N]> ")

Finally, you want to accept "Y" *and* "y", so you'd really want:

customer = input("Is there a customer in line? [Y/N]> ").lower()

---------
customer = input("Is there a customer in line? [Y/N]> ").lower()
Because customer really deserves to be boolean [True/False], you'd want to
change it immediately.

customer = (customer == "y")

This second line assumes that all non-"y"s are False, but that's a folly
you'll have to live with for now.

while shop_again == 'y':
>

If you've changed shop_again to be boolean:

while shop_again:

Some people don't get how this line would make sense. But it does. The
"while" statement only cares if it's value it gets is "truthy". Here are
lots of truthy things:

"y" == "y"
True
False == False
not False
"egg loaf"
[1, 2, 1, False, False]

and here are some falsy things:

"n" == "y"
False
True == False
not True
""
[]

If this makes no sense, please just say.

    if (customer == 2):
>

Again, if you've done my changes from above:

if not customer:


>         print("Welcome to the Star Wars Memorabilia Shop!")
>         customer = eval(input("Is there a customer in line? (1 = yes, 2 =
> no)> "))
>

Again:

customer = input("Is there a customer in line? [Y/N]> ").lower()
customer = (customer == "y")

BUT HOLD ON!

Run your program and then answer "2" twice. What happens? It's not good.

The problem is that answering "2" to this second one doesn't skip the loop!

x = ASK
LOOP:
    if not x:
        ASK
    if x:
        STUFF
    MORE STUFF

Where you want:

x = ASK
LOOP:
    if not x:
        ASK
    if x:
        STUFF
        MORE STUFF

or (even better):

while not x:
    x = ASK
LOOP:
    STUFF
    MORE STUFF

The second is what I've just decided to call the "ask-until-yes" model.
Basically, you ask until you get a "yes", so you don't have to put the loop
anywhere else. By the time the first loop is over, you *know* that x is
True!

    elif (customer == 1):
>

Again, if you've done my changes from above:

elif customer:


>         print("Please select an item to update your order and any other
> number to check out.")
>         print("Yoda Figure: $10 each.")
>         print("Star Wars Movie DVD: $20 each.")
>         print("Death Star Lego Set: $200 each.")
>         print(" 1 for Yoda Figure")
>         print(" 2 for Star Wars Movie DVD")
>         print(" 3 for Death Star Lego Set")
>     order = eval(input("Order: "))
>

Again:

order = input("Order number: ")


>     if (order == 1):


If you've followed my advice:

if order == "1":


>         yoda = eval(input("How many Yoda Figures do you want? : "))

        total = 10 * yoda


Ooookkay. Now you're thinking: "BUT *surely* I need eval here!!(?)".

You don't.

yoda = int(input("How many Yoda Figures do you want? : "))

This is better because there are so many problems with eval, and int also
means that you can only order whole numbers of yodas.
You probably also want to call this "number_of_yodas", because you're not
defining what "yoda" is.


>         print("Total:", total)

        print("Current order:", yoda, "at", total)

    if (order == 2):


If you've followed my advice:

if order == "2":


>         movie = eval(input("How many Star Wars Movie DVDs do you want? :
> "))

        total = 20 * movie


As above:

number_of_movies = int(input("How many Star Wars Movie DVDs do you want? :
"))
total = 20 * number_of_movies

        print("Total:", total)

        print("Current order:", movie, "at", total)

    if (order == 3):


If you've followed my advice:

if order == "3":


>         legos = eval(input("How many Death Star Lego Sets do you want? :
> "))

        total = 200 * legos


As above:

number_of_legos = int(input("How many Death Star Lego Sets do you want? :
"))
total = 20 * number_of_legos


>         print("Total:", total)

        print("Current order:", legos, "at", total)


>     shop_again = input("Would you like to keep shopping? 'Y' for yes, 'N'
> for no: ")


Again:

shop_again = input("Would you like to keep shopping? [Y/N]> ").lower()
shop_again = (shop_again == "y")


>     print()

print("Your order before shipping and discounts: ",total)

print()

print("Answer a trivia question for a discount!")

discount = eval(input("On what planet did Yoda live when Luke Skywalker
> first met him? 1) Earth 2) Dagobah 3) Pluto :"))


Again:

discount = input("On what planet did Yoda live when Luke Skywalker first
met him? 1) Earth 2) Dagobah 3) Pluto :")

if (discount == 1):

    print("Sorry, that answer was wrong!")

if (discount == 2):

    print("That's correct, you get a 10% discount!")

if (discount == 3):

    print("Sorry, that answer was wrong!")


If you've taken my advice:

 if discount == "1":
    print("Sorry, that answer was wrong!")
if discount == "2":
    print("That's correct, you get a 10% discount!")
if discount == "3":
    print("Sorry, that answer was wrong!")


> print()

if (discount == 2):


If you've taken my advice:

if discount == "2":


>     (total * .9)


Hold on! What do you think this line does? What is it meant to do?

You meant:

total = total*.9

Which for clarity should be written:

total = total * 0.9


>     print("Your total before shipping: ",total)



print("1) Regular Shipping: 3-4 business days, $5.00 per $50 ordered. 2)
> Express Shipping: overnight, $10 per $50 ordered. 3) Super Saver Shipping:
> 7-10 business days, free.")

shipping = eval(input("Please select the shipping method you want: "))


Again:
shipping = input("Please select the shipping method you want: ")

if (shipping == 1):


If you've taken my advice:

if shipping == "1":


>     total == total % 50

    total == total * 5


Eeek! Almost!

1) You've mixed up "==" and "=".

Look them up if this was a confusion thing, but I imagine it was
a subconscious typo.

But then what? You've set total to the cost of shipping! What you want is:

shipping = total % 50
shipping = shipping * 5
total = total + shipping

2) %?? You mean "//". Look them up. Now.

"542353 % 50" makes 3!
"542353 // 50" makes 10847, which is what you want.

So:

shipping = total // 50
shipping = shipping * 5
total = total + shipping

    print("Your total is: ",total)

if (shipping == 2):


If you've taken my advice:

if shipping == "2":


>     total == total/50

    total == total % 50

    total == total * 10


See above, twice.


>     print("Your total is: ",total)

if(shipping == 3):


If you've taken my advice:

if shipping == "3":


>     print("Your total is: ",total)

print()

print("Thanks for shopping here! Please come again!")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121226/182cf46b/attachment.html>


More information about the Python-list mailing list