Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am I getting "expected expression pylance"? Whats wrong with my elseif statements?

Absolutely stuck on what I’m doing wrong here. I’ve set everything up seemingly correctly unless I’m missing a small error. Can anyone provide some guidance on what I’m doing wrong? Are my elseif statements incorrect? I’ve tried changing the elseif statements to other options and still stuck. From what I can see looking over proper documentation everything seems to be setup correctly.

candy = {
    "m&m": 1.99,
    "butterfingers": 1.25,
    "skittles": 2.99,
    "snickers": 1.3
}

inventory = [5, 10, 2, 3]


qty1 = int(input("how many M&M's ? "))
if qty1 < 1:
    print(f"Sorry invalid input")
qty1 =0
elif qty1 > inventory[0]:
    print(f"Sorry we don't have that many...giving you {inventory[0]} instead")
qty1 = inventory[0] 
    inventory[0] = 0 
else:
inventory[0] = inventory[0] - qty1 


qty2 = int(input("how many butterfingers ? "))
if qty2 < 1:
    print(f"Sorry invalid input")
qty2 =0 
elif qty2 > inventory[1]:
    print(f"Sorry we don't have that many...giving you {inventory[1]} instead")
qty2 = inventory[1] 
    inventory[1] = 0 
else:
inventory[1] = inventory[1] - qty2 



qty3 = int(input("how many skittles ? "))
if qty3 < 1:
    print(f"Sorry invalid input")
qty3 =0 
elif qty3 > inventory[2]:
    print(f"Sorry we don't have that many...giving you {inventory[2]} instead")
qty3 = inventory[2] 
    inventory[2] = 0 
else:
inventory[2] = inventory[2] - qty3 



qty4 = int(input("how many snickers ? "))
if qty4 < 1:
    print(f"Sorry invalid input")
qty4 =0 
elif qty4 > inventory[3]:
    print(f"Sorry we don't have that many...giving you {inventory[3]} instead")
qty4 = inventory[3] 
    inventory[3] = 0 
else:
inventory[3] = inventory[3] - qty4 


cost = qty1 * candy["m&m"] + qty2 * candy["butterfingers"] + qty3 * candy["skittles"] + qty4 * candy["snickers"]
print("thanks for the order.....")
print()

print(f"your cost will be ${cost:2.2f}")

print(f"our new inventory is {inventory}")

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There are two issues with your code.

Problem 1: You have statements in between if and elif blocks. To fix this change:

if qty1 < 1:
    print(f"Sorry invalid input")
qty1 = 0
elif qty1 > inventory[0]:

to:

if qty1 < 1:
    print(f"Sorry invalid input")
elif qty1 > inventory[0]:

Problem 2: You have the else block that is not indented, to fix this change:

else:
inventory[0] = inventory[0] - qty1

to:

else:
    inventory[0] = inventory[0] - qty1 

If you fix these problems in all sections of the code you should get the following code with no errors:

candy = {
    "m&m": 1.99,
    "butterfingers": 1.25,
    "skittles": 2.99,
    "snickers": 1.3
}

inventory = [5, 10, 2, 3]

qty1 = int(input("how many M&M's ? "))
if qty1 < 1:
    print(f"Sorry invalid input")
elif qty1 > inventory[0]:
    print(f"Sorry we don't have that many...giving you {inventory[0]} instead")
    qty1 = inventory[0] 
    inventory[0] = 0 
else:
    inventory[0] = inventory[0] - qty1 

qty2 = int(input("how many butterfingers ? "))
if qty2 < 1:
    print(f"Sorry invalid input")
elif qty2 > inventory[1]:
    print(f"Sorry we don't have that many...giving you {inventory[1]} instead")
    qty2 = inventory[1] 
    inventory[1] = 0 
else:
    inventory[1] = inventory[1] - qty2 

qty3 = int(input("how many skittles ? "))
if qty3 < 1:
    print(f"Sorry invalid input")
elif qty3 > inventory[2]:
    print(f"Sorry we don't have that many...giving you {inventory[2]} instead")
    qty3 = inventory[2] 
    inventory[2] = 0 
else:
    inventory[2] = inventory[2] - qty3 

qty4 = int(input("how many snickers ? "))
if qty4 < 1:
    print(f"Sorry invalid input")
elif qty4 > inventory[3]:
    print(f"Sorry we don't have that many...giving you {inventory[3]} instead")
    qty4 = inventory[3] 
    inventory[3] = 0 
else:
    inventory[3] = inventory[3] - qty4 

cost = qty1 * candy["m&m"] + qty2 * candy["butterfingers"] + qty3 * candy["skittles"] + qty4 * candy["snickers"]
print("thanks for the order.....")
print()

print(f"your cost will be ${cost:2.2f}")

print(f"our new inventory is {inventory}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading