Jump to content

Quadratic calculator - First code

Windows9

First time coding. It basically calculates the nth term for a simple quadratic sequence. Are there any ways I can make it better

number1 = float(input("Number 1"))
number2 = float(input("Number 2"))
number3 = float(input("Number 3"))
number4 = float(input("Number 4"))
number5 = float(input("Number 5"))
sequence = [number1, number2, number3, number4, number5]
print(sequence)

#Calculating difference 1
dif1num1 = number2 - number1
dif1num2 = number3 - number2
dif1num3 = number4 - number3
dif1num4 = number5 - number4

#Calculating difference 2

dif2num1 = dif1num2 - dif1num1
dif2num2 = dif1num3 - dif1num2
dif2num3 = dif1num4 - dif1num3
dif_array = [dif2num1, dif2num2, dif2num3]
print(dif_array)
#Confirming quadratic

valid1 = int(0)
valid2 = int(0)
valid3 = int(0)

if dif2num1 and dif2num2 == dif2num3:
    valid3 = valid3 + 1

if valid3 == 0:
  print("The sequence is not quadratic")

var_ns = dif2num1 / 2

one_n_squared_1 = 1
one_n_squared_2 = 4
one_n_squared_3 = 9
one_n_squared_4 = 16
one_n_squared_5 = 25

new_n_squared_1 = one_n_squared_1 * var_ns
new_n_squared_2 = one_n_squared_2 * var_ns
new_n_squared_3 = one_n_squared_3 * var_ns
new_n_squared_4 = one_n_squared_4 * var_ns
new_n_squared_5 = one_n_squared_5 * var_ns

diff1 = number1 - new_n_squared_1
diff2 = number2 - new_n_squared_2

dif_01 = dif2num1 / 2
dif_02 = str(dif_01)
dif_03 = (dif_02 + "n squared")


diff_final = diff2 - diff1
diff_final_str = str(diff_final)
print(dif_03 + "+" + diff_final_str + "n")

 

Please tag me @Windows9 so I can see your reply

Link to comment
Share on other sites

Link to post
Share on other sites

You could accept inputs in a loop and place them in a list rather than assign a distinct variable to each of them. This would also allow you to expand your program to a sequence of arbitrary length. In the same way you could generate the powers of each integer programmatically, in a list.

 

Also all the "new" variables are redundant, you could have just run the multiplication while declaring the "old" ones.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

(caveat emptor, I speak C-like languages better than Python)

10 minutes ago, Windows9 said:

if dif2num1 and dif2num2 == dif2num3

 

I suspect in this line you mean...

if dif2num1 == dif2num3 and dif2num2 == dif2num3

... otherwise you might just be checking if dif2num1 is not 0 (at least in C it works that way; Python might just be that smart).

 

-----

 

On a higher level: using a loop to run through your first array to validate the first and create the second array (so your input data could be arbitrary length), and another loop to validate the second and create the third (etc.), and then another loop to validate the third.

 

Loops are your friend, especially in work like this. It also makes fixing algorithmic mistakes much quicker because you don't need to change n-lines of code every time, just one.

Main System (Byarlant): Ryzen 7 5800X | Asus B550-Creator ProArt | EK 240mm Basic AIO | 16GB G.Skill DDR4 3200MT/s CAS-14 | XFX Speedster SWFT 210 RX 6600 | Samsung 990 PRO 2TB / Samsung 960 PRO 512GB / 4× Crucial MX500 2TB (RAID-0) | Corsair RM750X | a 10G NIC (pending) | Inateck USB 3.0 Card | Hyte Y60 Case | Dell U3415W Monitor | Keychron K4 Brown (white backlight)

 

Laptop (Narrative): Lenovo Flex 5 81X20005US | Ryzen 5 4500U | 16GB RAM (soldered) | Vega 6 Graphics | SKHynix P31 1TB NVMe SSD | Intel AX200 Wifi (all-around awesome machine)

 

Proxmox Server (Veda): Ryzen 7 3800XT | AsRock Rack X470D4U | Corsair H80i v2 | 64GB Micron DDR4 ECC 3200MT/s | 4× WD 10TB / 4× Seagate 14TB Exos / 8× WD 12TB (custom external SAS enclosure) / 2× Samsung PM963a 960GB SSD | Seasonic Prime Fanless 500W | Intel X550-T2 10G NIC | LSI 9300-16i HBA | Fractal Design Node 804 Case (side panels swapped to show off drives) | VMs: TrueNAS Scale; Ubuntu Server (PiHole/PiVPN/NGINX?); Windows 10 Pro; Ubuntu Server (Apache/MySQL)


Media Center/Video Capture (Jesta Cannon): Ryzen 5 1600X | ASRock B450M Pro4 R2.0 | Noctua NH-L12S | 16GB Crucial DDR4 3200MT/s CAS-22 | EVGA GTX750Ti SC | UMIS NVMe SSD 256GB / TEAMGROUP MS30 1TB | Corsair CX450M | Viewcast Osprey 260e Video Capture | Mellanox ConnectX-2 10G NIC | LG UH12NS30 BD-ROM | Silverstone Sugo SG-11 Case | Sony XR65A80K

 

Camera: Sony ɑ7II w/ Meike Grip | Sony SEL24240 | Samyang 35mm ƒ/2.8 | Sony SEL50F18F | Sony SEL2870 (kit lens) | PNY Elite Perfomance 512GB SDXC card

 

Network:

Spoiler
                           ┌─────────────── Office/Rack ────────────────────────────────────────────────────────────────────────────┐
Google Fiber Webpass ────── UniFi Security Gateway ─── UniFi Switch 8-60W ─┬─ UniFi Switch Flex XG ═╦═ Veda (Proxmox Virtual Switch)
(500Mbps↑/500Mbps↓)                             UniFi CloudKey Gen2 (PoE) ─┴─ Veda (IPMI)           ╠═ Veda-NAS (HW Passthrough NIC)
╔═══════════════════════════════════════════════════════════════════════════════════════════════════╩═ Narrative (Asus USB 2.5G NIC)
║ ┌────── Closet ──────┐   ┌─────────────── Bedroom ──────────────────────────────────────────────────────┐
╚═ UniFi Switch Flex XG ═╤═ UniFi Switch Flex XG ═╦═ Byarlant
   (PoE)                 │                        ╠═ Narrative (Cable Matters USB-PD 2.5G Ethernet Dongle)
                         │                        ╚═ Jesta Cannon*
                         │ ┌─────────────── Media Center ──────────────────────────────────┐
Notes:                   └─ UniFi Switch 8 ─────────┬─ UniFi Access Point nanoHD (PoE)
═══ is Multi-Gigabit                                ├─ Sony Playstation 4 
─── is Gigabit                                      ├─ Pioneer VSX-S520
* = cable passed to Bedroom from Media Center       ├─ Sony XR65A80K (Google TV)
** = cable passed from Media Center to Bedroom      └─ Work Laptop** (Startech USB-PD Dock)

Retired/Other:

Spoiler

Laptop (Rozen-Zulu): Sony VAIO VPCF13WFX | Core i7-740QM | 8GB Patriot DDR3 | GT 425M | Samsung 850EVO 250GB SSD | Blu-ray Drive | Intel 7260 Wifi (lived a good life, retired with honor)

Testbed/Old Desktop (Kshatriya): Xeon X5470 @ 4.0GHz | ZALMAN CNPS9500 | Gigabyte EP45-UD3L | 8GB Nanya DDR2 400MHz | XFX HD6870 DD | OCZ Vertex 3 Max-IOPS 120GB | Corsair CX430M | HooToo USB 3.0 PCIe Card | Osprey 230 Video Capture | NZXT H230 Case

TrueNAS Server (La Vie en Rose): Xeon E3-1241v3 | Supermicro X10SLL-F | Corsair H60 | 32GB Micron DDR3L ECC 1600MHz | 1x Kingston 16GB SSD / Crucial MX500 500GB

Link to comment
Share on other sites

Link to post
Share on other sites

I'm suspecting this is python? If so, you don't need float on the numbers or int on the valids, python will take care of that for you and convert where necessary.

 

You don't appear to be using valid1 or valid2, so those aren't necessary.

 

if dif2num1 and dif2num2 == dif2num3:

This is casting dif2num1 and dif2num2 to booleans, and with floats that means that what this is actually saying is

if (dif2num1 != 0.0) and (dif2num2 != 0.0) == (dif2num3 != 0.0):

What I suspect you want is for all three to be equal, which would instead be represented as

if dif2num1 == dif2num2 == dif2num3:

 

dif_01 = dif2num1 / 2
dif_02 = str(dif_01)
dif_03 = (dif_02 + "n squared")

Mixing variables with the same naming style but totally different data types can get confusing fast. Python also does a lot of easy conversion with strings, so you could write this whole block as

dif_03 = (dif2num1 / 2 + "n squared")

 

 

And as others have mentioned

Cats Wanting Fruit Loops | Know Your Meme

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×