0

Hello friends of the internet,

I just began working in Python a few days ago and I am trying to create a starter program. All I am looking to do is create something that can take an input set of numbers (non-negative), from the user, and print the value of the highest number in the set.

I have read a few things about recursion functions that may be able to accomplish this task, but like I said, I'm a newby! If any of you professionals can show me something that can do just that I would be incredibly grateful.

def Max(list):
    if len(list) == 1:
        return list[0]
    else:
        m = Max(list[1:])
        return m if m > list[0] else list[0]

def main():
    list = eval(raw_input(" please enter a list of numbers: "))
    print("the largest number is: ", Max(list))

main()
dezz
  • 41
  • 6
  • 2
    What exactly is wrong with what you have now? – jwodder Sep 05 '17 at 19:59
  • 4
    Usually you need to iterate over the list and find the max. That's not done here. Also please, please, **PLEASE** do not get in the habit of using `eval` like that. – tadman Sep 05 '17 at 19:59
  • You don't need `Max` for one. Use the built-in function `max`. – Christian Dean Sep 05 '17 at 19:59
  • 2
    he is learning @ChristianDean, a recursive `max` is not a bad exercise – Netwave Sep 05 '17 at 20:00
  • @DanielSanchez I'm never said it was. The OP stated that they were trying to create programs for the first time, and if that's all their trying to do, using max is a good choice rather than trying to delve into recursion already. That can be learned in time. – Christian Dean Sep 05 '17 at 20:02
  • @ChristianDean, or maybe he just started with recursion, it is not up to us to judge that I think. Actually in my opinion it makes much more simpler to learn in a recursive way – Netwave Sep 05 '17 at 20:07
  • 2
    I understand your point @Daniel. However, since the OP stated "My First Ever Python Program" in the title, I thought he may not realized the functionality he's trying to implement with `Max` is already done with the built-in `max` function. If he knows this and is simply trying create `Max` for educational purposes, then that's completely fine as well. My intention was simply to let him know. – Christian Dean Sep 05 '17 at 20:26

2 Answers2

3

You have few issues in the code:

  • Don't use Python keywords like list as variable names
  • Don't use eval() when taking input from user
  • You can use built-in max() unless you want to create the function for educational purposes

So heres a better version of your code:

def main():

    my_list = raw_input(" please enter a list of numbers: ").split()
    my_list = map(int, my_list) # convert each item to int
    print("the largest number is: ", max(my_list))

main()

If you are using Python3 change raw_input() to input() and if you are using Python2 change print('item1', 'item2') to print 'item1', 'item2'

EDIT:

You can use a generator expression with max() to do that too as the following:

def main():

    my_list = raw_input(" please enter a list of numbers: ").split()
    max_num = max(int(x) for x in my_list)
    print("the largest number is: {}".format(max_num))

main()
Phil H
  • 19,928
  • 7
  • 68
  • 105
Mohd
  • 5,523
  • 7
  • 19
  • 30
  • an explanation of how `map` replaces `eval` would be beneficial. – TemporalWolf Sep 05 '17 at 20:15
  • `map` is great, but list comprehensions tend to be more explicit and, thus, more 'pythonic': `my_list = [int(element) for element in my_list]`. Otherwise, great solution here! – slightlynybbled Sep 05 '17 at 20:16
  • 1
    @slightlynybbled A generator with `max()` would be better too, I'll edit now – Mohd Sep 05 '17 at 20:17
  • This makes a more sense, thank you guys. I would like a brief explanation on why `map` replaces `eval` just to help my understanding. – dezz Sep 05 '17 at 20:20
  • `eval()` is not safe to use since it will evaluate any passed expression, you can use `map()` to perform a specific operation to your list, otherwise you can use list comprehension or a generator expression like the example I put just now – Mohd Sep 05 '17 at 20:22
  • @AidenProhaska [Check this](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice) for more detailed explanation of how `eval()` can be dangerous – Mohd Sep 05 '17 at 20:27
0

Single line answer:

print (max(list(map(int,input().split()))))

Explanation:

a=raw_input() #Take input in string.
b=a.split() #Split string by space.
c=map(int,b) # Convert each token into int.
d=max(c) #Evalute max element from list using max function.
print (d)

Edit:

print (max(map(int,input().split())))
Rohit-Pandey
  • 2,039
  • 17
  • 24