0
Why is that so?
Hi. Why is if num == 0 or num == 1 or num == 2 or num == 3 or num == 4 or num == 5 or num == 6 or num == 7: print(fruits[num]) else: print("Wrong number") working fine, of course - but looks stupid and if num < 0 or num > 7: print(fruits[num]) else: print("Wrong number") Is not working? Anyone have a glue?
3 Réponses
+ 6
1. There is an indentation missing.
2. The condition in the if clauses of the two codes are not equivalent. They're actually exact opposites.
+ 1
Stefan Bartl
I have a glue.
If num > 7 or num < 0 then how index can be exist in list?
Read this: If n< 0 or n>7 (the index of last fruit ), the program outputs "Wrong number".
You have printed opposite value. It should be:
if num < 0 or num > 7:
print ("Wrong number")
else:
print (fruits [num])
0
Thanks!