noel
noel

Reputation: 11

how to make this IF statement work

fgrades=["90","70","63","81","49"]   
a=0
b=0    
c=0    
d=0    
f=0    
grades=raw_input("enter your final grade: ")    
fgrade+=(grades,)

for i in range(len(fgrades)):
   if fgrades[i]>="90":
      a+=1        
   elif fgrades[i]>="80":
      b+=1        
   elif fgrades[i]>="70":
       c+=1        
   elif fgrades[i]>="60":
       d+=1        
   else:
       f+=1

print a,"\n",b,"\n",c,"\n",d,"\n",f

After I put all the things in all the numbers turn in to F. This happens even when you put 96 in as input.

Upvotes: 0

Views: 70

Answers (3)

biobirdman
biobirdman

Reputation: 4130

here are some suggestion that you might consider making to your code.

Firstly you can change your for loop statement from

for i in range(len(fgrades)):
    ...

to

for i in fgrades:
    ....

and so subsequently, you will just need to do comparision to value i instead of fgrades[i]

So your code will be

fgrades=[]
a=0
b=0
c=0
d=0
f=0

grades=raw_input("enter your final grade: ")

# Clean up grades, and convert to an integer
grades = int(grades.strip().replace(" ","")

fgrade.append(grades)

for i in fgrade:

   if i>=90:
      a+=1        
   elif i >=80:
      b+=1        
   elif i >=70:
       c+=1        
   elif i >=60:
       d+=1        
   else:
       f+=1

print a,"\n",b,"\n",c,"\n",d,"\n",f

Upvotes: 1

ronrest
ronrest

Reputation: 1222

A problem i can see here is that you are trying to perform inequalities on strings, rather than numbers. Try typecasting your input values into integers.

If you store the values in fgrades as integers, eg [90, 70, 60, 88, 96] then this should work:

fgrades=[90, 70, 60, 88, 96]

a=0

b=0

c=0

d=0

f=0

grades=raw_input("enter your final grade: ")

# Clean up grades, and convert to an integer
grades = int(grades.strip().replace(" ","")

fgrades.append(grades)

for i in range(len(fgrades)):

   if fgrades[i]>=90:
      a+=1        
   elif fgrades[i]>=80:
      b+=1        
   elif fgrades[i]>=70:
       c+=1        
   elif fgrades[i]>=60:
       d+=1        
   else:
       f+=1

print a,"\n",b,"\n",c,"\n",d,"\n",f

Otherwise, if you for some reason require the grades to be stored as strings, eg ["90","70","63","81","49"], then the following should work for you:

fgrades= ["90","70","63","81","49"] 

a=0

b=0

c=0

d=0

f=0

grades=raw_input("enter your final grade: ")

# Clean up grades
grades = grades.strip().replace(" ","")

fgrades.append(grades)

for i in range(len(fgrades)):

   if int(fgrades[i])>=90:
      a+=1        
   elif int(fgrades[i])>=80:
      b+=1        
   elif int(fgrades[i])>=70:
       c+=1        
   elif int(fgrades[i])>=60:
       d+=1        
   else:
       f+=1

print a,"\n",b,"\n",c,"\n",d,"\n",f

Upvotes: 2

Ian Price
Ian Price

Reputation: 7626

First off, you're trying to do numerical greater than or equal to operations on strings. These must be converted to a number data type (int, float, decimal) if you are comparing numbers.

Second, Andy's comment is corrent as well. fgrade doesn't exist as a variable/object yet.

fgrades=[]   
a=0
b=0    
c=0    
d=0    
f=0    
grades=int(raw_input("enter your final grade: "))
fgrades += (grades,)

for i in range(len(fgrades)):
   if fgrades[i]>=90:
      a+=1        
   elif fgrades[i]>=80:
      b+=1        
   elif fgrades[i]>=70:
       c+=1        
   elif fgrades[i]>=60:
       d+=1        
   else:
       f+=1

print a,"\n",b,"\n",c,"\n",d,"\n",f

Upvotes: 0

Related Questions