您的位置:首页 > 编程语言 > Python开发

零基础入门学习Python(2)--用Python设计第一个游戏

2018-01-30 14:32 721 查看

前言

小甲鱼的
Python
课程都是围绕着一个个小游戏,进行
Python
的讲解,由易入难。

小游戏流程图

Created with Raphaël 2.1.2Startprint('------------------我爱鱼C工作室------------------')temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")guess = int(temp)if guess == 8 print("我草,你是小甲鱼心里的蛔虫吗?!")print("哼,猜中了也没有奖励!")print("游戏结束,不玩啦^_^")Endprint("猜错啦,小甲鱼现在心里想的是8!")yesno

小游戏代码

print('------------------我爱鱼C工作室------------------')
temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
guess = int(temp)
if guess == 8:
print("我草,你是小甲鱼心里的蛔虫吗?!")
print("哼,猜中了也没有奖励!")
else:
print("猜错拉,小甲鱼现在心里想的是8!")
print("游戏结束,不玩啦^_^")


知识点

什么是
BIF


BIF就是 Built-in functions,内置函数。为了方便程序员快速编写脚本程序,python提供了非常丰富的内置函数,我们只需要直接调用即可,例如
print()
的功能就是打印到屏幕
input()
的作用就是接收用户输入

Python3提供了多少个
BIF


在IDLE中,输入
dir(__builtins__)
,可以看到Python提供的内置方法列表,其中小写的就是BIF。如果想具体查看某个BIF功能,比如
input()
,可以在IDLE中输入
help(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', 'ModuleNotFoundError', '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
b6c0
', '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(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.


‘FishC’ 和 ‘fishc’ 一样吗?

不一样。可以在IDLE中输入
'FishC' == 'fishc'
,进行测试

>>> 'FishC' == 'fishc'
False


Python中什么是最重要的?你赞同吗?

缩进!缩进是Python语言的灵魂,缩进使得python的代码更加精简,并且有层次。在python中对待缩进要十分小心的,因为如果没有使用正确的使用缩进,代码所做的事情就会和你所期望的相去甚远

如果在正确的位置输入
:
,IDLE会自动将下一行缩进!

上述例子中出现了
=
==
,都表示什么含义?


==
:表示判断是否相等

=
:表示赋值,把右边的值给到左边的变量里面去

你听说过
拼接
这个词吗?


在一些编程语言,我们可以将两个字符串”相加”在一起,如:
'I' +'Love' +'FishC'
会得到
'ILoveFishC'
,在python里,这种做法叫做拼接字符串。

编写程序:hello.py,要求用户输入姓名打印
你好,xx!


name = input("请输入您的姓名:")
print('你好,'+name+'!')


编写程序:calc.py要求用户输入1到100之间的数字并判断,输入符合要求打印
你妹好漂亮^_^
,不符合要求打印
你大爷好丑T_T


temp = input("请输入1到100之间的数字:")
num = int(temp)
if 1 <= num <= 100:
print('你妹好漂亮^_^')
else:
print('你大爷好丑T_T')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python
相关文章推荐