Wednesday, February 28, 2024

 

Casting in If statement and using int & float

If statements support more than ==. They support inequality symbols as well. Remember those <, >, and = signs you used in math way back . 

# equal
5 == 5

# not equal
3 ! = 5

# greater than
5 > 3

# greater than or equal to
5 >= 3

# less than
3 < 5

# less than or equal to
3 <= 5

This is because the way input works behind the scenes is it always assumes what you are typing is text (or a string) and gets stored as a variable in " ".

Casting is where we explicitly tell the computer that what we are typing is a number and not a letter.


The code below is saying any score greater than 100,000 is a winner. Anything less, try again.

myScore = input("Your score: ")
if myScore > 100000:
  print("Winner!")
else:
  print("Try again ")



Let's add int

There are two types of numbers the computer will recognize:
  • int whole number (ex: 42)
  • float any number with a decimal (ex: 1.81)
int

To change "your score" to a number, we need to add int in front of the input and place the entire input in ()

Update your code to look like this:

myScore = int(input("Your score: "))
if myScore > 100000:
  print("Winner!")
else:
  print("Try again ")

The way the computer will read this code is by starting with what is in brackets first ("your score"). The computer thinks this is text because of the "". When we add int, we are telling the computer, "This is not text. Please convert this to a whole number." Now the variable, myScore will store the answer as a number.

Let's add float

You would do the exact same thing, except using float for a decimal number. In the code below, I want to find pi to 3 decimal places.

myPi = float(input("What is Pi to 3dp? "))
if myPi == 3.142 :
  print("Exactly!")
else:
  print("Try again ")

We are casting that input as a float which means we are expecting a decimal number. The code will not crash if we put a . and then numbers after it to signify a decimal number.




code for Generation Generator :- 

print("Generation Identifier")
userBorn = int(input("Which year were you born:"))
if userBorn >= 1883 and userBorn <=1900 :
  print("Lost Generation")
elif userBorn >= 1901 and userBorn <=1927 :
  print("Greatest Generation")
elif userBorn >= 1928 and userBorn <= 1945 :
   print("silent Generation")
elif userBorn >= 1946 and userBorn <= 1964 :
   print("Baby Boomers")
elif userBorn >= 1965 and userBorn <= 1980 :
   print("Generation X")
elif userBorn >= 1981 and userBorn <= 1996 : 
   print("Millenials")
elif userBorn >= 1997 and userBorn <= 2012 :
   print("Generation Z")
else:
   print("Generation alpha")




 

Nesting in if statement

Nesting is where we put an if statement within an if statement using the power of indenting. The second if statement within the first if statement must be indented and its print statement needs to be indented one more time.

Here is code using Nesting :- 

tvShow = input("What is your favorite tv show? ")
if tvShow == "peppa pig":
  print("Ugh, why?")
  faveCharacter = input("Who is your favorite character? ")
  if faveCharacter == "daddy pig":
    print("Right answer")
  else:
    print("Nah, Daddy Pig's the greatest")
elif tvShow == "paw patrol":
  print("Aww, sad times")
else:
  print("Yeah, that's cool and all…") 



Today's focus is using all the skills you have learned so far:
  • input and output
  • concatenation
  • if statements
  • nested if statements

print("Wholesome Positivity machine")
userName = input("Who are you?:")
if userName == "appu" or userName == "angshu" or userName == "rana":
  print("Good morning",userName)
  userWant= input("What do you want to achieve:")
  print("That is Good to know that You",userWant)
  userRating=input("On a scale of 1-10 how do you feel today:")
  if userRating == "5" or userRating =="6" or userRating =="7" or userRating =="8" or userRating =="9" or userRating =="10":
    print ("It is pretty good today for you")
    print("Hey",userName,"keep your chin up!Today you are going to Teach people to code! in the most amazing way")
  elif userRating == "1" or userRating =="2" or userRating =="3" or userRating =="4" :
    print("Boost yourself to be more energetic .. I know one day you can win the world.")
else:
  print("we dont know you")

If Statements

 

If Statements

These statements are a bit like asking a question. You are telling the computer: if something is true, then do this specific block of code. Double equals (==) is asking the computer to compare if these two things are exactly the same.

In the code below, I am asking if the variable myName is the same as the string Dravid

myName = input("What's your name?: ")
if myName == "Dravid":   

But that doesn't print anything?

To create a print statement related to the input from the if statement, you must go to the next line and indent once.

An Example as a code  :- 

myName = input("What's your name?: ")
if myName == "Dravid":
  print("Welcome Dude!")   

What is else?

If the condition is not met with the if statement, then we want the computer to do the else part instead. Likewise, if the condition is met in the if statement, then the else bit is ignored by the computer. The else statement must be the first thing unindented after the if statement and in line with it.

myName = input("What's your name?: ")
if myName == "Dravid":
 print("Welcome Dude!")
 print("You're just the baldest dude I've ever seen")
else:
 print("Who on earth are you?!")  

What the elif is this?

The elif command (which stands for 'elseif') allows you to ask 2, 3, 4 or 142 questions using the same input! This command must be in a certain place. You can have as many elif statements as you want, but they must go in between if and else and have the same indentation. The print statements in your elif command need to line up with the indent of the other print statements.

An Example as a code  :- 
print("Secure login into Rude Rows")
userName = input("Tell me your Username:")
password =  input("Put your password here:")
if userName == "Ruderows" and password== "abcd" :
   print("Hey there",userName)
elif userName == "suman"  and password== "efgh":
  print("Hey there",userName)
elif userName == "biman" and password== "ijkl":
  print("Hey there",userName)
else :
  print("Go away")

 

Concatenate more than one input variable including other print function content.

All it really means is combine text (remember, text is called a string) and variables together into single sentences!

Here is the code to concatenate .

myName = input("What's your name? ")
myLunch = input("What are you having for lunch? ")
print(myName, "is going to be chowing down on", myLunch, "very soon!")

  • It turns out print has a super-power. We can give it as many different things to print as we want. All we need to do is put a comma( ,) between every different thing in the ().



 

Taking user input

Let's take a look at the input command and how that works. Input is when the user gives information to the computer.


It's very similar to the print command, except that it'll show the message in the console then wait until the user has typed something into the console and pressed enter. 

Let's get your hands dirty with the below code :- 

myName = input("What is your name ?:  ")
myFood = input("What is your food ?:  ")
myMusic= input("what is your fav music?:")
myLocation = input("where do u live?:")

print("You are")
print(myName)
print("You are probably hungry for")
print(myFood)
print("You must heard of ")
print(myMusic)
print("Living in ")
print(myLocation)

output  /Example 

What is your name ?:  Annat
What is your food ?:  mango
what is your fav music?: Trans
where do u live?: Singapore

You are
Annat
You are probably hungry for
 mango
You must heard of 
Trans
Living in 
Singapore

PS- bold words are input words given by the user.
Here in this above code, when we are taking an input as "what is your name? : " with the input function then automatically it is looking for a name in the console and when you have give the name it will be stored in the "myName" variable. when we call the "myName" variable with another print function then it will print the answer of input function that is given by you.

What is a variable?
input asks for something, takes it, but then has nowhere to put it. We can change that with a variable which is a value that we can use to name and store data. (Remember, David's box for David's phone?)






Naming variables
You can give a variable any name you want, but you can't use spaces. 
You can use:  
  • underscores_between_words
  • camelCaseToMakeItEasierToRead

Printing a Variable

You can print your variable using print and the name you used for your variable in your input command. Remember the three variables we just created: myName , myFood, myMusic
In your code you can now print 'name' by using print(myName) or 'Food' by using print(myFood).











  2D Dictionaries Remember that dictionaries are very similar to lists, except that they store data as key:value pairs. The value is what it...