Python-3 语法

时间:2023-03-09 07:16:45
Python-3 语法

#1 Tab键:

  1)控制缩进

  2)IDLE智能补全

#2 =等号:

  1)=:表示赋值

  2)==:表示判断

#3 流程图:

  print('..........小甲鱼_1..........')
  temp=input('猜测小甲鱼心里想的是哪个数字:')
  guess=int(temp)
  if guess==8:
      print('中了')
      print('哼,中了也没有奖励')
  else:
      print('猜错啦,小甲鱼想的是8!')
  print('游戏结束,不玩了。')

Python-3 语法

  改进:

  1)条件 if-else

  2)循环 while and

  3)import random 模块:random.randint函数-返回一个随机的整型

  import random
  secret = random.randint(1,10)
  print('..........小甲鱼_1..........')
  _continue = input('''是否参与游戏?
  是:Y\n''')
  if _continue == 'Y':    
      temp = input('猜测小甲鱼心里想的是哪个数字:')
      guess = int(temp)
      while guess and _continue == 'Y':
          if guess == secret:
              print('中了')
              print('哼,中了也没有奖励')
              break
          else:
              if guess > secret:
                  print('大了大了!')
                  _continue = input('''是否继续猜测?
  继续:Y\n''')
                  temp = input('继续猜测小甲鱼心里想的是哪个数字:')
                  guess = int(temp)
              else:
                  print('小了小了!')
                  _continue = input('''是否继续猜测?
  继续:Y\n''')
                  temp = input('继续猜测小甲鱼心里想的是哪个数字:')
                  guess = int(temp)
  else:
      print('游戏结束,不玩了。')

#4 内置函数 print、input

  >>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']

  注:只有小写的部分是内置函数 

  >>> help(input)
  Help on built-in function input in module builtins:

  input(...)
      input([prompt]) -> string
    
      Read a string from standard input.  The trailing newline is stripped.
      If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
      On Unix, GNU readline is used if enabled.  The prompt string, if given,
      is printed without a trailing newline before reading.