python入门

您所在的位置:网站首页 如何猜中别人心里想的数字 python入门

python入门

2024-07-04 21:24:06| 来源: 网络整理| 查看: 265

学习资料: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 a trailing 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>> 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>3 False >>> 1>> 1.0==1 True >>> 1.0===1 SyntaxError: 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 猜大啦 猜错了,重新输入



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭