Get startedGet started for free

Control flow

Conditional statements work similarly in R and Python. This is a basic skeleton of if-else statements in Python:

x = 1

if x > 0:
    print('Positive')
elif x < 0:
    print('Negative')
else:
    print('Zero!')

In the United States, the Center of Disease Control defines 'binge' drinking when men consume 5 or more drinks or women consume 4 or more drinks in about 2 hours. In this exercise, you will write a bunch of conditional statements to determine the status of a person who had 5 drinks.

This exercise is part of the course

Python for R Users

View Course

Exercise instructions

  • Assign 5 to a variable: num_drinks.
  • Write an if statement that prints 'error' if num_drinks is less than 0.
  • Write an elif statement that prints 'non-binge' if num_drinks is less than or equal to 4.
  • Write an else statement that prints 'binge'.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Assign 5 to a variable
____ = ____

# if statement
____ ____ < ____:
    ____
# elif statement
____ ____ <= ____:
    ____
# else statement
____:
    ____
Edit and Run Code