aerodriver 发表于 2023-5-24 09:18:12

input函数在不同模式下的问题

在交互模式里面 temp = input("请数成绩") 就会报错
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAX0AAABjCAYAAAB30tAOAAAX80lEQVR4nO2dP4/jSHrGHxlOD4sxJnIkoR20gQYu8GUbWMIAXur6wt7ogHUgJYcGvAmFvo9wg1awPmCwCZUs9hzsdnh9Igw0qGSj3cs6UDANMXLiwQ4O/gDlgCypWKwqFskiRYnvDxBmmirWf76qeqtYz4AxxkAQJ8rg+wHY5/ZdWBe+bDy6+23iaSoPBGHD3x07AwRhYvD9oLE4XMQtww0/QXSVvz92BgjChDx6VhlU+Zo8WlaNwJscVZvirVsWgqgLGf0T4ePqT/hm/gHwf4Mv7y+OnZ1ybBYYTICI3WPMr/3fCtNwjvCTALt/e4On/x5h/jcPgbfG7Bf6qGQjaGu8VT8EMkUGt2gEr/relLfSZbGqsw0WSWXjfqyPiugvp2P04xWmozmuet6ZX1/+w7GzAMQ/4U+jH/A6+hKfFbVFvMJ0soQX7JAJ+osLXAEIM4GvcKEw+EXGV/zbxlDL4cvMAnTXy/ryxTUA27LY1dkYt4GH0WRAhp9QcjpGv+e8mv0WX86OnYuybLAYzRH6EdhsqA7yyQWGGOLiEwB/s4u1jJtGZWRtF1xdwo05/5enXcnlVFBnw9kaO0wxmixwLc6uCAI6ox+v8MfRA365W+NfNc+qszDE2RKv3mIJH5FyuDnE5Sfm+1WjXhs/uBief2/j53eJHLecH/Ff+T6Zw73FdcYZzu7gzyeYLK7BaLhPiDAN/xt47A5g30W6EO7CmNgFHgOg+fgsE23kZ7/3o+x3XsB2PIwXsN0uYF4mnh0LPDAv2GXT9QK2q5b9+kQh+wpfHT7+eynAz+xH7yv2bfAz+zn49hDO+5H9XCGMHP97/3Atc2/uE7LsnRHzkdRlXfCdtpuWvlf3t3hdlx6+Q+5jm07R9SZI+rD0jBC9x9wDI5/dAezON3QbV2GKSA20NorIlzp4YnT2hj9n7D3meT6LMvEmRj9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cH6Y+m8eoRRsMOmycm3b+Sg75ZmrlDv8vNnPHdcrpT+cAUxehDaE2Twip6MLpIuWoTQFtUmrJca3gBcCI81UuRQtlKtMPRv7jw2WfWzbsbPnXbapST+5VJ93gG09Z9w/A2DpOB9l02qrH7ZAdaM/fJOVKHxcllSXEohU4/ge79QpDVfq2jk0/l2mjHRlirGP1ZCFbIxzbFPLel4MgKU0yPMbylKttFz3w3aosXtnCNz5qbzeJqk4m/PSM7qsDWvblkU3uom3OA3ZOclQvOtIvcrUqeeXZ8Ars13Upo8NgSsIUpFdouE2bbXP29TzJhlptzLrqpFWI/2wHept2RxfA1giUUeyqDitLmsD2rZVGM6SX/mJuCMhPZ+86izG1e6dUqRTSfmlHxtt2zaoWs+8/9yV2dxu2cduAwBLaTdFDKykm45Wh5o2rUuZtnBR9sJ6To3j88vh60VT7p2KaTXZD9vA6PG3WcQLPE0YYdVb/KjWUvgOl6JFWvkjLq5Y7/CxWDTxpXTkxRfbeMSyNbl7R1V/uvT4Ytz+Iy+Yt7ioVFTPqnIVrcWpdu/o4sottkVSGE3fN9WhDaq8yOmVaVMtmkVF1aJoUVvso6xZdsZYYT3LaQS7pD7EPFetQzlM1bTkrmOTli5cy7t36ouorKbAww2wln/1zlVvlug0iwEA6nMEoaOee4drcX5N75ITx2CTdRPEq2Rqfk0GnyB0VDP6fIvT5Lkj5/IQ/WQMXD8etr+N5snuCLL5BKGFNHIJgiB6xP8D6wPxOrlV3FAAAAAASUVORK5CYII=

而在编辑器模式里面就可以运行 为什么呢?

isdkz 发表于 2023-5-24 09:18:35

在交互模式下,输入函数 input() 会等待用户输入,并将输入的内容作为字符串返回。但是交互模式下,用户输入的内容是没有办法通过标准输入流传递给程序的,因此会出现错误。

而在编辑器模式下,用户输入的内容可以通过标准输入流传递给程序,因此可以正常运行。

如果想在交互模式下使用 input() 函数,可以在命令行中执行 Python 脚本文件,或者使用 IDE 等工具来运行程序。

liuhongrun2022 发表于 2023-5-24 09:25:47

交互模式也不会报错吧

aerodriver 发表于 2023-5-24 09:56:54

isdkz2023-5-24 09:18
input()...

лл Β

aerodriver 发表于 2023-5-24 09:57:27

isdkz2023-5-24 09:18
input()...

лл

isdkz 发表于 2023-5-24 09:59:39

aerodriver 发表于 2023-5-24 09:56
лл Β

chatgpt瞎扯了,input在两个模式都没有区别

冷冻哈密瓜 发表于 2023-5-24 12:53:21

这是因为在交互模式和编辑器模式下,Python 解释器的工作方式不同。

在编辑器模式下,整个程序在执行之前就已经准备完毕,并且可以预先检测到所有变量的类型。因此,当您调用类似 input() 这样的函数时,Python 解释器可以根据函数返回值的类型来推断变量的类型,并将其赋给正确的变量名。

而在交互模式下,Python 解释器是一行一行地执行代码,并且无法预先知道将要输入的是什么类型的数据。因此,当您调用类似 input() 的函数时,Python 解释器无法推断应该将输入转换成什么数据类型。而 input() 函数默认返回字符串类型,所以您需要将返回的字符串类型转换为您希望的数据类型,例如:

python
temp = input("请输入成绩:")
score = float(temp)
在这个例子中,input() 函数返回的字符串类型被转换为浮点数类型,然后赋值给了 score 变量。

因此,在交互模式下,您需要手动确保变量类型的正确性,而在编辑器模式下,Python 解释器可以自动完成这个任务。

aerodriver 发表于 2023-5-24 14:13:59

2023-5-24 12:53
Python

...

thanks for the answer .
         temp = input()
temp
页: [1]
查看完整版本: input函数在不同模式下的问题