简体   繁体   中英

How to make Python choose the greatest number from 5 inputs(simple)way?

The problem is: we write down a code

x = int(input("enter the number here"))

then we loop that 5 times with range and we want python to tell us which number is the greatest of all for example

1,10,100,1000

the greatest number is 1000 how can we do that?

I just do not know how to do it

Here is an easy approach:

nums = (input().split(','))
print(max(list(map(int, nums))))

As the input is taken as a string, when converting it to a list, the elements will be strings. The function map will convert each string in the list to an integer.

I don't know if it's exactly what you want but here is an input combined with a for loop in a range:

answers = []
for i in range(5):
    x = int(input("enter the number here : "))
    answers.append(x)


greatest_number = max(answers)
print("The greatest number is {}".format(greatest_number))

You can do it by splitting the input with respect to "," . After splitting the str should be turn into int using for loop. Now use max function with the new integer list to get the maximum number. so your final code should be:

x = input("enter the number here: ")
list = x.split(",")

int_list = []

for s in list:
    int_list.append(int(s))

max_num = max(int_list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM