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

  • 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 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:

主站蜘蛛池模板: 九江县| 华亭县| 鄂尔多斯市| 石家庄市| 公主岭市| 兖州市| 八宿县| 精河县| 邛崃市| 阳东县| 萍乡市| 松江区| 青海省| 连城县| 顺平县| 黔西县| 枣阳市| 漠河县| 大英县| 天气| 海宁市| 北辰区| 淮北市| 察隅县| 东阳市| 蕲春县| 固始县| 洪雅县| 陇川县| 荣昌县| 兴宁市| 罗定市| 夏津县| 秦皇岛市| 景宁| 阿勒泰市| 蒙自县| 五莲县| 呼和浩特市| 巍山| 西充县|