学习资料:www.fishc.com

我的第一个程序:

print('-------MissZhou的第一个游戏-------------')
temp=input('猜猜她心里想的是那个数字')
guess=int(temp)
if guess==8:print("你怎么猜到了")print("猜到了也没用")
else:print("猜错啦 想的是8")
print("游戏结束,不玩啦")

程序运行没啥好说的,有点编程底子就能猜出来结果==

在pycharm中就像phpstorm一样使用,(本来也都是jetbrains公司造的)建立新工程,建立新文件 run运行,像idea一样在下面运行框中输入值

在idle中(这里多说一嘴,python这货貌似和linux走的比跟windows近,你看在windows下需要自己下载安装,linux里面是内置的,然后idle中,叫shell!!但是不能用上键使用上一条指令orz),新建文件,输入我的程序,run即可

再说python的内置函数(BIF)参考,使用函数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', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', '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__', '__spec__', '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']

像不像php!!

具体函数的使用呢?Help(函数名)

比方说:

help(input)Help on built-in function input in module builtins:input(prompt=None, /)Read a string from standard input.  The trailing newline is stripped.The prompt string, if given, is printed to standard output without atrailing newline before reading input.If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.On *nix systems, readline is used if available.help(int)Help on class int in module builtins:class int(object)|  int(x=0) -> integer|  int(x, base=10) -> integer|  |  Convert a number or string to an integer, or return 0 if no arguments|  are given.  If x is a number, return x.__int__().  For floating point|  numbers, this truncates towards zero.|  |  If x is not a number or if base is given, then x must be a string,|  bytes, or bytearray instance representing an integer literal in the|  given base.  The literal can be preceded by '+' or '-' and be surrounded|  by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.|  Base 0 means to interpret the base from the string as an integer literal.|  >>> int('0b100', base=0)|  4|  |  Methods defined here:|  |  __abs__(self, /)|      abs(self)|  |  __add__(self, value, /)|      Return self+value.|  |  __and__(self, value, /)|      Return self&value.|  |  __bool__(self, /)|      self != 0|  |  __ceil__(...)|      Ceiling of an Integral returns itself.|  |  __divmod__(self, value, /)|      Return divmod(self, value).|  |  __eq__(self, value, /)|      Return self==value.|  |  __float__(self, /)|      float(self)|  |  __floor__(...)|      Flooring an Integral returns itself.|  |  __floordiv__(self, value, /)|      Return self//value.|  |  __format__(...)|      default object formatter|  |  __ge__(self, value, /)|      Return self>=value.|  |  __getattribute__(self, name, /)|      Return getattr(self, name).|  |  __getnewargs__(...)|  |  __gt__(self, value, /)|      Return self>value.|  |  __hash__(self, /)|      Return hash(self).|  |  __index__(self, /)|      Return self converted to an integer, if self is suitable for use as an index into a list.|  |  __int__(self, /)|      int(self)|  |  __invert__(self, /)|      ~self|  |  __le__(self, value, /)|      Return self<=value.|  |  __lshift__(self, value, /)|      Return self<<value.|  |  __lt__(self, value, /)|      Return self<value.|  |  __mod__(self, value, /)|      Return self%value.|  |  __mul__(self, value, /)|      Return self*value.|  |  __ne__(self, value, /)|      Return self!=value.|  |  __neg__(self, /)|      -self|  |  __new__(*args, **kwargs) from builtins.type|      Create and return a new object.  See help(type) for accurate signature.|  |  __or__(self, value, /)|      Return self|value.|  |  __pos__(self, /)|      +self|  |  __pow__(self, value, mod=None, /)|      Return pow(self, value, mod).|  |  __radd__(self, value, /)|      Return value+self.|  |  __rand__(self, value, /)|      Return value&self.|  |  __rdivmod__(self, value, /)|      Return divmod(value, self).|  |  __repr__(self, /)|      Return repr(self).|  |  __rfloordiv__(self, value, /)|      Return value//self.|  |  __rlshift__(self, value, /)|      Return value<<self.|  |  __rmod__(self, value, /)|      Return value%self.|  |  __rmul__(self, value, /)|      Return value*self.|  |  __ror__(self, value, /)|      Return value|self.|  |  __round__(...)|      Rounding an Integral returns itself.|      Rounding with an ndigits argument also returns an integer.|  |  __rpow__(self, value, mod=None, /)|      Return pow(value, self, mod).|  |  __rrshift__(self, value, /)|      Return value>>self.|  |  __rshift__(self, value, /)|      Return self>>value.|  |  __rsub__(self, value, /)|      Return value-self.|  |  __rtruediv__(self, value, /)|      Return value/self.|  |  __rxor__(self, value, /)|      Return value^self.|  |  __sizeof__(...)|      Returns size in memory, in bytes|  |  __str__(self, /)|      Return str(self).|  |  __sub__(self, value, /)|      Return self-value.|  |  __truediv__(self, value, /)|      Return self/value.|  |  __trunc__(...)|      Truncating an Integral returns itself.|  |  __xor__(self, value, /)|      Return self^value.|  |  bit_length(...)|      int.bit_length() -> int|      |      Number of bits necessary to represent self in binary.|      >>> bin(37)|      '0b100101'|      >>> (37).bit_length()|      6|  |  conjugate(...)|      Returns self, the complex conjugate of any int.|  |  from_bytes(...) from builtins.type|      int.from_bytes(bytes, byteorder, *, signed=False) -> int|      |      Return the integer represented by the given array of bytes.|      |      The bytes argument must be a bytes-like object (e.g. bytes or bytearray).|      |      The byteorder argument determines the byte order used to represent the|      integer.  If byteorder is 'big', the most significant byte is at the|      beginning of the byte array.  If byteorder is 'little', the most|      significant byte is at the end of the byte array.  To request the native|      byte order of the host system, use `sys.byteorder' as the byte order value.|      |      The signed keyword-only argument indicates whether two's complement is|      used to represent the integer.|  |  to_bytes(...)|      int.to_bytes(length, byteorder, *, signed=False) -> bytes|      |      Return an array of bytes representing an integer.|      |      The integer is represented using length bytes.  An OverflowError is|      raised if the integer is not representable with the given number of|      bytes.|      |      The byteorder argument determines the byte order used to represent the|      integer.  If byteorder is 'big', the most significant byte is at the|      beginning of the byte array.  If byteorder is 'little', the most|      significant byte is at the end of the byte array.  To request the native|      byte order of the host system, use `sys.byteorder' as the byte order value.|      |      The signed keyword-only argument determines whether two's complement is|      used to represent the integer.  If signed is False and a negative integer|      is given, an OverflowError is raised.|  |  ----------------------------------------------------------------------|  Data descriptors defined here:|  |  denominator|      the denominator of a rational number in lowest terms|  |  imag|      the imaginary part of a complex number|  |  numerator|      the numerator of a rational number in lowest terms|  |  real|      the real part of a complex number

变量:

使用前需要赋值;变量名包括字母数字下划线,不能以数字开头;区分大小写

字符串:

狭义的认为是引号内的一切东西。可以是单引号、双引号,但是不可以‘XXXXXXX”

打印let’s go

>>> print('let\'s go')let's go>>> print("let's go")let's go

试试这个例子:

>>> str='c:\now'>>> str'c:\now'>>> print(str)c:Ow

可以这样

>>> str='c:\\now'>>> str'c:\\now'>>> print(str)c:\now

要是需要特别多的反义呢?

原子字符串duangduangduang~~~

使用方法:

>>> str=r'C:\now\misszhou'>>> str'C:\\now\\misszhou'>>> print(str)C:\now\misszhou

注意原子字符串的结尾不可以有\

长字符串:跨行的那种,需要三重引号字符串

str="""Number of bits necessary to represent self in binary.|      >>> bin(37)|      '0b100101'|      >>> (37).bit_length()|      6""">>> str"Number of bits necessary to represent self in binary.\n |      >>> bin(37)\n |      '0b100101'\n |      >>> (37).bit_length()\n |      6">>> print(str)Number of bits necessary to represent self in binary.|      >>> bin(37)|      '0b100101'|      >>> (37).bit_length()|      6

比较

<= < > >= == !=都一样的呢

>>> 1>3False>>> 1<2True

没有===  (???忘了)

>>> 1.0==1True>>> 1.0===1SyntaxError: invalid syntax

条件

if....:  (真)

else:

有隐式转化

>>> if (1):print("1")else:print("2")1

random模块改良游戏:

import random
secret=random.randint(1,10)
print('-------MissZhou的第一个游戏改良版-------------')
temp=input('猜猜她心里想的是那个数字')
guess=int(temp)
while guess!=secret:temp=input("猜错了,重新输入")guess=int(temp)if guess==secret:print("你怎么猜到了")print("猜到了也没用")else:if guess>secret:print("猜大啦")else:print("猜小啦")
print("游戏结束,不玩啦")猜猜她心里想的是那个数字9猜错了,重新输入8(这里也没提供错误提示啊)猜大啦猜错了,重新输入3猜小啦猜错了,重新输入4你怎么猜到了猜到了也没用游戏结束,不玩啦

如果第一次就猜到了根本也没有提示啊

以上两个问题作为作业,改进程序如下

import random
secret=random.randint(1,10)
print('-------MissZhou的第一个游戏改良版-------------')
temp=input('猜猜她心里想的是那个数字')
guess=int(temp)
if(guess==secret):print("你怎么猜到了")print("猜到了也没用")
else:if guess > secret:print("猜大啦")else:print("猜小啦")
while guess!=secret:temp=input("猜错了,重新输入")guess=int(temp)if guess==secret:print("你怎么猜到了")print("猜到了也没用")else:if guess>secret:print("猜大啦")else:print("猜小啦")print("游戏结束,不玩啦")猜猜她心里想的是那个数字1你怎么猜到了猜到了也没用游戏结束,不玩啦猜猜她心里想的是那个数字8猜大啦猜错了,重新输入8猜大啦猜错了,重新输入7猜大啦猜错了,重新输入

python入门——猜数字游戏相关推荐

  1. 小甲鱼python数字游戏给三次机会_【python 入门案例】小甲鱼python入门 猜数字游戏...

    一.猜数字--一次机会,无提示 print("--------------第一次尝试---------------") temp = input("猜一下小西几心里想的什 ...

  2. python实现猜数字游戏_python如何实现猜数字游戏

    python实现猜数字游戏的方法:使用条件语句实现判断,代码为[print('猜一个1-20之间的整数.');print('开始猜:');for i in range(1, 7):try:guess ...

  3. python编写猜数字游戏

    一.猜数字游戏介绍 猜数字(又称 Bulls and Cows )是一种古老的的密码破译类益智类小游戏,起源于20世纪中期,一般由两个人或多人玩,也可以由一个人和电脑玩. 二.猜数字游戏规则 先解释标 ...

  4. python随机猜数字游戏_Python小游戏——猜数字教程(random库教程)

    今天来开发一个简单的数字逻辑游戏,猜数字(数字炸弹) 首先开发游戏第一件事,了解需求. 猜数字游戏规则: 计算机随机生成一个指定范围的数字,由玩家来猜测, 之后计算机会根据玩家提供数字来与自己生成的数 ...

  5. python猜数字游戏代码多次_黄哥Python:猜数字游戏代码

    猜数字游戏,电脑随机生存一个[1, 100] 之间的正整数,人输入一个数字,输出大了,还是小了,猜对了,就退出游戏.由于初学者没有学异常,所以没有采用异常. """ 猜数 ...

  6. python外星人入侵游戏代码_黄哥Python:猜数字游戏代码

    """猜数字游戏 黄哥所写黄哥Python培训 2020 全新升级课程"""import randomcomputer_num = rand ...

  7. 用python写猜数字游戏

    猜数字游戏 从名单中随机抽取5位同学,参加猜数字 目的:练习文件操作.循环.判断.构造函数.列表和集合的操作 主要思路: 随机抽取从文件中抽取5个人员参加游戏 判断名单名字的行数 随机生成数,并在文件 ...

  8. python实例 - 猜数字游戏

    猜数字游戏 在游戏中,程序每一轮会随机生成一个0-1024之间的数字,用户输入猜测的数字,程序告诉用户猜大了还是猜小了.在一定次数内猜对,则本轮用户获胜,否则本轮用户失败.每一轮开始时,程序会要求用户 ...

  9. python猜数字游戏编程、最后显示猜了几次_用Python完成猜数字游戏

    五一假期第一天突然想学点新东西,于是把Python重新捡起来.按照Crossin的编程教室中的<Python入门教程>写了一段代码,实现猜字游戏. !/usr/bin/python cod ...

最新文章

  1. 2017清华本科生特等奖得主出炉,AI学霸乔明达获奖
  2. 1. Linux内核的配置与裁减:
  3. Facebook与Google的互联网霸主争夺战
  4. 文件打开特别慢_“Origin进不去、下载慢”的解决办法合集
  5. Spring Cloud微服务之Mybatis-Plus代码生成器整合(四)
  6. svchost占用内存过高_「电脑常用技巧」电脑卡的原因(占用过高怎么办)?
  7. 瞧一瞧,看一看,微信应用号(小程序)
  8. java程序中空一阁_如何使用JFlex、JavaCUP(详细代码模版) by 踏雪赤兔
  9. 12堂超级搜索术课程笔记链接汇总
  10. luarocks安装
  11. unity3d 人物对话_Unity 3D游戏-NPC对话系统With XML
  12. 一文讲解5G、互联网、物联网、大数据、人工智能的关系
  13. python操作word文档,合并
  14. 一场接近于失败的胜利:CIA“星门计划”始末
  15. 高通 Camx debug log控制
  16. netty 错误 #[IllegalReferenceCountException: refCnt: 0, decrement: 1]
  17. 使用Python统计股票高开后的走势
  18. 延安日报20220323导读:“金斧”、金珠、玉刀、木箱
  19. WRMPS经典Cookie欺骗漏洞批量拿下shell-黑客博客
  20. Mock服务的理解和搭建

热门文章

  1. delphi出现‘尚未调用CoInitialize’异常
  2. Mybatis源码介绍
  3. 用C/C++实现简单游戏开发:easyx实现幻彩贪吃蛇
  4. Dear ImGui中文文档(一)
  5. 利用ffmpeg加水印
  6. TranslateMessage(msg); WINAPI
  7. android 弹出选择输入法选择界面
  8. FatMouse'trade
  9. Some code changes cannot be hot swapped into a running virtual machine,
  10. Swagger 在线接口api使用