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.
Este exercício faz parte do curso
Python for R Users
Instruções do exercício
- Assign
5to a variable:num_drinks. - Write an
ifstatement that prints'error'ifnum_drinksis less than 0. - Write an
elifstatement that prints'non-binge'ifnum_drinksis less than or equal to 4. - Write an
elsestatement that prints'binge'.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Assign 5 to a variable
____ = ____
# if statement
____ ____ < ____:
____
# elif statement
____ ____ <= ____:
____
# else statement
____:
____