Another tradition in learning how to program is writing one you don't even understand. In Python Shell, go to "File," then click on "New Window." Then write the following program in the new window:
import random
prob1=0
prob2=0
guess=0
incorrect=0
answer=0
print("""Let's see how well you know multiplication! You have to get as many right as you can. When you miss 3, you lose.""")
while incorrect < 3:
prob1=random.randint(2, 12)
prob2=random.randint(2, 12)
answer=prob1 * prob2
print("What is", prob1, "X", prob2, "?")
guess=int(input())
if guess == answer:
print("Correct!")
elif guess != answer:
print("That is not correct.")
incorrect=incorrect + 1
if incorrect == 3:
print("You missed 3. Better luck next time!")
Save the program. Name it whatever you like (I named mine "Multiplier"), but you have to put ".py" at the end, so your computer knows it is a Python program. Now go to "Run", then click "Run Module F5." The program will start running in Python Shell. You will see something like "====================RESTART===================." That tells when a program starts running. If nothing shows up, press Enter once or twice, enough times to get the program to start running. You should get what is in the print statement, then a multiplication problem, each factor a random number from 2 to 12. The answer is the product of the numbers. You should keep getting random problems until you miss 3, which will end the program. Also, for the indents, most programmers use 4 spaces, so it's best you do the same thing.
Now, there's a few things I forgot to mention in the earlier article about basic Python. You can even put words together! Try this:
print("Cat" + "Dog")
CatDog
That seems a little useless, but it can be very useful. There's a good use for everything in Python. You can even print words a certain amount of times. Here's an example:
print("Cat" * 5)
CatCatCatCatCat
Again, seems useless, right? It can be important as well. Also, you probably don't even have to use the print statement. Try typing this in Python Shell:
"Cat"
'Cat'
So, just doing that is like the print statement, except this time, there are apostrophes around "Cat."
In this article, you created a program. Try reading your program and see if you can understand it, at least a little. It will help.
No comments:
Post a Comment