zoukankan      html  css  js  c++  java
  • 笨办法学Python

    1、习题 11: 提问

    学习目标:了解人机交互场景,熟悉raw_input 的用法。

    1、在 Python2.x 中 raw_input( ) 和 input( ),两个函数都存在,具体区别详情请参考习题5,其中区别为:

    • raw_input( ) 将所有输入作为字符串看待,返回字符串类型。
    • input( ) 只能接收“数字”的输入,在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )。

    2、在 Python3.x 中 raw_input( )input( ) 进行了整合,去除了 raw_input( ),仅保留了 input( ) 函数,其接收任意任性输入,将所有输入默认为字符串处理,并返回字符串类型。

    习题十一中的练习代码是:

    #! -*-coding=utf-8-*-
    
    print("How old are you?"),
    age = raw_input()
    
    print "How tall are you?",
    height = raw_input()
    
    print "How much do you weigh?",
    weight = raw_input()
    
    print "So, you're %r old, %r tall and %r heavy." % (
    age, height, weight)
    
    C:Python27python.exe D:/pythoncode/stupid_way_study/demo11/Exer11-1.py
    How old are you? 14
    How tall are you? 34
    How much do you weigh? 123
    So, you're '14' old, '34' tall and '123' heavy.
    
    Process finished with exit code 0
    

    上述代码有一个问题,就是每一个print 一句后面都有一个逗号,这是因为这样的话print 就不会输出新行符而结束这一行跑到下一行去了,具体详情用法请参考习题7

    2、习题 12: 提示别人

    学习目标:继续学习raw_input 的用法,了解怎么添加提示信息。

    习题十二中的练习代码是:

    age = raw_input("How old are you? ")
    
    height = raw_input("How tall are you? ")
    
    weight = raw_input("How much do you weigh? ")
    
    print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)
    

    上述代码的运行结果为:

    C:Python27python.exe D:/pythoncode/stupid_way_study/demo11/Exer11-1.py
    How old are you? 23
    How tall are you? 45
    How much do you weigh? 123
    So, you're '23' old, '45' tall and '123' heavy.
    
    Process finished with exit code 0
    

    了解一下pydoc的用法:

    D:pythoncodestupid_way_studydemo11>python -m pydoc Exer11-1
    How old are you? 12
    How tall are you? 23
    How much do you weigh? 123
    So, you're '12' old, '23' tall and '123' heavy.
    Help on module Exer11-1:
    
    # 展示该文件具体内容
    NAME
        Exer11-1
    
    FILE
        d:pythoncodestupid_way_studydemo11exer11-1.py
    
    DESCRIPTION
        # print("How old are you?"),
        # age = raw_input()
        #
        # print "How tall are you?",
        # height = raw_input()
        #
        # print "How much do you weigh?",
        # weight = raw_input()
        #
        # print "So, you're %r old, %r tall and %r heavy." % (
        # age, height, weight)
    
    DATA
        age = '12'
        height = '23'
        weight = '123'
    

    pydoc是python自带的一个文档生成工具,使用pydoc可以很方便的查看类和方法结构

    同时我们可以在本地开启端口,然后在浏览器端看源代码解释:

    本地窗口使用命令python3 -m pydoc -p 1234 开启:

    1547784090545

    浏览器端可直接访问开放的端口:http://localhost:1234/

    1547784037370

    当然还可以在命令行下直接查看某一模块的源代码:

    使用命令:python3 -m pydoc os

    1547784290470

    3、总结

    这两题主要是学习了和计算机交互的函数raw_input() ,注意由于Python2中input() 自身的原因会导致一些问题,所以一般在Python2中只使用input(),然后学习了解pydoc的用法。

  • 相关阅读:
    C# Ini配置文件
    C#日志写入
    GZFramework.DB.Core初始化
    httpHelper
    GZFramework代码生成器插件使用教程
    MVC部署IIS设置
    SignalR记录
    洛谷 P2360 地下城主
    洛谷 P1379 八数码难题(map && 双向bfs)
    洛谷 P1155 双栈排序
  • 原文地址:https://www.cnblogs.com/csyxf/p/10287286.html
Copyright © 2011-2022 走看看