Python基础知识001

Python基础知识001

  • 学习Python的第一个语句:

  print(“Hello World!”)  #在Python3中,print是一个函数,在Python2中则不是,有兴趣可以自行了解。

  • 变量的定义法则

  在Python中,定义变量只能是英文字母开头或下划线(_),切记注意。(例:name = "Li ",_num = 18)

  在定义名字的时候,如果你想把它设置为常量,则一般把名字大写。(例:PI = 3.14;AGE = 18)

  • 来一个关于input的小项目

  input,当输入是数字的时候,默认是str类型,所以需要转换类型。一般可用int(input(“age:”))

 

 

  • 来一个关于猜年龄的小项目:

  student_age = 16

  age = int( input("age:") )

  if age == student_age:

    print("Yes you got it!")

  elif age < student_age:

    print('samller!")

  else:

    print("bigger")

  以上程序每次都有重新运行才可进行新一轮猜测,所以可以优化一下:

  

  student_age = 16

  age = int( input("age:") )

  count = 0

  while count<3:

  if age == student_age:

    print("Yes you got it!")

    break

  elif age < student_age:

    print('samller!")

  else:

    print("bigger")

  count += 1

else:

  print("次数太多!")

任意玩法版:

  student_age = 16

  age = int( input("age:") )

  count = 0

  while count<3:

  if age == student_age:

    print("Yes you got it!")

    break

  elif age < student_age:

    print('samller!")

  else:

    print("bigger")

    count += 1

   #继续重复判断是否需要重新玩,如果需要,则重新设置count=0,再次进入循环

   if count == 3:

      couinte_info = input("Do you want to again?")

      if couinte != "n":

        count = 0

 

 

posted @ 2021-04-27 20:19  Five_Stars  阅读(78)  评论(0)    收藏  举报