程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Learning in Python input () function

編輯:Python

Python 中的 input() Functions make it very easy to get input from users of your application.The input function is like print functions are built-in,它的使用非常普遍.當你調用 input 函數()時,You can think of it as an expression,Its value is what the user entered at the prompt.This is always returned as a string,So if you want to use numbers as input,你將需要使用int()函數來處理.


input()函數

  • Receive a tip
  • 在屏幕上顯示提示
  • The user can enter a response to the prompt
  • Returns the user's input as a string

Username as input

在PythonA classic example of an input function is getting the name of the user of your application.So let's go ahead and do it,Now we can write some code,Prompt the user for their name,並將結果存儲在'name'變量中.我們開始吧.

The snippet above is actually a call to [Jupyter Notebook]的東西,It's a very interesting usePython編程的工具.By using in this exampleJupyter Notebook,It's very easy to see what's being calledinput()The prompt message when the function is used.We can enter some characters as our name in the prompt.然後,We can add code to print()這個變量.

We might want to make this output a little more interesting.在下面的Jupyter單元中,We can store some extra text with nameThe results in the variables are concatenated,to make the output more pleasant.


在循環中使用 input()

你可能會發現,You want to prompt the user for some information more than once.在這種情況下,You can use in a loop input 函數.The loop will continue to prompt the user for some information,until a certain condition is met.在下一個例子中,We're going to ask users what name they like.The logic of this is put in one[while循環]中.在while循環中,我們可以使用if-elsestructure to determine whether we want to print out the name or break out of the loop.


used in numbers input()

To make your program useful,You may need to accept user input in the form of numbers.讓我們看一個簡單的例子,它使用inputThe function is obtained from the user2個數字.The goal of this program is to record these two values,把它們加在一起,Then print out the result for the user.Let's try this now.

稍等一下.這裡發生了什麼?Our program requires the user to provide the first number.We entered numbers5.The program then asks the user to enter a second number.我們輸入了4的數值.The program then prints out a message:5和4的總和是54.This doesn't seem right,不是嗎?Let's try again,make a small adjustment.

int()和str()

Before we look at the code to fix the error in the above example,讓我們談談 Python Two new utility functions in .它們是 int() 函數和 str() 函數.These functions allow you to convert data types between integers and strings.You all need to do this,因為在PythonIt is quite possible to add the numbers together,而且在PythonIt is also possible to add strings together.What you can't do is add integers and strings to each other.So the next example is a refactoring of a program,同時使用int()和str()函數,This way we can get our desired output.


使用Loop、int()和str()的input()

在這個例子中,我們將在 Python 的 while 循環中使用 input 函數.The program will ask the user to provide a number,Or they can enter lettersQ來退出程序.When the condition in the loop is true,The program will convert the number entered by the user from decimal to binary format.for each number entered by the user,The program will print out the binary equivalent.


Show me a dog!

We have one more program to look at,It worked perfectlyinput()函數.This program will ask the user if he wants to see a dog.我的意思是,Who doesn't want to look at a dog,對嗎?So what we can do is let the user simply enter letters "y",If they want to watch the dogs,就輸入 "n".We'll put this inside a loop,This way you can keep watching the dog,Watch as long as you want.我們需要從IPython.displayadd some special modules,比如display和HTML,in order to keep the pictures in oursJupyterdisplayed in the notebook.Let's take a look at this action.


Python input() 函數總結

Python input() 是一個內置函數,Used to read strings from the user's keyboard.input() The function returns the string that the user typed into standard input.當你調用這個 input() 函數時,Python The interpreter waits for user input from the keyboard.When the user enters some data,Every character is echoed to the output.This facilitates user feedback as the value being entered.After you enter the value and pressReturn或Enter鍵後,直到最後一個Enter鍵為止,The string you entered is input()函數返回.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved