![]() |
Super flexibility in python, examples - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Super flexibility in python, examples (/thread-31857.html) Pages:
1
2
|
Super flexibility in python, examples - Kakha - Jan-06-2021 finds the maximum both in the text and in numbers from two related functions, it is really beautiful. in1=str(input("1. insert text: ")) in2=str(input("2. insert text: ")) in3=str(input("3. insert text: ")) def max1(a,b): if a>b: return a else: return b def max2(a,b,c): return max1(a,max1(b,c)) c = max1(in1,in2) d = max2(in1,in2,in3) print(f"max between 1 and 2= {c}") print(f"max between 2 and 3= {d}") RE: Super flexibility in python, examples - ndc85430 - Jan-06-2021 A few things: 1. The naming of your functions isn't great - what do "1" and "2" mean? 2. You shouldn't need to write your own max function anyway, since the standard library has one. 3. input already returns a string, so the calls to str on lines 1-3 are redundant.
RE: Super flexibility in python, examples - deanhystad - Jan-06-2021 Why not use "max". This code works for any number of words and ignores case words = input('Enter words separated by spaces: ').split() print(max(words, key=str.upper))
RE: Super flexibility in python, examples - Kakha - Jan-06-2021 (Jan-06-2021, 08:01 PM)ndc85430 Wrote: A few things: Coooool RE: Super flexibility in python, examples - Kakha - Jan-06-2021 (Jan-06-2021, 08:01 PM)ndc85430 Wrote: A few things: but can't work with numbers At line:1 char:3 + 4 5 6 5 4 3 + ~ Unexpected token '5' in expression or statement. At line:1 char:5 + 4 5 6 5 4 3 + ~ Unexpected token '6' in expression or statement. At line:1 char:7 + 4 5 6 5 4 3 + ~ Unexpected token '5' in expression or statement. At line:1 char:9 + 4 5 6 5 4 3 + ~ Unexpected token '4' in expression or statement. At line:1 char:11 + 4 5 6 5 4 3 + ~ Unexpected token '3' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken RE: Super flexibility in python, examples - deanhystad - Jan-06-2021 What do you mean "but it can't work for numbers"? What can't work? I am very confused by your parser error. It doesn't match any code you've provided. Your code does not work for numbers. If you type numbers when asked to "insert text" it will sort the "numbers" alphabetically instead of numerically because the "numbers" are strings. For example, the numbers 3, 22, 111 would be sorted to be in order 111, 22, 3 because '1' < '2' < '3'. RE: Super flexibility in python, examples - Kakha - Jan-07-2021 (Jan-06-2021, 09:24 PM)deanhystad Wrote: What do you mean "but it can't work for numbers"? What can't work? I am very confused by your parser error. It doesn't match any code you've provided. I am learning and at this stage I write code very badly RE: Super flexibility in python, examples - deanhystad - Jan-07-2021 (Jan-07-2021, 06:42 AM)Kakha Wrote: I am learning and at this stage I write code very badlyI've been writing Python code for about a year and I still write code very badly. I still don't understand your comment about "but can't work with numbers". What does did you mean by that? Is that a problem where you would like to receive some help? If so, please provide additional information. RE: Super flexibility in python, examples - Kakha - Jan-07-2021 (Jan-07-2021, 06:48 AM)deanhystad Wrote:(Jan-07-2021, 06:42 AM)Kakha Wrote: I am learning and at this stage I write code very badlyI've been writing Python code for about a year and I still write code very badly. Your code is small and very pretty, but you get an error when adding numbers. My code summarizes numbers and text, it would be better if they made it universal. my code shows how python uses the function in the function, it understands the parameters regardless of their number RE: Super flexibility in python, examples - deanhystad - Jan-07-2021 (Jan-07-2021, 07:47 PM)Kakha Wrote:The error is caused by "key=str.upper". If you want to sort something other than strings, or if you didn't want to ignore case you would not specify that key.(Jan-07-2021, 06:48 AM)deanhystad Wrote: I've been writing Python code for about a year and I still write code very badly. |