Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I hope this question makes sense because I'm not too sure how to ask it.

But my program - in python - asks the user to input their score from 1-10 on these ice cream flavours in an array. Displays their score and then prints their highest score as their favourite flavour. Which takes the number of index and prints that flavour from the array. However, let's say if the user put Mint Choc Chip and Strawberry both as 10. The program will only print the item that is in the array first, which is min choc chip despite strawberry also being the highest score. Does anyone know a way which can make the program display all the highest scored flavours? Please keep in mind that I am new to Python so if the answer seems obvious to you, it is not for me. So please be kind and any help or suggestions will be greatly appreciated!

code:

Python
import numpy as np

flavours = ["Vanilla", "Chocolate", "Mint Choc Chip", "Rosewater", "Strawberry", "Mango", "Frutti Tutti", "Fudge Brownie", "Bubblegum", "Stracciatella"]

Ice_Cream= [0]*10

print("Please input your score on these flavours of ice cream. With 1 being the lowest and 10 the highest.\n")

for i in range(0,10): print(flavours[i]+":") Ice_Cream[i] = int(input())

print("\nResults:\n")

for i in range(0,10): print(flavours[i]+":",Ice_Cream[i])

high = np.argmax(Ice_Cream)

if high > 1: print ("\nYour favourite flavour of ice cream is", flavours[high], flavours[high])

else:

print ("\nYour favourite flavour of ice cream is", flavours[high])


What I have tried:

I have tried to add this: if high > 1: print ("\nYour favourite flavour of ice cream is", flavours[high], flavours[high])

But just prints the same flavour that appears in the array twice, so it will print: Your favourite flavour of ice cream is mint choc chip mint choc chip. And I know this doesn't make sense because if the highest score was three flavours it will only print two (of the same). I have also tried to look for other functions such as import etc. But I couldn't find one that helps. This is not necessary for the program but will make it better and more realistic. Thank you!
Posted
Updated 18-Mar-18 2:53am
v2

1 solution

Your formatting leaves much to be desired. However, you can simplify things by checking the values as you go through the initial loop, like this:

Python
flavours = ["Vanilla", "Chocolate", "Mint Choc Chip", "Rosewater", "Strawberry", "Mango", "Frutti Tutti", "Fudge Brownie", "Bubblegum", "Stracciatella"]

Ice_Cream= [0]*10

print("Please input your score on these flavours of ice cream. With 1 being the lowest and 10 the highest.\n")

high = 0
index = 0
for i in range(10):
    print(flavours[i]+":")
    Ice_Cream[i] = int(input())
    if Ice_Cream[i] > high:
        high = Ice_Cream[i]
        index = i

print("\nResults:\n")

for i in range(10):
    print(flavours[i]+":",Ice_Cream[i])

print ("\nYour favourite flavour of ice cream is", flavours[index])
 
Share this answer
 
Comments
pythontimes 18-Mar-18 11:13am    
Thank you, this works but still only displays the first item in the array if they have the same score. Thank you so much for your help and simplifying things!!
Richard MacCutchan 18-Mar-18 11:18am    
Yes, I know. You could just save the high value in the first loop, and then create a final loop that goes through the Ice_Cream array and check for that value, and print the corresponding flavour entry. That would get all entries for the value.
pythontimes 18-Mar-18 11:20am    
Okay, thank you!!
Maciej Los 18-Mar-18 12:51pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900