Python Forum
My first complex(ish) code .
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My first complex(ish) code .
#6
(Jun-19-2021, 12:16 PM)perfringo Wrote: If you see a pattern like this:

a =int(input("Value of a = "))
b =int(input("Value of b = "))
c =int(input("Value of c = "))
You should consider refactoring your code. There are lot of repetition and you should avoid it. This principle is called DRY (Don't Repeat Yourself). You can write it like this:

values = [int(input(f'Enter value of {value}: ')) for value in 'abc']
This is list comprehension combined with f-strings (formatted string literals). As this is very small list, it would to. If we are talking about huge number of values, then generator expression is preferred. You don't have to have separate names/variables, you can feed unpacked list/generator to your function.

Some style suggestions to write function. Don't use spaces around parentheses on function first line. Do use spaces around operators and after comma. Use four spaces for indentation. For conventions of coding in Python have a look at PEP-8 Style Guide for Python Code. So your function, from pure style perspective, could look like (keep in mind that meaningful names are helpful for users of your code and your future self):

def lineard(x, y, z):
    return y * y - 4 * x * z
You can feed unpacked argument list to your function like this:

p = lineard(*values)
You should be aware that if user enters something which is not convertible to integer the program will blow up and raise error. You could/should write validation function to avoid it.

I will not dive into if...elif...else.

This was valuable info I am thankful to you're reply .
Reply


Messages In This Thread
My first complex(ish) code . - by DUDDLEGOD - Jun-19-2021, 08:30 AM
RE: My first complex(ish) code . - by Larz60+ - Jun-19-2021, 11:17 AM
RE: My first complex(ish) code . - by DUDDLEGOD - Jun-21-2021, 07:27 AM
RE: My first complex(ish) code . - by Yoriz - Jun-19-2021, 11:51 AM
RE: My first complex(ish) code . - by perfringo - Jun-19-2021, 12:16 PM
RE: My first complex(ish) code . - by DUDDLEGOD - Jun-21-2021, 07:31 AM

Forum Jump:

User Panel Messages

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