练习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 代码:

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

2 运行结果

  • PS E:\3_work\4_python\2_code\02_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_work\4_python\2_code\02_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“里面,变量必须放在{}内部,否则程序报错,即这种格式的代码是不允许的。

猜你喜欢

转载自www.cnblogs.com/luoxun/p/13190570.html