Developers often need to interact with users,to get data or provide some kind of result.Most programs today use dialog boxes to ask the user for some type of input.而 Python Provides us with two built-in functions to read keyboard input. \
input(): This function first takes input from the user and converts it to a string.The type of the returned object is always<type ‘str’>.It doesn't evaluate expressions,It just returns the complete statement as a string.例如,Python 提供了一個名為 input 的內置函數,它接受用戶的輸入.when the input function is called,It stops the program and waits for user input.當用戶按下回車鍵時,The program resumes and returns what the user entered.
語法:
inp = input('STATEMENT') Example:1. >>> name = input('What is your name?\n') # \n ---> newline ---> It causes a line break >>> What is your name? Ram >>> print(name) Ram # ---> comment in python
# Python 程序# 展示了 input() 的使用val = input("Enter your value: ")print(val)
輸出:
以字符串作為輸入:
name = input('你叫什麼名字?\n') # \n ---> newline ---> results in a newlineprint(name)
輸出:
你叫什麼名字?RamRam
The input function is in Python 中的工作原理: \
代碼:
# 在 Python A program that checks the input type in num = input ("Enter number :")print(num)name1 = input("Enter name : ")print(name1)# The print type of the input valueprint ("type of number", type(num))print ("type of name", type(name1))
輸出:
raw_input(): This function is for older versions(如 Python 2.x).This function gets exactly what was typed from the keyboard,將其轉換為字符串,Then return it to the variable where we want to store it.
例子:
# 顯示使用 raw_input() 的 Python 程序g = raw_input("輸入你的名字 :")print g
輸出:
輸入你的名字 :The sea embraces the sea>>>
這裡,g 是一個變量,It will get string value,Typed by the user during program execution.raw_input() Data entry to the function is terminated by the Enter key.我們也可以使用 raw_input() Enter numeric data.在這種情況下,We use type conversion.
感謝大家的閱讀,Let me know in the comments if you have any questions.I hope you can give me one點贊+收藏+評論 ,Your support is the driving force for Haihai to update!I will continue to share the front end later & Backend related expertise.