两分钟了解python中的input函数


本文摘自php中文网,作者烟雨青岚,侵删。

两分钟了解python中的input函数

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

#函数input

1

2

3

4

message = input("Tell me something, and I will repeat it back to you: ")

print(message)

message1 = input()

print(message1)

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

1

2

3

prompt1="type someting"

prompt2=prompt1+": "+input()

print(prompt2)



1

2

3

4

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默认输入是字符,所以需要字符比较时应当进行类型转换

1

2

3

4

5

6

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!")


#求模运算符

1

2

3

4

5

6

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”。

1

2

input("What kind of car would you like to hire?")

print("Let me see if I can find you a Subaru")

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

1

2

3

4

5

6

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的整数倍。

1

2

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函数的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python正则表达式如何匹配字符串

介绍Python面向对象版学员管理系统

Python如何实现网络爬虫

Python处理csv文件实例详解

Python在循环引用中如何管理内存

web自动化测试(一)selenium 3使用系列问题集

Python学完基础学什么

Python怎么逐行读写txt文件

Python有double类型吗

实例分析Python跨文件全局变量的实现方法

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...