Chapter V small turtle after-school programming that summary and reflection

First: For small video vulnerability of small turtle mentioned, once again improve our game: When the user enters the wrong type, timely to remind the user to re-enter, prevent crashes.

import random
secret = random.randint(1,10)
time = 3
guess= 0
print("猜猜我是几:",end="")
while  (guess != secret) and (time>0):
    temp = input()
    time = time - 1
#
    while not temp.isdigit():
        temp = input("抱歉,您的输入有误,请输入一个整数:")
    guess = int(temp)
#1.首先两个重要的内容s.isdigit意为字符串s全为数字为真;s.isalpha意为字符串s全为字符为真。利用
s.is的方法识别输入是否正确可以有效地避免输入temp为str后一直保持str的情况,从而达到识别的目的。
  2.guess与上面temp对齐因为是正确赋值后再取整赋值于guess。   
    if guess == secret:
       print("you are a good boy")
    else:
       if  guess <secret:
          print("it is so small")
       else:
          print ("it is too big")
    if time > 0:
       print("再试一次吧:", end=" ")
    else:
       print("机会用光咯T_T")
print ("game over")


ps:s为字符串
s.isalnum()  所有字符都是数字或者字母,为真返回 Ture,否则返回 False。
s.isalpha()   所有字符都是字母,为真返回 Ture,否则返回 False。
s.isdigit()     所有字符都是数字,为真返回 Ture,否则返回 False。
s.islower()    所有字符都是小写,为真返回 Ture,否则返回 False。
s.isupper()   所有字符都是大写,为真返回 Ture,否则返回 False。
s.istitle() 所有单词都是首字母大写,为真返回 Ture,否则返回 False。
s.isspace()   所有字符都是空白字符,为真返回 Ture,否则返回 False。

例如:
>>> s = 'I LOVE FISHC'
>>> s.isupper()
>>> True
#注意括号!!!!!!


A second inscribed program determines whether a given year is a leap year. (Note: Please use the BIF has learned the flexible use)

temp = input('请输入您现在的年份:')

while not temp.isdigit():
    temp=input('输入错误请重新输入:')
    #第一次编程在这里出错,isdigit一定加()

year = int(temp)

if not year%4 == 0:
    print ('您输入的年份不为闰年!')
else :
    print('您输入的年份为闰年!')

Summary: on the application is, you can effectively avoid the use of type and function isinstance function can not solve the problem of the return input string (input return value is always a string), and is a direct input to determine the type of the return value rather than input so the judge whlie!

Published 17 original articles · won praise 1 · views 368

Guess you like

Origin blog.csdn.net/cccccccaaaaaaaaa/article/details/105168296