+ 1
Is it possible to simplify this somehow?
fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) if num < 0 or num > 7: print ("Wrong number") if num == 0: print ("apple") if num == 1: print ("cherry") if num == 2: print ("banana") if num == 3: print ("kiwi") if num == 4: print ("lemon") if num == 5: print ("pear") if num == 6: print ("peach") if num == 7: print ("avocado")
11 Answers
+ 8
You didn't make use of the list at all. Take another look at indexing.
+ 6
if num < 0 or num > 7:
print("Wrong number")
else:
print(fruits[num])
+ 5
I feel a presence, a lost soul, a disembodied spirit. Speak to me spirit, who are you? I hear a voice, faint as though it is coming from a deep well. The spirit's name is "Rik", "Rik Wittkopp". Speak to me Rik -- what message do you have for us? I hear the voice, distant and hoarse. It says:
"
print(fruits[num] if 0 <= num < len(fruits) else "Wrong number")
"
(Rik is having technical difficulties. For some reason only people following him can see his posts and only in their feed.)
+ 5
Simon Sauter Though your solution is great - quite pythonic, indeed - and the sarcasm is a Good Thing in Python, I just recommend not giving finished code. Give conceptual hints instead. This allows posters to think it through and learn for real.
+ 2
Simon Sauter Thank you for the uplifted mood
+ 2
Simon Sauter
đ€Łđđ
I love the responses to your comment, now you know how a psychic feels when no-one believes him.
Thanks for passing on my post
+ 1
print(fruits[num] if 0<=num<len(fruits) else "Wrong number")
+ 1
There are more things on sololearn, Rik, than are dreamt of in their philosophy.
0
Print(fruits[num])
That's all simple
0
Print (fruits[num])
That's just simple
- 1
If num in range(1 , 7):
Print(fruits[num])
Else:
Print(âWrong Number Boiâ)