Now we know some functions necessary to the elements of input. Now, we will learn more functions that are even more vital to input: if, or, input(), and and.
We all know what the words "if," "or," and "and" mean, and so does Python. These functions are not terribly confusing, but you may get a few terms mixed up here and there. Now, let's talk about how to use input().
Input() lets Python know when it's the user's turn to interact with the program. But, there are also ways to let Python know what kind of things the user must type. Here are some functions that determine what kind of input the user must use: int() and str().
Int is short for integer, which means a whole number. Str is short for string, which is a sentence or statement Python takes literally (we went over strings in the first article). When used as functions for input, they are int() and str(). Also, for the heck of it, let me tell you another term used in Python: float. A float is a decimal number.
There is something that is always used after an if statement is used. Something called a block of code. Remember the second article, where we wrote that program? Do you remember the lines that had the if statement in them? You'll notice that the end of all those lines had a colon (:) at the end, and the line after had an indentation of four spaces. That is a block of code. It means that if a certain statement is true, it does whatever is in the block of code. If the statement is false, Python skips that part and goes to the next line. The other two functions, or and and, are used in the same lines as if. If or is used, it means if this statement is true or this one. If and is used, it means if the statement is true and this one. If both are used, it might mean something like if this statement is true and this one or this one and this one.
The only way to experiment with these functions and to figure out how they work is to make more programs! Do what you did to make the Multiplier program from article #2, and write this:
factor1=9
factor2=12
answer=factor1 * factor2
print("What is", factor1, "X", factor2, "?")
guess=int(input())
if guess == answer:
print("Correct!")
if guess != answer:
print("That is not correct. Better luck next time!")
Save it and run it. You should see your program say, "What is 9X12?" then you should be able to put in an answer, and if you get it right, the program should say, "Correct!" and if you get it wrong, it should say "That is not correct. Better luck next time!" The program had already figured out what 9 times 12 is, and when you typed a number, Python processed your input and checked both blocks of code to see if they were true or false. If the statement was true, the program did what was in the block. If the statement was false, the program completely skipped the block. However, in the second block of code, you can also use another function called elif, which means "else if." We only used the if statement in this program. Now, let's write a program that uses or.
print("Type in any number you want.")
num=int(input())
if num == 3 or num == 8 or num == 12:
print("That's the specific number I was looking for.")
You have to be very specific with the or function, and not speak like a normal person. If you type in the program, "if num == 3 or 8 or 12," Python won't know what == 3, 8, and 12. We still haven't used and yet, so let's write another program that involves that function.
factor1=12
factor2=12
factor3=5
factor4=15
answer1=factor1 * factor2
answer2=factor3 * factor4
print("What is", factor1, "X", factor2, "?")
guess1=int(input())
print("What is", factor3, "X", factor4, "?")
guess2=int(input())
if guess1 == answer1 and guess2 == answer2:
print("You got both correct!")
If you get 144 for the first answer and 75 for the second answer, you should see "You got both correct!" displayed in Python IDLE. If you missed one or both of them, you should see nothing displayed after both problems.
So, in this article, you learned about the three commands required for input(). With such knowledge learned, I will finally give you challenges: I challenge you to add more to the third program using the and function. Try to give you a message saying which one you got wrong.
No comments:
Post a Comment