如何从具有相同最高值的阵列中打印输入值的最高值?在Python中 [英] How do I print the highest of an inputted value from an array, which has the same highest value? In Python

查看:114
本文介绍了如何从具有相同最高值的阵列中打印输入值的最高值?在Python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我希望这个问题有道理,因为我不太确定怎么问它。



但是我的程序 - 在python中 - 要求用户在阵列中输入这些冰淇淋口味的1-10分。显示他们的分数,然后将他们的最高分打印为他们喜欢的风味。其中索引的数量和打印数组的味道。但是,假设用户将Mint Choc Chip和Strawberry都设置为10.该程序将仅打印阵列中的项目,即使草莓也是最高分,这是min choc芯片。有谁知道一种方法可以使程序显示所有最高得分的口味?请记住,我是Python的新手,所以如果你的答案看起来很明显,那就不适合我了。所以请善待,任何帮助或建议将不胜感激!



代码:



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:

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])





我尝试了什么:



我试图添加这个:如果高> 1: 打印(\ n你最喜欢的冰淇淋口味是,口味[高],口味[高])



但是只打印两次出现在阵列中的相同味道,所以它将打印出来:你最喜欢的冰淇淋口味是薄荷巧克力薄荷巧克力芯片。我知道这没有意义,因为如果最高分是三种口味,那么它只能打印两种(相同的)。我也试图寻找其他功能,如导入等。但我找不到一个有帮助。这对于该计划来说不是必需的,但会使其更好,更现实。谢谢!



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!

推荐答案

您的格式还有很多不足之处。但是,您可以通过在初始循环中检查值来简化操作,如下所示:



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:

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])


这篇关于如何从具有相同最高值的阵列中打印输入值的最高值?在Python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆