Can you please help me?

B

bobflipperdoodle

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!")
 
M

Mitya Sirenef

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!
 
B

bobflipperdoodle

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!!
 
B

bobflipperdoodle

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!!
 
M

Mitya Sirenef

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!
 
I

Ian Kelly

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 address removed) instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top