Jump to content

Whats wrong with this Code?

Fokus

image.png.4523af5e2a75cb6462ab2d275da56dbb.png       Whats wrong with this bit of code? I think the problems are down at the print statement but I'm not sure. Please let me know your thoughts about this.  

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, coyotetracker said:

image.png.4523af5e2a75cb6462ab2d275da56dbb.png       Whats wrong with this bit of code? I think the problems are down at the print statement but I'm not sure. Please let me know your thoughts about this.  

What language is it?

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 17.2.1) | iPhone XR (iOS 17.2.1) | iPad Mini (iOS 9.3.5) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, BlueChinchillaEatingDorito said:

What language is it?

Python

Link to comment
Share on other sites

Link to post
Share on other sites

If you want to add first then divide then you need to put 'a' and 'b' in parenthesis

class MathUtils:
  @staticmethod
  def average(a,b):
    return (a+b)/2
print(MathUtils.average(2,1))

Operator Precedence:

Spoiler

pemdas.jpg.e3a71f0c87975734fcfe0460d26d452c.jpg

 

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, coyotetracker said:

image.png.4523af5e2a75cb6462ab2d275da56dbb.png       Whats wrong with this bit of code? I think the problems are down at the print statement but I'm not sure. Please let me know your thoughts about this.  

Order of operations still applies in programming as it does in math. The code will execute b / 2 before a + b.

Link to comment
Share on other sites

Link to post
Share on other sites

Isn't this the wrong sub?

i5 6600k and GTX 1070 but I play 1600-900. 1440p BABY!

Still, don't put too much faith in my buying decisions. xD 

Link to comment
Share on other sites

Link to post
Share on other sites

U forgot the "'s and get rid of the ) at the end you only need one

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, SoulWolfX said:

U forgot the "'s and get rid of the ) at the end you only need one

That's what I thought I eventually figured it out. Thanks, m8.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, coyotetracker said:

That's what I thought I eventually figured it out. Thanks, m8.

np.

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, SoulWolfX said:

U forgot the "'s and get rid of the ) at the end you only need one

You don't need quotes around any of it, since print() is fine printing numbers.

 

And the final ) is needed, since the first one closes the call to MathUtils.average().

If they only had 1 ), the code wouldn't run.

 

Like a few people have said, it was due to the brackets missing around (a + b) to ensure that the addition is done first.

CPU: 6700k GPU: Zotac RTX 2070 S RAM: 16GB 3200MHz  SSD: 2x1TB M.2  Case: DAN Case A4

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

×