I'm new to coding and I want to understand nested IF statements. I don't know what I'm doing wrong, the # total amount is what I am struggling with since I am not getting an answer from PyCharm. Much appreciated for those who could help me.
# Dogfood bags in 3 pack sizes and their cost
l = 100
m = 70
s = 40
# Basic questions
size = input("Bag size 20kg(L), 15kg(M), 5kg(S): ")
type = input("For adult dog or puppy dog (A/P): ")
if size.lower() in "l":
q = input("How many bags of Large size bags? ")
elif size.lower() in "m":
q = input("How many bags of Medium size bags? ")
elif size.lower() in "s":
q = input("How many bags of Small size bags? ")
print("*************")
# Finalize output
if type.lower() in "a":
print(f"Food Type: Adult")
elif type.lower() in "p":
print(f"Food Type: Puppy")
if size.lower() in "l":
print(f"Bag size: Large")
elif size.lower() in "m":
print(f"Bag size: Medium")
elif size.lower() in "s":
print(f"Bag size: Small")
print(f"Number of bags: {q}")
print()
# Total amount cost
if size.lower() == "l":
if type.lower() == "a":
print(f"Total amount with delivery ${q * l}")
elif type.lower() == "p":
print(f"Total amount with delivery ${int(q * l) - 5}")
elif size.lower() == "m":
if type.lower() == "a":
print(f"Total amount with delivery ${q * l}")
elif type.lower() == "p":
print(f"Total amount with delivery ${int(q * l) - 5}")
elif size.lower() == "s":
if type.lower() == "a":
print(f"Total amount with delivery ${q * l}")
elif type.lower() == "p":
print(f"Total amount with delivery ${int(q * l) - 5}")
CodePudding user response:
Hey it might help people answer your question if you could be more specific with the trouble you are running into. I ran your code and figured out pretty quickly what the problem is. Your IF statements all seemed to work as intended, I found that the only thing that seemed to be wrong with the code is that the total amounts would be way off what I would imagine they should be. In python when you multiply a string by an integer Ex:
> value = "1" * 5
The output is the string repeated the integer amount of times, the in the case above value
would be equal to "11111"
. The problem with your program is that the input()
function only returns strings, even if all the characters are numbers. In your case, the q
variable is a string all the way until the end of the code. A simple fix to this would be using the int()
function, casting the variable to be type integer
instead of string
.
size = "Small"
q = input(f"How many bags of {size} size bags? ")
q = int(q)
Now if you were to do that same thing as done above with the value
variable, the output would be:
> size = "Small"
> q = input(f"How many bags of {size} size bags? ")
How many bags of Small size bags? 2
> q = int(q)
> print(q * 5)
10
Also something to keep in mind is that using the variable name type
is very bad practice as it is also the name of a function that tells you the data type of a variable.
CodePudding user response:
Instead of solving the bug for you, I'll try and cover a valuable lesson for life.
Instead of doing multiple conditions to change a single word (Large, Medium, Small) in a question, just store the right word once in a variable and reuse it unconditionally. The code will become much much simpler:
sizes = {
'l': ('Large', 100)
'm': ('Medium', 70)
's': ('Small', 40)
}
size = input("Bag size 20kg(L), 15kg(M), 5kg(S): ")
size = size.lower()
if size not in sizes.keys():
print('Wrong bag size')
return
size_description, size_price = sizes[size]
print(f"Bag size: {size_description}")
types = {
'a': 'Adult',
'p': 'Puppy'
}
type = input("For adult dog or puppy dog (A/P): ")
type = type.lower()
if size not in types.keys():
print('Wrong type')
return
type_description = types[size]
print(f"Food type: {type_description}")
quantity = input(f"How many bags of {size_description} size? ")
quantity = int(quantity)
print(f"Number of bags: {quantity}")
total_price = quantity * size_price
if type == 'a':
total_price -= 5
Now, let's discuss the changes:
- You never check for the same condition twice, which makes the code much much simpler to understand and maintain
- I added some checks, because your code would fail if the users specifies something other than
l/m/s
ora/p
- Instead of repeating conditions over and over again, we use a dictionary to map user inputs to expected values without using conditions
- Rearranged prints to be next to the value they depend on. That's a styling decision, but I would advise on taking
- As some1and2 correctly pointed out, you have to cast
quantity
to get it to work correctly
Hope that helps.