Python Forum
My first complex(ish) code .
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My first complex(ish) code .
#1
My first code which is complex(ish).It is for quadratic equations. it may be a bit janky or bigger idk.It is made without any totorials so
thats why I want feed back .Started learning in the begining of covid ( not much experince ) . I dont know how to attach files so hope pasting the code is ok ?. And I am using replit.com not a code thing (idk what it is called) on my computer.
def lineard (x,y,z) :
  return y*y-4*x*z 

a =int(input("Value of a = "))
b =int(input("Value of b = "))
c =int(input("Value of c = "))
p =lineard (a,b,c)
print(p)

import math

if p > 0 :
 print("real and different roots")
 print((-b+math.sqrt(p))/2 * a) 
 print((-b-math.sqrt(p))/2 * a) 
elif p == 0 : 
  print("real and same roots")
  print(-b/2*a)
else:
 print("roots are not real ")
  
if a == 0:
  print("give valid numbers")
Smile
Yoriz write Jun-19-2021, 08:49 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
imports should generally be at the top of file, so you should move line 10.

There are exceptions, generally when the item being imported is generated by the script being run (or by one of the other imported modules) in which case, importlib is used to dynamically import package after being generated.
Reply
#3
Indents should be 4 spaces, there is a combination of 1 and 2 spaces.
Reply
#4
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Jun-19-2021, 11:17 AM)Larz60+ Wrote: imports should generally be at the top of file, so you should move line 10.

There are exceptions, generally when the item being imported is generated by the script being run (or by one of the other imported modules) in which case, importlib is used to dynamically import package after being generated.
Thanks alot for the comment i will fix the code
Reply
#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


Forum Jump:

User Panel Messages

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