Exercise in the second week,

Source: Internet
Author: User

Exercise in the second week,

# Shopping Cart program

''' After the program is started, ask the user to enter the salary and print the commodity list
The user selects the purchased item based on the number.
If the user has enough money, he will directly deduct the money.
You can exit at any time. When you exit, print the purchased items and balances '''

Salary = input ("Enter your salary this month :")
Goods = ['1. cola (12) ', '2. sprite (13) ', '3. notebook (5) ', '4. iPhone (8888) ', '5. earphone (3643) ']
Price = [12, 13, 5, 8888,364 3]
Cost = 0
Bought = []
Startshop = 1
K = 0
Print (goods)
While startshop = 1:
I = input ('Please type the goods number of what you want to buy, when you are done, type "0" to quit :')
If int (I) = 0:
Startshop = 0
Else:
If price [int (I)-1] + cost> int (salary ):
Print ("You don't have enough money .")
J = input ('Do you want to buy other goods? Type 1 for yes, type 0 for quit :')
If int (j) = 1:
Continue
If int (j) = 0:
Startshop = 0
Else:
Cost = cost + price [int (I)-1]
Bought. insert (k, goods [int (I)-1])
K + = 1
Print ("You have bought", bought [:], "and cost", cost, "yuan. "," Now you have ", int (salary)-cost," yuan left ")

-----------------------------------------------------New Version-------------------------------------------------------

salary=input("Please input your salary: ")
if salary.isdigit():
salary = int(salary)
goods=['Coco(12 yuan)','Sprite(13 yuan)','Notebook(5 yuan)','iPhone(8888 yuan)','Earphone(3643 yuan)']
price=[12, 13, 5, 8888, 3643]
cost=0
bought = []
startshop = 1
k = 0
for a in enumerate(goods): print(a)
while True:
i = input('Please type the goods number of what you want to buy, when you are done, type "q" to quit>>> ')
if i.isdigit():
i = int(i)
if i > 4 or i < 0:
print('Pleasure type the number shown before the merchandise.')
continue
if price[i] + cost > int(salary):
print("You don't have enough money.")
while True:
j = input('Do you want to buy other goods? Type y for yes, type q for quit: ')
if j == 'y':
break
elif j == 'q':
print("You have bought")
for w in bought:
print(w)
print("and cost", cost, "yuan.", "Now you have", salary - cost, "yuan left.")
exit()
else:
print('\033[41;1mPleasure type "y" or "q"!\033[0m')
continue
else:
cost = cost + price[i]
bought.insert(k, goods[i])
k += 1
print('Added', goods[i], 'to your list successfully. Now you have', salary - cost, 'yuan left.')
elif i == 'q':
print("You have bought")
for w in bought:
print(w)
print("and cost", cost, "yuan.")
print("Now you have", salary - cost, "yuan left.")
exit()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.