- Python Projects for Kids
- Jessica Ingrassellino
- 280字
- 2021-07-09 19:35:12
Users interacting with your program
We just built a function that adds two numbers together. Learning to make a program that does math is interesting, but our function is limited because our addition()
function needs to have variables changed manually to calculate results for different numbers.
What if there was a way to get information from the user and store THAT information in a variable so that it could be used by addition or subtraction functions each time? Anyone who has used a calculator of any kind already knows that this is possible. Python has a function called raw_input()
that allows us to tell the program to ask the user a question. The raw_input()
function is incredibly useful. We can get every kind of information from the user this way, and we can make interactions between the user and the computer based on the user input.
We can use the Python shell to test how the raw_input()
function works. Try typing these two lines of code into your Python shell:
name = raw_input('What is your name?') print(name)
What happened here? Let's have a look:
- What should have happened is that you should have had a prompt in the terminal that asked
'What is your name?'
Then, you can type your response. - Following your response, hit Enter. Nothing will happen (nothing should happen yet!).
- You have given the computer information (a memory) to store in the
name
variable, but now you need to get the information out of thename
variable. - You can get the output of the memory that the user entered by printing the
name
variable.
In this screenshot, you can see the entire sequence in the Python shell:

- Python程序設(shè)計(jì)教程(第2版)
- OpenCV實(shí)例精解
- 網(wǎng)絡(luò)爬蟲(chóng)原理與實(shí)踐:基于C#語(yǔ)言
- 自制編程語(yǔ)言
- Python編程從0到1(視頻教學(xué)版)
- NGINX Cookbook
- HTML5 APP開(kāi)發(fā)從入門(mén)到精通(微課精編版)
- Django實(shí)戰(zhàn):Python Web典型模塊與項(xiàng)目開(kāi)發(fā)
- JavaScript+jQuery網(wǎng)頁(yè)特效設(shè)計(jì)任務(wù)驅(qū)動(dòng)教程
- RocketMQ實(shí)戰(zhàn)與原理解析
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)(第二版)
- C#程序設(shè)計(jì)基礎(chǔ)入門(mén)教程
- Raspberry Pi Blueprints
- RESTful Web API Design with Node.js(Second Edition)
- 自己動(dòng)手構(gòu)建編程語(yǔ)言:如何設(shè)計(jì)編譯器、解釋器和DSL