DogYears.swift code not executing , need help with the swift code

I am trying to Write a Swift program in DogYears.swift to calculate the age, in human years, of any dog older than 2.

1 Like

Hi there, welcome to the forums!

The errors you’re getting back from the terminal are pretty clear about where your mistakes are. (I’ve not done the Swift course myself, so my response is based off a cursory read of the relevant parts in the Swift Reference Manual.)

Error 1

DogYears.swift:3:13: error: expected '{' after 'if' condition
if dogAge<=2:
            ^

This is an error because the correct syntax for an if/else branching structure in Swift is:

if (condition) {
  statements
}

The error message is quite clearly telling you that it is expecting the curly braces { }, which are not present. :slight_smile:

Error 2

DogYears.swift:4:11: error: '=' must have consistent whitespace on both sides
  humanAge= 10.5 * dogAge
          ^

Here, Swift is telling you that it expects the assignment operator to have equal whitespace either side.

(Getting technical: Swift uses the whitespace to determine whether the operator is a pre/postfix unary operator, or a binary operator. Assignment is a binary operation - it involves two variables/literals - so the operator should have the same whitespace either side. You can write either a + b or a+b and Swift will consider it valid.)

Error 3

DogYears.swift:7:11: error: '=' must have consistent whitespace on both sides
  humanAge= (10.5 * 2) + (dogAge-2) * 4
          ^

This is the same situation as Error 2.

Once you’ve fixed those errors, you’ll run into some more because there are other issues with your program.

None of these errors were particularly obscure. You don’t necessarily immediately need to know about / understand what I mean by pre/post fix unary operators etc… but you should be able to comprehend this error:

DogYears.swift:4:11: error: '=' must have consistent whitespace on both sides
  humanAge= 10.5 * dogAge
          ^

which is telling you a) there’s an issue with the whitespace and also b) precisely where it’s wrong, by pointing to the offending operator with the caret (^). :slight_smile:

4 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.