0

I want the first if ... else statement to check both a and b simultaneously. And I want an efficient way of counting no_of_good and no_of_poor in order to minimize the number of lines of code. Is it possible?

How can I print both a and b as good in if statement? And also how to calculate good and poor count?

a = 1  
b = 3  
good_count=0  
poor_count=0  
if a in range(0,6) and b in range(0,6):   
    //print and count
elif a in range(6,10) and b in range(6,10):  
    //pprint and count

This is a long method of doing the work:

a = 1  
b = 3  
good_count=0  
poor_count=0  
if a in range(0,6):  
    print("a = GOOD")
    good_count+=1`    
elif a in range(6,10):
    print("a = POOR")
    poor_count+=1
if b in range(0,6):    
    print("b = GOOD") 
    good_count+=1 
elif b in range(6,10):   
    print("b = POOR ")  
    poor_count+=1
print("no_of_good=",good_count," ","no_of_poor=",poor_count)

Actual output:

a = GOOD  
b = GOOD  
no_of_good= 2   no_of_poor= 0  

Expected output should also be same but by using any different method

3
  • 1
    Does your shorter method not work? What is the issue you are having? Commented Apr 27, 2019 at 12:16
  • "efficient" and "minimize the number of lines of code" are not necessarily the same thing - one-liners are often inefficient. Commented Apr 27, 2019 at 12:21
  • If you don't care about the name of the variables, you can just use a list, like my answer below! Commented Apr 27, 2019 at 14:12

2 Answers 2

1

If you don't want to repeat your if statements then you could look into having a datatstructure to hold your values and iterate over all the values.

I also changed the condition to use integer comparisson instead of a range; it seames cleaner in my opinion.

Here is an example using a dict to hold the data:

data = {
    'a': 1,
    'b': 3,
    'c': 5,
    'd': 6,
}

good_count = 0
poor_count = 0
for k, v in data.items():
    if 0 <= v <= 5:
        good_count += 1
        result = 'GOOD'
    else:
        poor_count += 1
        result = 'POOR'
    print('{} = {}'.format(k, result))

print('no_of_good = {}'.format(good_count))
print('no_of_poor = {}'.format(poor_count))

And this is the output:

a = GOOD
b = GOOD
c = GOOD
d = POOR
no_of_good = 3
no_of_poor = 1

Does that help you?

Sign up to request clarification or add additional context in comments.

2 Comments

yeah it helped. i want to ask if there is a variable already holding a value e.g. avg_sen_len = (word_count/sen_count). and the value for this variable constantly changes as the text changes. how can we add it into the dict.
You could update the dict like this: data['avg_sen_len'] = word_count / sen_count
0

If you don't care about the variables, a,b,c,d etc, you can just use a list, and use the index of the lis t to print GOOD or POOR

li = [1,3,5,6]

good_count = 0
poor_count = 0

#Iterate through the list
for idx, item in enumerate(li):

    #Check the condition on the item and print accordingly
    if 0 <= item <= 5:
        good_count+=1
        print('index {} = GOOD'.format(idx))
    elif 6<= item <= 10:
        poor_count+=1
        print('index {} = POOR'.format(idx))

#Print final good and poor count
print('no_of_good = {}'.format(good_count))
print('no_of_poor = {}'.format(poor_count))

The output will be as below:

index 0 = GOOD
index 1 = GOOD
index 2 = GOOD
index 3 = POOR
no_of_good = 3
no_of_poor = 1

2 Comments

here the list contain variable values li={1,3,5,6} but i don't care about the "variable values" NOT "variables". because the values are changing each time you run the code SO i can only specify the variables a,b,c,d in the list. Does it follow the same procedure? also i don't want to print index 0 = good. instead a OR b OR c OR d = GOOD (what ever variable name at that index)
Yes, change the list when you want to change the values, also you will run out of variable names once you list grows bigger, hence it is better to have a dynamically generated variable name, does a name like var_0 or var_1 work for you? @Mis_Sid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.