Conteúdo do Curso
Python - Sport
Conditional Logic Operators
Just like in sports, where decisions are made based on the current situation, programming often requires making choices. Today, we'll explore how to use if-else statements with math comparison operators to make decisions in your code. This skill is crucial for data analytics, where you often need to compare values and execute different actions based on the results.
In Python, if-else statements allow you to execute certain blocks of code based on whether a condition is true or false. Think of it like a coach deciding the next play based on the current score. The basic structure involves checking a condition and executing code accordingly:
python
Comparison operators are used to compare two values. Here are the most common ones:
==
(equal to)!=
(not equal to)>
(greater than)<
(less than)>=
(greater than or equal to)<=
(less than or equal to)
Imagine you're analyzing a basketball game and want to determine if a team has won, lost, or if the game is a draw. You can use these operators to make that decision:
team1_score = 102 team2_score = 98 if team1_score > team2_score: print("Team 1 wins!") elif team1_score < team2_score: print("Team 2 wins!") else: print("It's a draw!")
You can also combine multiple conditions using logical operators like and
and or
to create more complex decision-making logic. For example, if you want to check if a player had an outstanding game based on points and rebounds:
points = 85 rebounds = 12 if points > 80 and rebounds > 10: print("Player had an outstanding game!")
Swipe to start coding
Your goal is to complete determine_winner
function that determines the winner of a game based on the scores of two teams.
Inputs:
team1
: A string representing the name of the first team.team1_score
: An integer representing the score of the first team.team2
: A string representing the name of the second team.team2_score
: An integer representing the score of the second team.
Steps:
-
Compare Scores: Use if-else statements to compare
team1_score
andteam2_score
. -
Determine the Winner:
- If
team1_score
is greater thanteam2_score
, return the name of theteam1
. - If
team2_score
is greater thanteam1_score
, return the name of theteam2
. - If the scores are equal, return a message indicating a draw, such as "Draw"
- If
-
Return the Result: Ensure the function returns the name of the winning team or the draw message.
Solução
Obrigado pelo seu feedback!
Conditional Logic Operators
Just like in sports, where decisions are made based on the current situation, programming often requires making choices. Today, we'll explore how to use if-else statements with math comparison operators to make decisions in your code. This skill is crucial for data analytics, where you often need to compare values and execute different actions based on the results.
In Python, if-else statements allow you to execute certain blocks of code based on whether a condition is true or false. Think of it like a coach deciding the next play based on the current score. The basic structure involves checking a condition and executing code accordingly:
python
Comparison operators are used to compare two values. Here are the most common ones:
==
(equal to)!=
(not equal to)>
(greater than)<
(less than)>=
(greater than or equal to)<=
(less than or equal to)
Imagine you're analyzing a basketball game and want to determine if a team has won, lost, or if the game is a draw. You can use these operators to make that decision:
team1_score = 102 team2_score = 98 if team1_score > team2_score: print("Team 1 wins!") elif team1_score < team2_score: print("Team 2 wins!") else: print("It's a draw!")
You can also combine multiple conditions using logical operators like and
and or
to create more complex decision-making logic. For example, if you want to check if a player had an outstanding game based on points and rebounds:
points = 85 rebounds = 12 if points > 80 and rebounds > 10: print("Player had an outstanding game!")
Swipe to start coding
Your goal is to complete determine_winner
function that determines the winner of a game based on the scores of two teams.
Inputs:
team1
: A string representing the name of the first team.team1_score
: An integer representing the score of the first team.team2
: A string representing the name of the second team.team2_score
: An integer representing the score of the second team.
Steps:
-
Compare Scores: Use if-else statements to compare
team1_score
andteam2_score
. -
Determine the Winner:
- If
team1_score
is greater thanteam2_score
, return the name of theteam1
. - If
team2_score
is greater thanteam1_score
, return the name of theteam2
. - If the scores are equal, return a message indicating a draw, such as "Draw"
- If
-
Return the Result: Ensure the function returns the name of the winning team or the draw message.
Solução
Obrigado pelo seu feedback!