Reputation: 11
this is my code:
cart = ['S/n'," "*10, 'Items', " " * 15, "Quantity", " " * 8, "Unit Price", " " * 6, "Price"]
total_pricee = 0
pricee = 0
count=1
def invalid_input(Quantitiy):
while Quantitiy > '5' or Quantitiy < '1':
Quantitiy = input("Please key in a valid quantity(Between 1 to 4):")
if Quantitiy < '5' and Quantitiy > '1':
New_Quan=Quantitiy
break
while not Quantitiy.isdigit():
Quantitiy = input('Invalid input.Please enter a valid input:')
while Quantitiy.isdecimal() == False:
break
def add_to_cart(name, Quantity, price):
global total_pricee, pricee,count
cart.append('\n')
cart.append('{:<10s}'.format(str(count)+'.'))
cart.append('{:^10s}'.format(name))
cart.append('{:^30s}'.format(Quantity))
cart.append('{:^10s}'.format('$' + '{:.2f}'.format(float(price))))
pricee = '{:.2f}'.format(float(Quantity) * price)
cart.append('{:^23s}'.format('$' + str(pricee)))
total_pricee += float(pricee)
count = count +1
while True:
print('[1] Water')
print('[2] rice')
print('[3] ice')
print('[0] View Cart')
opt = input("Select option:")
if opt > '3' or opt < '0':
print("Select valid option!")
if opt == '3':
qunt = input("How many would you like?")
invalid_input(qunt)
nam3 = "Ice"
add_to_cart(nam3, qunt, 2)
if opt == '1':
qunt2 = input("How many would you like?")
invalid_input(qunt2)
nam2 = "Water"
add_to_cart(nam2, qunt2, 3)
if opt == '2':
qunt1 = input("How many would you like?")
invalid_input(qunt1)
nam1 = "Rice"
add_to_cart(nam1, qunt1, 5)
if opt == "0":
print(*cart)
print("Total price until now:", "$" + '{:.2f}'.format(total_pricee))
print('Would you like to check out?')
print('[1] Yes')
print('[2] No')
checkout=input("Please select an option:")
if checkout=='1':
print('You have bought',count,'items')
print("Please pay""$" + '{:.2f}'.format(total_pricee))
print('Thank you for shopping with us!')
exit()
The problem I am facing is that when the user enters a quantity more than 5(refer to invalid_input
function), the program asks it to give a quantity less than 6.However, when I apply the add_to_cart
, it takes the original value the user put in(which is greater than 5).
[1] Water
[2] rice
[3] ice
[0] View Cart
Select option:3
How many would you like?6
Please key in a valid quantity(Between 1 to 4):5
[1] Water
[2] rice
[3] ice
[0] View Cart
Select option:0
S/n Items Quantity Unit Price Price
1. Ice 6 $2.00 $12.00
You see how it asked the user to key in a valid quantity(5) however the quantity taken by add_to_cart
is the initial value(6).I want the quantity to display from the valid quantity.Help please!Thanks!
Upvotes: 1
Views: 41
Reputation: 1057
You just need to replace the >
to >=
to avoid the error in your logic,
def invalid_input(Quantitiy):
#look the Qunatity>=5
while Quantitiy >= '5' or Quantitiy < '1':
Quantitiy = input("Please key in a valid quantity(Between 1 to 4):")
if Quantitiy < '5' and Quantitiy > '1':
New_Quan=Quantitiy
break
what happens if you give only Qunatity> 5
and input 5
is that the condition becomes false.Because 5 is not greater than 5 itself.
Apart from this mistake when you were asking user to Please key in a valid quantity(Between 1 to 4)
then you were not returning it back to your main module .Here is a modified version of your code,
#replaced cart with str1
str1="S/n\tItems\t Quantity\tUnit Price\t\tPrice"
total_pricee = 0
pricee = 0
count=1
def invalid_input(Quantitiy):
while Quantitiy >= '5' or Quantitiy < '1':
Quantitiy = input("Please key in a valid quantity(Between 1 to 4):")
if Quantitiy < '5' and Quantitiy > '1':
New_Quan=Quantitiy
#returned from here
return Quantitiy
break
while not Quantitiy.isdigit():
Quantitiy = input('Invalid input.Please enter a valid input:')
while Quantitiy.isdecimal() == False:
break
#added the return here
return Quantitiy
def add_to_cart(name, Quantity, price):
global total_pricee, pricee,count,str1
#replaced each cart statement with str1
str1+="\n"
str1+=str(count)+'.'+'\t'
str1+=str(name)+'\t\t'
str1+=str(Quantity)+'\t\t'
str1+=str(price)+'\t\t'
pricee = '{:.2f}'.format(float(Quantity) * price)
pricee
str1+=str(pricee)
total_pricee += float(pricee)
count = count +1
while True:
print('[1] Water')
print('[2] rice')
print('[3] ice')
print('[0] View Cart')
opt = input("Select option:")
if opt > '3' or opt < '0':
print("Select valid option!")
if opt == '3':
qunt = input("How many would you like?")
#getting the returned value to quant
qunt =invalid_input(qunt)
nam3 = "Ice"
add_to_cart(nam3, qunt, 2)
if opt == '1':
qunt2 = input("How many would you like?")
#getting the returned value to quant1
qunt2=invalid_input(qunt2)
nam2 = "Water"
add_to_cart(nam2, qunt2, 3)
if opt == '2':
qunt1 = input("How many would you like?")
#getting the returned value to quant1
qunt1=invalid_input(qunt1)
nam1 = "Rice"
add_to_cart(nam1, qunt1, 5)
if opt == "0":
print(str1)
print("Total price until now:", "$" + '{:.2f}'.format(total_pricee))
print('Would you like to check out?')
print('[1] Yes')
print('[2] No')
checkout=input("Please select an option:")
if checkout=='1':
print('You have bought',count,'items')
print("Please pay""$" + '{:.2f}'.format(total_pricee))
print('Thank you for shopping with us!')
exit()
Hope this helps you!
Upvotes: 1