登录  /  注册

两分钟了解python中的input函数

烟雨青岚
发布: 2020-06-28 12:44:46
转载
7059人浏览过

两分钟了解python中的input函数

两分钟了解python中的input函数

input函数在python中是一个内建函数,其从标准输入中读入一个字符串,并自动忽略换行符。下面我们就来看看input函数的具体用法吧。

#函数input

message = input("Tell me something, and I will repeat it back to you: ")
print(message)
message1 = input()
print(message1)
登录后复制

a80b89c8ee5ca58d368b2cab3a582f9.png
#创建多行字符串的方式

prompt1="type someting"
prompt2=prompt1+": "+input()
print(prompt2)
登录后复制



prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "\nWhat is your first name? "
name = input(prompt)
print("\nHello, " + name + "!")
登录后复制


2b87a0465d8fbe9d74e467d3bffa9a3.png
#input默认输入是字符,所以需要字符比较时应当进行类型转换

age=input("How old are you?")
age=int(age)
if(age>=18):
print("You have the right to vote!")
else:
print("Sorry,you have no right to vote!")
登录后复制


#求模运算符

c1=input("Please input a number,and i will do modulo operation for it.")
c1=int(c1)
c2=input("Please input another number,and i will do modulo operation for it.")
c2=int(c2)
c3=c1%c2
print(c3)
登录后复制

7bd45fa05e1437abf5217b2bb2e66d4.png
#汽车租赁 :编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如“Let me see if I can find you a Subaru”。

input("What kind of car would you like to hire?")
print("Let me see if I can find you a Subaru")
登录后复制

49ed5824c340d6d77164714033aa327.png
#餐馆订位 :编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息,指出没有空桌;否则指出有空桌。

p1=input("How many people in the dinner?")
p1=int(p1)
if(p1>8):
print("Sorry,There is no empty table.")
else:
print("ok,There is an empty table")
登录后复制


#10的整数倍 :让用户输入一个数字,并指出这个数字是否是10的整数倍。

n1=input("Please input a number")n1=int(n1)if(n1%10==0):print(n1+" is an integer multiple of 10.")else:
print(n1+" is not an integer multiple of 10.")
登录后复制


input函数在python中是一个内建函数,其从标准输入中读入一个字符串,并自动忽略换行符。

也就是说所有形式的输入按字符串处理,如果想要得到其他类型的数据进行强制类型转化。默认情况下没有

提示字符串(prompt  string),在给定提示字符串下,会在读入标准输入前标准输出提示字符串。如果遇

文件结束符(end of file)会触发一个EOFError。

感谢大家的阅读,希望大家收益多多。

本文转自:https://blog.csdn.net/qq_24135817/article/details/79818479

推荐教程:《python教程

以上就是两分钟了解python中的input函数的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号