Small Turtle Python Lecture 4

In the 1while statement, when the condition is true, it will continue to loop, such as the following example, but you can use Ctral + C to force the end

while 'C':
    print("i love you")

 

2. Observe the number of prints

i = 10
while i > 0:
    print("i love you")
    i = i - 1

10 prints of 'i love you'

 

3. Pay attention to the usage of and (meaning that both sides are true), please write an expression equivalent to 10<a<19

10 < a < 19
10 < a and a < 19

 

4. Short circuit logic

Generally speaking, short-circuit logic is worth in a logic, only the first half is judged, as long as the first half can determine the result, the second half of the logic will not be judged.

 

5. In Python3, can multiple statements be written in one line?

Can;

print('asdf');print('asdf')

 

6. In python3, can a statement be written in multiple lines?

Yes, it can be broken into several lines using backslashes or parentheses

print\

("hh")

 

7.and or

x or y  ---if x is false,then y,else x

x and y --if x is false,then x,else y

not x--- if x if false, then True,else False

 

8. Note the condition in the while i.e. 0 is false

copy code
num = int(input("Please enter an integer: "))
i = 0
while num:
    i = i + 1
    num = num - 1
    print(i)
copy code

Additional with small turtle

copy code
temp = input('Please enter an integer:')
number = int(temp)
i = 1
while number:
    print(i)
    i = i + 1
    number = number - 1
copy code

 

9. (Plagiarized from the small turtle)

copy code
temp = input('Please enter an integer:')
number = int(temp)
while number:
    i = number - 1
    while i:
        print(' ', end = '')
        i = i - 1
    j = number
    while j:
        print('*', end = '')
        j = j - 1
    print()
    number = number - 1
copy code

10. (Plagiarized from the small turtle)

copy code
import random
times = 3
secret = random.randint(1,10)
print('-------------------I love fish C studio-------------------')
# Here first assign a value to guess (assign a value that is definitely not equal to secret)
guess = 0
# The default of print() is to automatically add a newline after printing the string, and the end=" " parameter tells print() to replace the line with a space.
# Well, if the turtle thinks it's creative, you should try to use end="JJ"?
print("Let's guess which number the turtle is thinking about now:", end=" ")
while (guess != secret) and (times > 0):
    temp = input()
    guess = int(temp)
    times = times - 1 # Each time the user enters, the available chance is -1
    if guess == secret:
        print("My grass, are you the roundworm in the little turtle's heart?!")
        print("Hey, there is no reward for guessing right!")
    else:
        if guess > secret:
            print("Brother, it's bigger~~~")
        else:
            print("Hey, small, small~~~")
        if times > 0:
            print("Try again: ", end=" ")
        else:
            print("Opportunities run out T_T")
print("The game is over, stop playing ^_^")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325537029&siteId=291194637