Chapter-4: If statement
For learn how to download and install python link here: How to download and install python in window
For learn how to run and execute the code in python link here:How to write and run the code
In this blog, the format is first I write Code then Result of that code. You can directly copy the code and so your time save in written that code. Also , image of the code and result and after which it contain explanation of the code.
In this blog, the format is first I write Code then Result of that code. You can directly copy the code and so your time save in written that code. Also , image of the code and result and after which it contain explanation of the code.
Example-1: Random number
Code:
from random import randint
num = randint(1,10)
guess= eval(input('Enter your guess between 1 and 10:'))
if guess==num:
print('You got it!')
else:
print('Try again, Better luck next time')
Result:
Enter your guess between 1 and 10: 2
Try again, Better luck next time
Enter your guess between 1 and 10: 5
Try again, Better luck next time
Enter your guess between 1 and 10: 4
Try again, Better luck next time
- If you read in chapter 3 , where we explain about import random code but we take it to next level . In the previous post you learn only random number but here we ask user to choose a number and then if the guess is correct then say yes "You got it" otherwise say "Better luck next time"
- In the 1st line we call random function by write, from random import randint
- In 2nd line , first we write randint and then in bracket we give the range , Here in this line num=randint(1,10) , here 1 is the starting number and 10 is the last number.
- In the 3rd line, we take input from the user and ask user to write any number between 1 and 10.
- In the 4th line, we use if statement and use if guess==num: , here guess is input which we enter and num is the random value decided by python. If they equal, then in next line it print ('You got it') otherwise print('Try next time')
Please note that, when we use if statement then in last we use colon like in this example., if guess==num:(colon) , otherwise it show error.
Also, here you can see we use two equal (==) , otherwise it show error.
Example-2: if statement
Code:
num=eval(input('Enter your marks:'))
if num > 90:
print('Your grade is A')
if num > 80:
print('Your grade is B')
if num > 70:
print ('Your grade is C')
Result:
Enter your marks: 81
Your grade is B
Your grade is C.
Explanation:
- Here as we enter 81 then as you can see in the code there are two statement , if num > 70 and num > 80. So, 81 is true for both statement that's why it show both grade B and C. To fix this problem see the next code.
Example-3: If & and statement
Code:
num=eval(input('enter your marks:'))
if num> 90 and num < 100:
print('Your grade is A')
if num < 80 and num <90:
print('Your grade is B')
if num < 70 and num < 80:
print('Your grade is C')
Result:
enter your marks: 82
Your grade is B
Explanation: Here we use " if & and " statement both so that's why result show exact grade not like the previous example.
Always use colon(:) after the if statement otherwise code error.
Example-4: If & elif statement
Code:
grade=eval(input('Enter your score:'))
if grade>=90:
print('A')
elif grade>=80:
print('B')
elif grade>=70:
print('C')
elif grade>=60:
print('D')
else:
print('F')
Result:
Enter your score: 20
F
Again when we restart the shell
Enter your score: 50
F
Again when we restart the shell
Enter your score: 80
B
- Here you see ">=" it means greater than and equal to.
- Here in this example you see "elif" function which means 'either'.
- But to use 'elif' , first you use 'if' function.
- Same as previous example ,but in this we solve example in simple way.
Example-5: Some important term
Example-6: If & or statement
Code:
num=eval(input('Enter a number:'))
if num < 1 or num>100:
print('inValid')
else:
print('valid')
Result:
Enter a number: 50
valid
Explanation:
- Here you see "or" function.
- In the 1st line, first we take input from the user.
- In the 2nd line, we use if the input less than 1 and greater than 100 then it show invalid otherwise show valid.
No comments:
Post a Comment
If you have any doubt, let me know.