标签:
学习资料: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<=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>3 False >>> 1<2 True
没有=== (???忘了)
>>> 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 猜大啦 猜错了,重新输入
标签:
原文地址:http://blog.csdn.net/zhou_yujia/article/details/51943824