Python Forum
Super flexibility in python, examples
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Super flexibility in python, examples
#1
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}")
Reply
#2
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.
buran, Skaperen, Kakha And 1 others like this post
Reply
#3
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))
Output:
Enter words separated by spaces: Who is the max Who
buran, Kakha, Skaperen like this post
Reply
#4
(Jan-06-2021, 08:01 PM)ndc85430 Wrote: 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.

Coooool
Reply
#5
(Jan-06-2021, 08:01 PM)ndc85430 Wrote: 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.

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
Reply
#6
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'.
Skaperen likes this post
Reply
#7
(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.

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'.

I am learning and at this stage I write code very badly
Reply
#8
(Jan-07-2021, 06:42 AM)Kakha Wrote: I am learning and at this stage I write code very badly
I'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.
Reply
#9
(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 badly
I'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.

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
Reply
#10
(Jan-07-2021, 07:47 PM)Kakha Wrote:
(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.

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.

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
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.
Kakha likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems with super() Hoespilaar 2 253 Apr-10-2024, 10:10 AM
Last Post: Hoespilaar
  super() in class akbarza 1 469 Dec-19-2023, 12:55 PM
Last Post: menator01
  Understanding Python super() for classes OmegaRed94 1 1,840 Jun-09-2021, 09:02 AM
Last Post: buran
  Examples of Customer requirements ComputerAstronaut 1 1,852 Dec-08-2020, 03:22 AM
Last Post: Larz60+
  Super Urgent, work related. Gimp Python tsurubaso 5 2,972 Oct-19-2020, 08:18 AM
Last Post: tsurubaso
  print() examples leodavinci1990 3 1,961 Aug-21-2020, 03:34 PM
Last Post: snippsat
  superclass and super() grkiran2011 1 1,743 Jun-20-2020, 04:37 AM
Last Post: deanhystad
  MRO About super() catlessness 1 2,047 Jan-12-2020, 07:54 AM
Last Post: Gribouillis
  Super with Sublime Text - TypeError: super() takes at least 1 argument (0 given) Shafla 6 7,449 May-04-2019, 08:30 PM
Last Post: Shafla
  mpi4py examples/tutorials MuntyScruntfundle 1 2,708 Dec-01-2018, 10:22 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020