Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Learning Python 3 with the Linkbot/Decisions

From Wikibooks, open books for an open world
<Learning Python 3 with the Linkbot

If statement

[edit |edit source]

As always I believe I should start each chapter with a warm-up typing exercise, so here is a short program to compute the absolute value of an integer:

n=int(input("Number? "))ifn<0:print("The absolute value of",n,"is",-n)else:print("The absolute value of",n,"is",n)

Here is the output from the two times that I ran this program:

Number?-34The absolute value of -34 is 34
Number?1The absolute value of 1 is 1

So what does the computer do when it sees this piece of code? First it prompts the user for a number with the statement "n = int(input("Number? "))". Next it reads the line "if n < 0:". Ifn is less than zero Python runs the line "print("The absolute value of", n, "is", -n)". Otherwise it runs the line "print("The absolute value of", n, "is", n)".

More formally Python looks at whether theexpressionn < 0 is true or false. Anif statement is followed by an indentedblock of statements that are run when the expression is true. Optionally after theif statement is anelse statement and another indentedblock of statements. This second block of statements is run if the expression is false.

There are a number of different tests that an expression can have. Here is a table of all of them:

operatorfunction
<less than
<=less than or equal to
>greater than
>=greater than or equal to
==equal
!=not equal

Another feature of the if command is the elif statement. It stands for else if and means if the original if statement is false but the elif part is true, then do theelif part. And if neither theif orelif expressions are true, then do what's in theelse block. Here's an example:

a=0whilea<10:a=a+1ifa>5:print(a,">",5)elifa<=3:print(a,"<=",3)else:print("Neither test was true")

and the output:

1 <= 32 <= 33 <= 3Neither test was trueNeither test was true6 > 57 > 58 > 59 > 510 > 5

Notice how theelif a <= 3 is only tested when theif statement fails to be true. There can be more than oneelif expression, allowing multiple tests to be done in a singleif statement.

Moving a Linkbot's Motors

[edit |edit source]

In the previous chapter,Who Goes There?, we saw that you can move a linkbot's motor with themoveJoint() function.It is typically used like:

myLinkbot.moveJoint(1,180)

where the first argument is the motor you want to move and the second argument is the angle in degrees you want to move it. The above code will move motor "1" in the positive direction by 180 degrees.

What if you want to move more than one motor at a time? The linkbot also has amove() function that does just that. It looks something like this:

myLinkbot.move(90,180,270)

Themove() function expects three numbers inside the parentheses. The first number is the angle to move motor 1, the second number is the angle to move motor 2, and the third number is the angle to move motor 3. If your Linkbot only has two motors, the valuefor the face that does not have a motor is ignored. For example, if I have a Linkbot-I and I ran the above sample code, the "180" is ignored because motor 2 is not a movable motor. Instead, the Linkbot will move motor 1 forward 90 degrees and motor 3 forward 270 degrees.

When themove() function is executed, the Linkbot will move any/all of its motors simultaneously.

command_linkbot.py

[edit |edit source]

In this example, we'd like to create a program that we can use to move our Linkbot around. This demo will use the pythonbreak command. In the previous chapter, we saw that Python can get stuck in "infinite loops". Thebreak command can be used to exit loops even if the loop condition is still true.

Lets start with just moving the Linkbot forward and backward first.To run this demo, you'll need a Linkbot-I and two wheels. A caster is also recommended but optional.

importbarobodongle=barobo.Dongle()dongle.connect()myLinkbot=dongle.getLinkbot('abcd')# Replace abcd with your Linkbot's serial IDwhileTrue:# This is an infinite loop. Get out by using the "break" statementcommand=input('Please enter a robot command or "quit" to exit the program: ')ifcommand=='forward':# Rotate the wheels 180 degrees to move the robot forwardmyLinkbot.move(180,0,-180)# Motor 3 has to spin in the negative direction to move the robot forwardelifcommand=='backward':myLinkbot.move(-180,0,180)elifcommand=='quit':breakelse:print('I\'m sorry. I don\'t understand that command.')print('Goodbye!')

A Note on Strings

You might have noticed that towards the end of the program, there is a line that readsprint('I\'m sorry. I don\'t understand that command.') with funny backslashes in the code. When you run the program though, it doesn't print those backslashes. These backslashes actually serve an important purpose.

When Python encounters either a single quote ' or double quote " , it knows that what follows will be a string that is terminated by the same type of quotation mark that started it. But, what if we want to include some quotation marks as part of the string? Consider the following piece of code:

mystring="Hello there. My dog says "woof!" when he's bored."

When Python sees the first Quotation mark at"Hello th..., it says "Ok, this is going to be a string and as soon as I see another quotation mark, that will be the end of the string". Soon, Python sees another quotation mark at...says "woof! and thinks "I've found the end of the string!", but this is not the end of our string! We wanted Python to go all the way to the end at...bored." . How do we tell Python that the quotation marks around"woof!" are special, and that our string doesn't actually end there?

The answer is backslashes. If there is a backslash preceding a quotation mark, that is a special indicator to Python to include that quotation mark as part of the string, rather than terminating the string. Therefore, the fixed code should be:

mystring="Hello there. My dog says\"woof!\" when he's bored."

In this example, the single quote inhe's is automatically included as part of the string because the string started with a double-quotation mark, and thus Python is looking for a double-quotation mark to terminate the string.

Sample runs

Sample runs:

Please enter a robot command or 'quit' to exit the program:forwardPlease enter a robot command or 'quit' to exit the program:backwardPlease enter a robot command or 'quit' to exit the program:aoeuI'm sorry. I don't understand that command.Please enter a robot command or 'quit' to exit the program:quitGoodbye!


Examples

[edit |edit source]

equality.py

# This Program Demonstrates the use of the == operator# using numbersprint(5==6)# Using variablesx=5y=8print(x==y)

And the output

FalseFalse

high_low.py

# Plays the guessing game higher or lower# This should actually be something that is semi random like the# last digits of the time or something else, but that will have to# wait till a later chapter.  (Extra Credit, modify it to be random# after the Modules chapter)number=7guess=-1print("Guess the number!")whileguess!=number:guess=int(input("Is it... "))ifguess==number:print("Hooray! You guessed it right!")elifguess<number:print("It's bigger...")elifguess>number:print("It's not so big.")

Sample run:

Guess the number!Is it...2It's bigger...Is it...5It's bigger...Is it...10It's not so big.Is it...7Hooray! You guessed it right!

even.py

# Asks for a number.# Prints if it is even or oddnumber=float(input("Tell me a number: "))ifnumber%2==0:print(int(number),"is even.")elifnumber%2==1:print(int(number),"is odd.")else:print(number,"is very strange.")

Sample runs:

Tell me a number:33 is odd.
Tell me a number:22 is even.
Tell me a number:3.48953.4895 is very strange.

average1.py

# keeps asking for numbers until 0 is entered.# Prints the average value.count=0sum=0.0number=1# set to something that will not exit the while loop immediately.print("Enter 0 to exit the loop")whilenumber!=0:number=float(input("Enter a number: "))ifnumber!=0:count=count+1sum=sum+numberifnumber==0:print("The average was:",sum/count)
Sample runs

Sample runs:

Enter 0 to exit the loopEnter a number:3Enter a number:5Enter a number:0The average was: 4.0
Enter 0 to exit the loopEnter a number:1Enter a number:4Enter a number:3Enter a number:0The average was: 2.66666666667

average2.py

# keeps asking for numbers until count numbers have been entered.# Prints the average value.#Notice that we use an integer to keep track of how many numbers,# but floating point numbers for the input of each numbersum=0.0print("This program will take several numbers then average them")count=int(input("How many numbers would you like to average: "))current_count=0whilecurrent_count<count:current_count=current_count+1print("Number",current_count)number=float(input("Enter a number: "))sum=sum+numberprint("The average was:",sum/count)

Sample runs:

This program will take several numbers then average themHow many numbers would you like to average:2Number 1Enter a number:3Number 2Enter a number:5The average was: 4.0
This program will take several numbers then average themHow many numbers would you like to average:3Number 1Enter a number:1Number 2Enter a number:4Number 3Enter a number:3The average was: 2.66666666667

Exercises

[edit |edit source]

Write a program that asks the user their name, if they enter your namesay "That is a nice name", if they enter "John Cleese" or "MichaelPalin", tell them how you feel about them ;), otherwise tell them "Youhave a nice name."

Solution
name=input('Your name: ')ifname=='Ada':print('That is a nice name.')elifname=='John Cleese':print('... some funny text.')elifname=='Michael Palin':print('... some funny text.')else:print('You have a nice name.')


Modify the higher or lower program from this section to keep track of how many times the user has entered the wrong number. If it is more than 3 times, print "That must have been complicated." at the end, otherwise print "Good job!"

Solution
number=7guess=-1count=0print("Guess the number!")whileguess!=number:guess=int(input("Is it... "))count=count+1ifguess==number:print("Hooray! You guessed it right!")elifguess<number:print("It's bigger...")elifguess>number:print("It's not so big.")ifcount>3:print("That must have been complicated.")else:print("Good job!")


Write a program that asks for two numbers. If the sum of the numbers is greater than 100, print "That is a big number."

Solution
number1=float(input('1st number: '))number2=float(input('2nd number: '))ifnumber1+number2>100:print('That is a big number.')


Modify the Linkbot program presented earlier to include the commands "turnright", "turnleft", and "beep". Make the "turnright" command turn the robot to the right, "turnleft" turn the robot to the left, and "beep" beep the robot buzzer for 1 second.

Solution
importbaroboimporttime# So we can use time.sleep() laterdongle=barobo.Dongle()dongle.connect()myLinkbot=dongle.getLinkbot('abcd')# Replace abcd with your Linkbot's serial IDwhileTrue:# This is an infinite loop. Get out by using the "break" statementcommand=input('Please enter a robot command or "quit" to exit the program: ')ifcommand=='forward':# Rotate the wheels 180 degrees to move the robot forwardmyLinkbot.move(180,0,-180)# Motor 3 has to spin in the negative direction to move the robot forwardelifcommand=='backward':myLinkbot.move(-180,0,180)elifcommand=='turnright':myLinkbot.move(180,0,180)elifcommand=='turnleft':myLinkbot.move(-180,0,-180)elifcommand=='beep':myLinkbot.setBuzzerFrequency(440)time.sleep(1)myLinkbot.setBuzzerFrequency(0)elifcommand=='quit':breakelse:print('I\'m sorry. I don\'t understand that command.')print('Goodbye!')


Learning Python 3 with the Linkbot
 ← Count to 10DecisionsLinkbot Multitasking → 
Retrieved from "https://en.wikibooks.org/w/index.php?title=Learning_Python_3_with_the_Linkbot/Decisions&oldid=3677238"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp