1

Im trying to take in user input to get a list of numbers and then use a for loop to grab the largest value. For what I have now I can use 8237483294 but It will list each integer as its own independent value and will have its own place in the list so it would be [8,2,3,7,4,8,3,2,9,4] Which was an A+ for what I wanted. But now I want to take it to another place and take multi digit values such as [23,44332,32523,243,22,] my code is

users_number = input("list number")
numbers = users_number
max = numbers[0]
for number in numbers:
    if number > max:
        max = number
print(max)
  • Would it work if the user input numbers separated by some sort of delimiter (like a comma or space)? – blackbrandt Jan 19 '21 at 00:58
  • There are many solutions to what you want but I'd suggest you take a look at [String split()](https://www.w3schools.com/python/ref_string_split.asp) – Alexander Freyr Jan 19 '21 at 00:58
  • Related: [Get a list of numbers as input from the user](https://stackoverflow.com/q/4663306/4518341), [Taking multiple inputs from user in python](https://stackoverflow.com/q/7378091/4518341) – wjandrea Jan 19 '21 at 01:30

3 Answers3

3

Use string.split().

#Get user input 
# (no need for the two variables you used in your example)
numbers = input("List numbers separated by spaces").split()
#Convert to list of ints
numbers = [int(num) for num in numbers]

#Solution 1: use max()
print("Max value:", max(numbers))

#Solution 2: Iterate through list
max_val = numbers[0]
for num in numbers:
    if num > max_val:
        max_val = num
print(max_val)

On an additional note, I presented 2 possible methods for finding the max value. max() is a built-in that finds the maximum value in the list. There is also the iterating method, which is what you do in your code.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
blackbrandt
  • 2,010
  • 1
  • 15
  • 32
  • Word, I havent learned .split() but I see you specifically said add spaces to the values inputed then I read on python website that .split defaults to white space. So the spaces would only work with the .split? And thats what will serpeate the integers form 123434 to say 12, 3, 434? – Andrew Harmsworth Jan 19 '21 at 05:47
  • If you check the documentation, you can add an argument to the `.split()`. So you could say `numbers.split("#")` or `numbers.split('~')` or `numbers.split('asdf')` and it would split the string on '#', '~', and 'asdf' respectively. – blackbrandt Jan 19 '21 at 14:51
  • @AndrewHarmsworth if my answer worked for you, please consider hitting the check mark next to my answer to accept it. – blackbrandt Jan 19 '21 at 20:07
  • It says I can like it but it will not be added to account – Andrew Harmsworth Jan 20 '21 at 05:47
  • You can upvote my answer (which I believe you already did) but there is also a check mark beneath the downvote button. Check that, and it will mark my answer as accepted. [StackOverflow Explanation](https://stackoverflow.com/help/someone-answers) – blackbrandt Jan 20 '21 at 14:31
0

Here is a more user-friendly approach that asks for the numbers one by one from the user. The advantage is that you don't have to rely on your users to enter the correct syntax for a list.

numbers = []
while True:
    number = input("Enter a number from your list. When finished with list, enter 'done': ")
    if number == "done":
        break
    else:
        numbers.append(int(number))
max_num = max(numbers)
print(max_num)
pakpe
  • 5,391
  • 2
  • 8
  • 23
  • 1
    Thanks a lot I like it but it say I can like but it wont count towards the vote – Andrew Harmsworth Jan 20 '21 at 05:48
  • I believe you need 15 reputation points to upvote an answer and have it count. But you should be able to accept the answer. Try accepting my answer if you find it helpful. – pakpe Jan 20 '21 at 09:16
0

Simple and elegant one liner:

max(map(int, input("Enter a list: ").split()))
funnydman
  • 9,083
  • 4
  • 40
  • 55