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?

15th Nov 2021, 5:28 PM
Stefan Bartl
Stefan Bartl - avatar
3 Respostas
+ 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.
15th Nov 2021, 5:32 PM
Simon Sauter
Simon Sauter - avatar
+ 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])
16th Nov 2021, 2:58 AM
A͢J
A͢J - avatar
0
Thanks!
15th Nov 2021, 9:46 PM
Stefan Bartl
Stefan Bartl - avatar