• 练习11--提问(如何把数据读到程序里面去)


    一 大部分软件的主要做的事情

    1 从用户哪里获得一些输入

    2 把用户输入的数据改一改

    3 打印出改变了的用户输入

    二 input函数

    1 定义:接受一个标准的输入数据,返回为String类型

    2 格式:变量名 = input()

    3 注意:在 Python3.x 中 raw_input() 和 input() 进行了整合,去除了 raw_input( ),仅保留了input( )函数,其接收任意任性输入,将所有输入默认为字符串处理,并返回字符串类型。(详细内容见:https://www.runoob.com/python3/python3-func-input.html

    4 如何从别人那里人那里获得一些数字来做数学运算?你可以试试输入x = int(input()) ,这样可以从 input() 里面获取到字符串形式的数字,再用 int() 把它们转化成数值。

    5 我们在每一个打印行末尾放一个 end=' ' ,是为了告诉 print 不要另起一行。

    三 代码及运行结果

    1 代码:

    print("How old are you?",end=' ')
    age = input()
    print("How tall are you?",end=' ')
    height = input()
    print("How much do you weight?",end=' ')
    weight = input()
    print(f"So,you're {age} old,{height} tall and {weight} heavy.")

    2 运行结果

    PS E:3_work4_python2_code2_LearnPythonTheHardWay> python ex11.py
    How old are you? 18
    How tall are you? 166
    How much do you weight? 88
    So,you're 18 old,166 tall and 88 heavy.

    3 一个错误

    我在代码中加了这样一行:print(f"So,you're {} old,{} tall and {} heavy.",age,height,weight)

    在PowerShell里面运行程序之后,程序报错,具体内容如下:

    PS E:3_work4_python2_code2_LearnPythonTheHardWay> python ex11.py
      File "ex11.py", line 9
        print(f"So,you're {} old,{} tall and {} heavy.",age,height,weight)
                ^
    SyntaxError: f-string: empty expression not allowed

    说明在格式化输出”f“里面,变量必须放在{}内部,否则程序报错,即这种格式的代码是不允许的。

  • 相关阅读:
    计算机网络精华知识总结01
    使用hexo创建github博客
    Android Toolbar样式定制详解
    我的PhoneGap安装配置经历
    APMServ5.2.6升级PHP
    WordPress网站更换老鹰主机详细操作
    Windows下的SVN环境搭建详解
    最新Android 出现Please ensure that adb is correctly located at问题的解决方法
    QSqlDatabase
    Qt Pro相关
  • 原文地址:https://www.cnblogs.com/luoxun/p/13190570.html
Copyright © 2020-2023  润新知