Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Can you please help me?

Reply
Thread Tools

Can you please help me?

 
 
bobflipperdoodle@gmail.com
Guest
Posts: n/a
 
      12-26-2012
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!

This is the code:


shop_again = 'y'

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

elif (customer == 1):
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: "))


if (order == 1):
yoda = eval(input("How many Yoda Figures do you want? : "))
total = 10 * yoda
print("Total:", total)
print("Current order:", yoda, "at", total)
if (order == 2):
movie = eval(input("How many Star Wars Movie DVDs do you want? : "))
total = 20 * movie
print("Total:", total)
print("Current order:", movie, "at", total)
if (order == 3):
legos = eval(input("How many Death Star Lego Sets do you want? : "))
total = 200 * 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: ")
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 :"))
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):
(total * .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: "))

if (shipping == 1):

total == total % 50
total == total * 5
print("Your total is: ",total)
if (shipping == 2):
total == total/50
total == total % 50
total == total * 10
print("Your total is: ",total)
if(shipping == 3):
print("Your total is: ",total)
print()
print("Thanks for shopping here! Please come again!")

 
Reply With Quote
 
 
 
 
Mitya Sirenef
Guest
Posts: n/a
 
      12-26-2012
On 12/26/2012 04:40 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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!
>
> This is the code:
>
>
> shop_again = 'y'
>
> print("Welcome to the Star Wars Shop!")
> customer = eval(input("Is there a customer in line? (1 = yes, 2 = no)> "))
> while shop_again == 'y':
> if (customer == 2):
> print("Welcome to the Star Wars Memorabilia Shop!")
> customer = eval(input("Is there a customer in line? (1 = yes, 2 = no)> "))
>
> elif (customer == 1):
> 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: "))
>
>
> if (order == 1):
> yoda = eval(input("How many Yoda Figures do you want? : "))
> total = 10 * yoda
> print("Total:", total)
> print("Current order:", yoda, "at", total)
> if (order == 2):
> movie = eval(input("How many Star Wars Movie DVDs do you want? : "))
> total = 20 * movie
> print("Total:", total)
> print("Current order:", movie, "at", total)
> if (order == 3):
> legos = eval(input("How many Death Star Lego Sets do you want? : "))
> total = 200 * 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: ")
> 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 :"))
> 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):
> (total * .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: "))
>
> if (shipping == 1):
>
> total == total % 50
> total == total * 5
> print("Your total is: ",total)
> if (shipping == 2):
> total == total/50
> total == total % 50
> total == total * 10
> print("Your total is: ",total)
> if(shipping == 3):
> print("Your total is: ",total)
> print()
> print("Thanks for shopping here! Please come again!")
>



I think before worrying about totals, et cetera, you need
to approach this in a more modular way. Here's an
example of using 3 functions to do the product menu,
where each choice gets 1 count of product, without
option to specify quantity:


from collections import defaultdict

prompt = "> "
cart = defaultdict(int)
product_tpl = "%d) %-15s [ $%d ]"
products = {
1: ("Yoda", 10),
2: ("DVD", 20),
3: ("Lego Set", 200)
}

def product_menu():
while True:
print("Select item to order, 'f' when finished.")
for pid, prod in products.items():
print(product_tpl % (pid, prod[0], prod[1]))

inp = input(prompt)
if inp == 'f': return
product = getproduct(inp)

if product : cart[product] += 1
else : print("Invalid Input")
print()

def getproduct(inp):
try:
return products[int(inp)]
except (ValueError, KeyError):
return None

def main():
product_menu()

for (name, price), count in cart.items():
print("product: %s, price: %d, count: %d" % (name, price, count))
total = sum(price*count for (name, price), count in cart.items())
print("Total for the order is: $%d" % total)

main()


A few notes:

The code is a lot simpler when you define the collection of products
outside of the selection loop. It also means you can reuse the products
dict in other functions.

It's easier, usually, to work with 'while True' loops because you have
precise control of where it breaks, either with a break statement
or with a return. In the type of loop you used, you had to define
the default value at the top, then change that value, and then
by looking at the line where you change the value, it's not
obvious whether it will terminate or not, you also need to
check what is the value that will terminate and whether it is
changed again before the end of loop. Compare this with
a return statement: you know precisely that it DOES terminate
AND you know that it happens at that line you're looking at.

Also note how the input is handled: I don't care what kind
of mistake the user makes, I just want to know if he chose
the existing product or not, so I try to return the product
and if it fails for whatever reason, I tell the user that input
was wrong.

Arguably it might be better to handle the menu exit in
that function, as well, but this code works pretty well,
I think.

The way defaultdict works is that it initializes value
to int() when it does not exist yet, and int() returns
0, so cart[product] += 1 adds 1 product initially,
which is what we want.

By the way, you could do the same (and might be
a bit clearer, but more verbose), by initializing the
default dict in this way:

cart = defaultdict(lambda: 0)

Hope this helps!

--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

 
Reply With Quote
 
 
 
 
bobflipperdoodle@gmail.com
Guest
Posts: n/a
 
      12-26-2012
Thank you very much for your reply. I actually just deleted this post as you were replying! I had figured out a few things and then got confused about a few others :/ If you have a chance, can you look at the other post? Thank you!!
 
Reply With Quote
 
bobflipperdoodle@gmail.com
Guest
Posts: n/a
 
      12-26-2012
Thank you very much for your reply. I actually just deleted this post as you were replying! I had figured out a few things and then got confused about a few others :/ If you have a chance, can you look at the other post? Thank you!!
 
Reply With Quote
 
Mitya Sirenef
Guest
Posts: n/a
 
      12-26-2012
On 12/26/2012 06:21 PM, (E-Mail Removed) wrote:
> Thank you very much for your reply. I actually just deleted this post as you were replying! I had figured out a few things and then got confused about a few others :/ If you have a chance, can you look at the other post? Thank you!!



Fortunately, my reply is as relevant to the 2nd post just as much as to
the 1st .
I must be able to see future!

--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      12-27-2012
On Wed, Dec 26, 2012 at 4:21 PM, <(E-Mail Removed)> wrote:
> Thank you very much for your reply. I actually just deleted this post as you were replying! I had figured out a few things and then got confused about a few others :/ If you have a chance, can you look at the other post? Thank you!!


You can't actually delete posts, because this is a Usenet newsgroup
and mailing list. The Google group is just a gateway to the
newsgroup. There are incidentally a number of users who filter out
all posts from Google Groups due to the high level of spam posts
originating from there, so you would be likely to get a better
response if you ditched the Google group and posted directly to
comp.lang.python or (E-Mail Removed) instead.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can you spare 10 minutes to do a survey? You can be a valuable help to university researchers! daniel DVD Video 0 03-02-2007 02:19 AM
Please can you help me how can apply unicode in java yadoosmart@gmail.com Java 1 09-26-2006 04:11 PM
Stupid question. Please, only stupid responders. If you're not sureif you're stupid, you probably aren't. =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-18-2005 05:11 AM
HELP! HELP! PLEASE, PLEASE, PLEASE tpg comcntr Computer Support 11 02-15-2004 06:22 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments