Python 中的输入函数:概念和示例 |
您所在的位置:网站首页 › python编写动画 › Python 中的输入函数:概念和示例 |
0
分享至
用微信扫码二维码 分享至好友和朋友圈 导读:在这篇文章中,我将通过一些通俗易懂的例子来解释 Python 的 input() 函数的基础知识。 本文字数:1906,阅读时长大约: 2分钟 在这篇文章中,我将通过一些通俗易懂的例子来解释 Python 的 input() 函数的基础知识。Python 是世界上最流行的编程语言之一,广泛应用于各种应用程序中。Python 中的一个基本概念是input()函数,它允许用户通过提供输入值与程序进行交互。 让我们看看input()函数是如何工作的,以及如何在 Python 程序中有效地使用它。 在开始之前,我希望你已经安装了 Python 并设置了一个基本的编辑器。如果没有,请参考我的以下指南: ◈ linux.cn Python Input 函数 Python 中的input()函数用于接受用户输入的字符串。它提示用户输入一个值并等待用户提供输入。然后将用户的输入作为字符串存储在变量中,稍后可以在程序中使用。 句法 input("your message to user") 当你运行input()函数时,它会向用户显示消息并 等待 输入。显示光标等待。当用户输入并按下回车键,input()函数就会读取用户的输入。该值存储在你指定的变量中。 让我们看几个例子。 示例 1:简单的 input() 函数用法 以下代码片段接受输入并显示带有附加字符串的输出: your_name = input("Enter your name:") print("Hello " + your_name) 输出: Enter your name:arindamHello arindam Python 输入函数:一个简单的例子 示例 2:以整数和浮点数作为输入 在使用input()函数时,你还可以在运行时使用int()或float()将输入转换。这是一个例子: no_of_books = int(input("Enter total books ordered:")) print ("Total number of books:", no_of_books) price_of_each_book = float(input("Enter unit price:")) print ("Total price:", no_of_books * price_of_each_book) 输出: Enter total books ordered:5 Total number of books: 5 Enter unit price:10.1 Total price: 50.5 示例 3:连接列表 你还可以使用其他函数(例如列表)来接受一组值并将它们转换为 Python 中的 docs.python.org。这是一个接受输入并将其转换为列表的示例。然后使用另一组值并附加到第一个列表: # 获取第一个列表的输入 list_1 = list(input("Enter numbers for list 1:")) # 获取第二个列表的输入 list_2 = list(input("Enter some letters for list 2:")) # 循环遍历第二个列表并添加到第一个列表 for j in list_2: list_1.append(j) # 打印修改后的第一个列表 print(list_1) 输出: Enter numbers for list 1:1234 Enter some letters for list 2:ABCD ['1', '2', '3', '4', 'A', 'B', 'C', 'D'] 总结 我希望这个简单的指南通过几个示例阐明了input()函数。对于简单的场景,它是一个强大的功能,可以从标准输入中接受值。 via: 作者: 选题: 译者: 校对: 本文由 原创编译, 荣誉推出 LCTT 译者 :geekpi 翻译: 1898.5 篇 贡献: 3437 天 2013-10-25 2023-03-24 https://linux.cn/lctt/geekpi 欢迎遵照 CC-BY-SA 协议规定转载, 如需转载,请在文章下留言 “ 转载:公众号名称”, 我们将为您添加白名单,授权“ 转载文章时可以修改”。 特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。 Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services. /阅读下一篇/ 返回网易首页 下载网易新闻客户端 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |