I have code like this :
n = list(input().split()) #Input score in alphabet
for i in n :
if i == "E" :
print("You Have Bad Score") #if you have an E score, then you cant pass
elif len(n) < 7 :
print("Your score does not qualify") #must enter at least 8 values
else :
print("You pass")
# Output
A B C C D D D E = Your have bad score
A B B A A B B C = You Pass
A B B B A = Your score doesn't qualify
But the problem in my code is when i input A B C C D D D E the output is "You Pass" not "You have bad score". Can you help me ?
2 Answers 2
Your codes has some mistypes or errors: 1. the x should be n. Secondly, try to use more descriptive variable name to help you.
[Notes - you still have to fix/work out the multiple print statements issues...]
Try this it should help you find out.
scores = list(input().split()) #Input score in alphabet
print(scores) # you can comment this out
for s in scores:
if any(s == 'E' for s in scores):
print("You Have Bad Score") # earlier bail-out when `E` scores.
break
elif len(scores) < 7 :
print("Your score does not qualify") #must enter at least 8 values
else :
print("You pass")
# Output
# A B C C D D D E = Your have bad score
# A B B A A B B C = You Pass
#A B B B A = Your score doesn't qualify
# Sample Run - with # first list of given scores:
A B C C D D D E
['A', 'B', 'C', 'C', 'D', 'D', 'D', 'E']
You Have Bad Score # changed as the new Req. asked!
6 Comments
bad score - then you can do this: if any(s == 'E' for s in scores): print('You got bad scores!') # where you put it, depends on your design...Here, you are printing all the options.
Means, for example the imput is A B C D E, then there will be 4 statements that you pass and one says you have bad score. If you just want one output as you pass, remove the print statement in the loop. Use a boolean flag like below.
Also, no need to check if length of scores is less than 8 in the loop. Take it outside and don't continue further. I don't see it as useful inside loop according to the current context.
isFail = false
if len(scores) < 8:
print("you didn't qualify")
for i in scores:
if i == "E":
isFail= true
if isFail :
print("you have bad score")
else:
print("you pass")
xdefined in your code which you are using in your if condition.