关键词

Python input()

input()函数有助于接受用户的输入,并以字符串格式返回输出。用户可以输入字符串或数字格式。

 **input([prompt])** #Where prompt is a string value

输入()参数:

接受单个参数。无论用户键入什么,它都将作为输入参数。

参数 描述 必需/可选
提示 写入标准输出(通常是屏幕)的字符串,不带尾随换行符 可选择的

输入()返回值

input()函数实际上是对输入字符串进行求值,并尝试将其作为 Python 代码运行,输出作为字符串返回。

| 投入 | 返回值 | | 线 | 通过移除尾部换行符来创建字符串 | | 如果电渗流 | 它会引发 EOFError 异常。 |

Python 中input()方法的示例

示例 1:输入()在 Python 中是如何工作的?

 # get input from user

inputString = input()

print('The inputted string is:', inputString) 

输出:

Python is interesting.
The inputted string is: Python is interesting 

示例 2:通过提示获取用户输入

 # get input from user

inputString = input('Enter a string:')

print('The inputted string is:', inputString) 

输出:

Enter a string: Python is interesting.
The inputted string is: Python is interesting 

示例 3:输入员工详细信息

 # take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company) 

输出:

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google 

示例 4:计算两个输入整数相加的程序

 # convert inout into int
first_number = int(input("Enter first number ")) sec second number "))

print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1) 

输出:

Enter first number 28
Enter second number 12

First Number: 28
Second Number: 12
Addition of two number is:  40 

本文链接:http://task.lmcjl.com/news/212.html

展开阅读全文
上一篇:Python hash() 下一篇:python id()