0

Just started Python programming and I am currently learning about the if, else and elif functions from an online course i bought. I did everything word for word but i keep getting errors on VSC. The course is using different software, so maybe it has something to do with the program difference. Here is the code. Thanks in advance.

print("Hello and welcome to the roller coaster!")

height = float(input("Let's start with your height! Please type in your height in m "))


if height >= 1.3:
   age = input("You are allowed to ride the roller coaster! Now, how old are you?")

if  int(input("age")) < 12:
    print("Your total is $5")
elif  int(input("age")) < 18:
    print("Your total is $7")
else:
    print("Your total is $12")
else:
    print("I'm sorry but you are too short to ride the roller coaster. Come back later.")

I tried looking it up here but couldn't find a solution. here are the error messages

[{

"severity": 8,
"message": "Expected expression",
"source": "Pylance",
"startLineNumber": 15,
"startColumn": 1,
"endLineNumber": 15,
"endColumn": 5

}]

[{ "severity": 8, "message": "Unexpected indentation", "source": "Pylance", "startLineNumber": 16, "startColumn": 1, "endLineNumber": 16, "endColumn": 5 }]

[{

"severity": 8,
"message": "Unindent not expected",
"source": "Pylance",
"startLineNumber": 16,
"startColumn": 90,
"endLineNumber": 16,
"endColumn": 90

}]

[{

"severity": 8,
"message": "Expected expression",
"source": "Pylance",
"startLineNumber": 16,
"startColumn": 90,
"endLineNumber": 16,
"endColumn": 90

}]

[{

"severity": 8,
"message": "Statements must be separated by newlines or semicolons",
"source": "Pylance",
"startLineNumber": 16,
"startColumn": 90,
"endLineNumber": 16,
"endColumn": 90

}]

2 Answers2

0

Indentation often gets many layers deep. In your case you will need two layers of indentations because you have two layers of if blocks (an if inside an if):

print("Hello and welcome to the roller coaster!")

height = float(input("Let's start with your height! Please type in your height in m "))


if height >= 1.3:
   age = input("You are allowed to ride the roller coaster! Now, how old are you?")

    if  int(input("age")) < 12:
        print("Your total is $5")
    elif  int(input("age")) < 18:
        print("Your total is $7")
    else:
        print("Your total is $12")
else:
    print("I'm sorry but you are too short")
JNevill
  • 46,980
  • 4
  • 38
  • 63
0

A couple of things to note:

  • As indicated in the comments there is an issue with the indentation of the second "if/else" block.
  • And, there is a second unnecessary request for age.

With that in mind, following is a refactored version of your program with some bits simplified.

print("Hello and welcome to the roller coaster!")

height = float(input("Let's start with your height! Please type in your height in m "))

if height >= 1.3:
    age = input("You are allowed to ride the roller coaster! Now, how old are you? ")

    if  int(age) < 12:              # This block of code needed to be indented
        print("Your total is $5")
    elif  int(age) < 18:            # Changed to just a simple value test - no need to input age a second time
        print("Your total is $7")
    else:
        print("Your total is $12")
else:
    print("I'm sorry but you are too short to ride the roller coaster. Come back later.")

Note the refinement of the age testing.

Following are a couple of tests for height and age.

craig@Vera:~/Python_Programs/Coaster$ python3 Coaster.py 
Hello and welcome to the roller coaster!
Let's start with your height! Please type in your height in m 1.2
I'm sorry but you are too short to ride the roller coaster. Come back later.
craig@Vera:~/Python_Programs/Coaster$ python3 Coaster.py 
Hello and welcome to the roller coaster!
Let's start with your height! Please type in your height in m 1.4
You are allowed to ride the roller coaster! Now, how old are you? 15
Your total is $7

The takeaway here is always be cognizant of proper indentation with your "if/else" testing as well as any structures you might use (e.g. "while loops", and so forth).

NoDakker
  • 3,390
  • 1
  • 10
  • 11