官术网_书友最值得收藏!

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 the name 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:

主站蜘蛛池模板: 潜山县| 新河县| 浦县| 太谷县| 阿坝县| 宣威市| 光山县| 岳阳县| 莲花县| 黔南| 凤山县| 遂平县| 临城县| 留坝县| 郁南县| 界首市| 正定县| 义马市| 凤庆县| 旬邑县| 太湖县| 阳江市| 长白| 克山县| 孟州市| 元江| 江山市| 衡南县| 八宿县| 永昌县| 乌兰县| 合水县| 视频| 和顺县| 新乡市| 蓬溪县| 韶关市| 江永县| 华安县| 洞头县| 永济市|