Day 4 ** Python study notes **

(This article is only normal learning record, if any error please chiefs pointed out that if this article can help you that I am also very happy friends)

 

P5 improve our game

First, note

1. Change the original game, suggesting increased

  • Use conditional branching , conditional branching syntax:
if conditions: 
    the condition is true (True) operation performed 
the else : 
    operating condition is false (False) executed
  • wordgame_2
Print ( ' wordgame_2 ' ) 
the TEMP = the INPUT ( ' Guess a number: ' ) 
Guss = int (the TEMP)
 IF Guss == 8 :
     Print ( ' on it ' )
     Print ( ' ! But no reward ' )
 the else :
     IF Guss> . 8 :
         Print ( ' large large la la ' )
     the else :
         Print ( ' small small la la ' )
 Print (' Game Over ' )

2. Add the prompts and provide multiple opportunities

  • Use a while loop , while loop syntax:
while conditions: 
    the condition is true (True) operation performed
  • wordgame_3
Print ( ' wordgame_3 ' ) 
the TEMP = the INPUT ( ' Guess a number: ' ) 
Guss = int (the TEMP)
 the while ! Guss = 8 : 
    the TEMP = the INPUT ( ' Guess again now: ' ) 
    Guss = int (the TEMP)
     IF Guss = 8 = :
         Print ( ' on it ' )
         Print ( ' ! but no reward ' )
     the else :
         IF Guss> 8 :
             Print( ' Great friends great friends ' )
         the else :
             Print ( ' little friends little friends ' )
 Print ( ' Game Over ' )

3. Each time you run the program produces answers are random

  • Using the random module in the randint () function , the function returns a random integer, the usage rule
    # Use introducing import module 
import Random
     # call the function module format must be the name of the module. Function name 
sercet = random.randint (1,10)
  • wordgame_4
Import Random 
sercet = random.randint (1, 10 )
 Print ( ' wordgame_4 ' ) 
the TEMP = the INPUT ( ' Guess a number: ' ) 
Guss = int (the TEMP)
 the while Guss =! sercet: 
    the TEMP = the INPUT ( ' Guess again it : ' ) 
    Guss = int (the TEMP)
     IF Guss == sercet:
         Print ( ' on it ' )
         Print ( ' but no reward! ' )
    the else :
         IF Guss> sercet:
             Print ( ' great big friends friends ' )
         the else :
             Print ( ' little friends little friends ' )
 Print ( ' Game Over ' )

4. Comparison Operators Logical Operators ratio higher priority

5. Use and logical operators, any expression may be connected together and a Boolean value is obtained, such as:

>>>(1<3) and (3>2)
True

6.print () default is printing the string will automatically add a newline character, end = "" parameter tells the print () instead of line breaks with spaces, such as:

Print ( " Guess: " , End = "  " )

 

Second, after-school job

Will the following code will print 0. How many times "I love fish C!"

while 'C':

print ( 'I love fish C!')

Unlimited, because automatically determine the condition is true, it is infinite loop

1. Will the following code will print how many times "I love fish C!"

i = 10

while i:

print ( 'I love fish C!')

i = i - 1

10, in Python, None, any type of value 0, empty string "", empty tuple (), empty list [] {} are empty dictionary as False, there are custom type, if __ __ achieved nonzero () __ or __ len () method and the method returns 0 or False, the examples thereof are also as False, True other objects are 

2. Write the 10 <cost <50 equivalent expression

(10 < cost) and (cost < 50)

3.Python3 in his writing can be more statements do?

Can be separated by a semicolon, such as:
 >>> Print ( 'I LOVE'); Print ( 'you') 

4. Python3, one sentence can be divided into multiple lines written it?

Can, with a backslash \, or parentheses decomposed into several lines, such as:
 >>> Print ( ' I Love \ 
        you ' )
 >>> I Love you
 >>> (. 4>. 3
          and . 5>. 4 )
 >> > true

5. Does the Python and C language operators and && operators What is the difference? [This title has for C or C ++ based friends]

6. heard of "short-circuit logic (short-circuit logic)" do?

Logical operator has interesting properties: does not operate when required without value. That might be more "profound", for example, the expression x and y, x and y need two variables at the same time is true (True) when the result was true. Therefore, if and when that variable x is false (False) when the expression returns False immediately, without having to pipe the value of y variables. 
This behavior is called short-circuit logic (Short -circuit Logic) or lazy evaluation (lazy evaluation), this behavior is also applied with or operator, this small turtle back course will be mentioned, not urgent. 
In fact, Python approach is that if x is false, the expression returns the value of x (0), otherwise it will return the value of y (example with reference to the upstairs title).

 

Third, move hands

0. perfect second improvement requirements (provide users with the opportunity to try three times, the opportunity to run out or the user to guess the answers were to exit the loop) and improve video small turtle code.

  • Answers
Import Random 
Times = 3 
Secret = random.randint (1, 10 )
 Print ( ' ------------------ I love fish studio -------- C ---------- ' )
     # here give guess assignment (assigned an absolute value not equal to the secret) 
guess = 0
     # Print () the default is to print a complete line feed character string is automatically added, end = "" parameter tells the print () with spaces instead of newline 
    # ah, a small turtle creative think you should try end = "JJ"? 
Print ( " might guess a small turtle in mind now is which number: " , End = "  " )
 the while (GUESS = Secret!) and (Times> 0): 
    the TEMP = the INPUT () 
    GUESS= Int (the TEMP) 
    Times = Times - 1 # user input once every available opportunity to -1 
    IF GUESS == Secret:
         Print ( " I am the grass, you are a small turtle heart worms do?! " )
         Print ( " Well, guess there is no reward! " )
     the else :
         IF GUESS> Secret:
             Print ( " brother, big big ~ ~ ~ " )
         the else :
             Print ( " Hey, small, small ~ ~ ~ " )
         IF Times> 0:
             Print( " Try it again: " , End = "  " )
         the else :
             Print ( " the opportunity to use up slightly T_T " )
 Print ( " Game over, not Wanla ^ _ ^ " )
  • my answer
import random
secret = random.randint(1,10)
print("猜数字")
number = input("输入数字:")
guess = int(number)
n = 3
while (guess != secret) and (n > 1):
    if guess > secret:
        print("大了")
    else:
        print("小了")
    n = n - 1
    number = input("输入数字:")
    guess = int(number)
if  guess == secret:
    print("对了,游戏结束")
else:
    print("机会没了,游戏结束")

1.尝试写代码实现以下截图功能:

  • 参考答案
temp = input('请输入一个整数:')
number = int(temp)
i = 1
while number:
    print(i)
    i = i + 1
number = number - 1
  • 我的答案
temp = input("请输入一个整数:")
number = int(temp)
i = 1
while i <= number:
    print(i)
    i = i + 1

2. 尝试写代码实现以下截图功能:

  • 参考答案
temp = input('请输入一个整数:')
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
  • 我的答案 
temp = input("请输入一个整数:")
number = int(temp)
i = 1
while number > 0:
    print(" " * (number - 1) + "*" * number)
    number = number - 1

 

Guess you like

Origin www.cnblogs.com/yankaohaitaiwei/p/12424272.html