接收用户输入的数据使用 input() 函数

  1. >>> a = input()
  2. 123
  3. >>> a
  4. 123
  5. >>> type(a)
  6. <type 'int'>
  7. >>> a = input()
  8. abc
  9. Traceback (most recent call last):
  10. File "<stdin>", line 1, in <module>
  11. File "<string>", line 1, in <module>
  12. NameError: name 'abc' is not defined
  13. >>> a = input()
  14. "abc"
  15. >>> a
  16. 'abc'
  17. >>> type(a)
  18. <type 'str'>
  19. >>> a = input()
  20. 1+3
  21. >>> a
  22. 4
  23. >>> a = input()
  24. "abc"+"def"
  25. >>> a
  26. 'abcdef'
  27. >>> a = input("请输入数量:")
  28. 请输入数量:100
  29. >>> a
  30. 100

input()接受表达式输入,并把表达式的结果赋值给等号左边的变量,input函数返回的类型是字符串

3. 练习题

从键盘上录入苹果的价格 、重量 ,输出: 苹果单价 9.00 元/⽄,购买了 5.00 ⽄,需要⽀付 45.00 元.