Phyton was accepted by worldwide coders unanimously due to the fact that it is very much as English words. There are not much difficult syntax and keywords which has to be learned to code Python.

Lets take the conditional element block from Python
Keywords : if , elif, else

if statements will not decide on skipping or running the complete code. They only make decisions about a code block which is indented for the if, elif and else block.

if true :


Above if the syntax for the using the if statement. if keyword follows with condition section and then the colon to show the completion of the statement.
Condition word “true” means code block will be executed
Condition word “False” means code block will be skipped

The other way to use the condition section is by assigning the condition outcome to a variable and use it in the condition statement

run = true
if run :
print(“The code block will be running”)

Likewise elif and else statements also can be used.
Coder can get inputs from the user for deciding the condition to be true or false which will be used to execute the code block based on the user inputs.

Model code to practice

import random
print(“Let’s play rock, paper, or scissors”)
player_choice = input(“\nChoose rock, paper, or scissors: “).lower()
choices = [“rock”, “paper”, “scissors”]
computer_choice = random.choice(choices)
print(f”Computer chose: {computer_choice}”)
if (player_choice == “rock” and computer_choice == “scissors”) or (player_choice == “scissors” and computer_choice == “paper”) or (player_choice == “paper” and computer_choice == “rock”):
winner = “Player”
elif player_choice == computer_choice:
winner = “Tie”
else:
winner = “Computer”
if winner == “Player”:
print(“You won”)
elif winner == “Computer”:
print(“Computer won”)
else:
print(“It’s a tie”)

Leave a comment

Trending