day3---用户交互,input的应用

1.与用户交互

  • 输入:input()

    python2.x版本

    • input后面家的东西要声明输入的类型

              >>> input(">>:")
            >>:sean #不可以直接输入要输入的东西,
            Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
              File "<string>", line 1, in <module>
            NameError: name 'sean' is not defined
            >>> input(">>:")
            >>:"sean"   #属于字符串,加引号
            'sean'
            >>> input(">>:")
            >>:1 #输入的数字类型
            1
            >>> input(">>:")
            >>:[1,2]   #输入的列表
            [1, 2]
            >>>
                  >>> raw_input(">>:")
                 >>:sean
                 'sean'
                 >>> raw_input(">>:")
                 >>:12
                 '12'       #input()变成了raw_input()输入的效果和python3的效果一样
      • python3.x

         

              name = input("请输入您的名字:")  # 接收用户的输入,无论用户输入的是什么类型,最终返回的一定是字符串
             print(name)
             print(type(name))
  • 输出
    • 总结

      python2中 raw_input与python3中input作用相同

原文地址:https://www.cnblogs.com/lishuangjian/p/11782479.html