+ 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")

6th Jan 2022, 11:32 PM
FeRoSs
FeRoSs - avatar
11 Antworten
+ 8
You didn't make use of the list at all. Take another look at indexing.
6th Jan 2022, 11:46 PM
Simon Sauter
Simon Sauter - avatar
+ 6
if num < 0 or num > 7: print("Wrong number") else: print(fruits[num])
6th Jan 2022, 11:45 PM
Simon Sauter
Simon Sauter - avatar
+ 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.)
7th Jan 2022, 12:38 AM
Simon Sauter
Simon Sauter - avatar
+ 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.
7th Jan 2022, 1:18 AM
Emerson Prado
Emerson Prado - avatar
+ 2
Simon Sauter Thank you for the uplifted mood
7th Jan 2022, 12:49 AM
FeRoSs
FeRoSs - avatar
+ 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
7th Jan 2022, 8:48 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
print(fruits[num] if 0<=num<len(fruits) else "Wrong number")
7th Jan 2022, 12:21 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
There are more things on sololearn, Rik, than are dreamt of in their philosophy.
8th Jan 2022, 2:09 AM
Simon Sauter
Simon Sauter - avatar
0
Print(fruits[num]) That's all simple
8th Jan 2022, 10:00 AM
Yakesh Balaji Raja.P.
Yakesh Balaji Raja.P. - avatar
0
Print (fruits[num]) That's just simple
8th Jan 2022, 7:29 PM
Saint Stanley
- 1
If num in range(1 , 7): Print(fruits[num]) Else: Print(‘Wrong Number Boi’)
8th Jan 2022, 4:10 PM
Joshua Taylor Dustin
Joshua Taylor Dustin - avatar