Ошибка expression expected python

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 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 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 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 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 $") print(f"our new inventory is ") 

>Solution :

There are two issues with your code.

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

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

else: inventory[0] = inventory[0] - qty1 
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 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 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 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 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 $") print(f"our new inventory is ") 

Источник

Читайте также:  Node js html template

Why do I keep getting ‘Expected Expression Pylance Error’?

keyboard, each out of 10 marks and use this data to determine the individual’s grade. Grade bands: A – 80 to 100 B – 70 to 79 C – 60 to 69 D – 50 to 59 F – below 50 I am a beginner to python and would like some advice on how to fix the errors in my code this is what I have so far

print ("Insert first grade") g1=input("Enter input") print ("Insert second grade") g2=input("Enter input") print ("Insert third grade") g3=input("Enter input") print ("Insert fourth grade") g4=input("Enter input") print ("Insert fifth grade") g5=input("Enter input") Sum = ([g1,g2,g3,g4,g5]) Total = 50 def Percentage(Sum, Total): return 100*Sum/Total if (Percentage >= 80) and (Percentage =70 and Percentage = 60 and Percentage = 50 and Percentage  

Hi and welcome to SO. If you included that code as text in your post (and you can edit it now to do so) then it would be much easier to help you as we could cut and paste and work with it.

2 Answers 2

Let's start with this line:

That just creates a list of the grades; it doesn't add them up. If you want to add them together, you can use this:

As you can see from the above, Python has a built-in function named sum already, which means it's a bad name to use for your own variable. Letter case matters to Python, so Sum is distinct and technically fine, but it's actually considered good style to stick to lowercase for regular variables and functions, and only use uppercase for constant values and class names.

Indentation also matters, so this is an error:

def Percentage(Sum, Total): return 100*Sum/Total 

The return is the body of the function you're defining, so it needs to be indented further than the def :

def Percentage(Sum, Total): return 100*Sum/Total 

And that creates a function that you need to call; you aren't calling it, but treating it as a number, which won't work.

Why is Total 50 when there are only 5 grades?

For that matter, why are you hard-coding the number of grades? You should just use a loop to read in as many as the user wants to input. Something like this, perhaps:

print ("Enter a negative number to end grade input") grade = 0 count = 0 total = 0 while grade >= 0: grade = int(input("Enter next grade: ")) if grade >= 0: total += grade count += 1 

And then you can use your percentage function to calculate the rest (note that I used "total" for what you called "Sum", and "count" for what you called "Total").

Источник

Why do I have "if" expression expected error? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

from random import * temp =Randint( 0,70 ) print(temp) if : temp = 69 print("nice") 

3 Answers 3

  1. randint is a function so it's first letter should be small case
  2. The for loop syntax was wrong
  3. And we use == for checking equality and = for assignment

You can try the following code below

from random import randint temp = randint( 0,70 ) print(temp) if temp == 69: print("nice") 

Your if statement isn't quite right. The colon needs to go at the end of the line, and in an if statement you need a double equals sign ( == , which compares two elements) rather than a single equals sign (which assigns a variable). Additionally, you need to indent (put a tab before) lines that are part of a block. You can read more about Python if statements here. The fixed if statement should look like this:

Lastly, since Python names are case-sensitive, randint must be in all lowercase.

A quick thing to remember is that “==“ means two things are equal and “!=“ means if something is not equal.

Источник

PyCharm raising Unresolved reference + expression expected for mypy ignore based on error code

I believe PyCharm is interpreting my type: ignore[code] comments as a type comment, and reporting ignore as an unresolved reference. Unresolved reference on type comment Also, PyCharm expects an expression within the brackets. Expression expected within type ignore comment mypy error I'm trying to suppress:

pylint_ignore.py:8: error: Skipping analyzing 'pylint.utils': found module but no type hints or library stubs [import] 

And yes, I know I can just say type: ignore , and not include a code, or specify to ignore this particular import in a config file. However, I would like to specify the error codes, because I think it's a good feature. How can I get PyCharm not to complain about this? Research This answer to How do I stop pyCharm from complaining about underscore strings? Helped me realize under Preferences --> Editor --> Inspections --> Python --> Unresolved references , I can add a fully qualified symbol name to be ignored. I believe this is officially documented here. I tried adding *.ignore.* (since I don't want to have to build up a per-module ignore list), but this didn't work. If this is the right approach, can you help me figure out the right syntax? Versions

mypy==0.770 python==3.6.5 PyCharm PE 2020.1 

Источник

Оцените статью