打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Exercise 11: Asking Questions

Now it is time to pick up the pace. I have got you doing a lot of printing so that you get used to typing simple things, but those simple things are fairly boring. What we want to do now is get data into your programs. This is a little tricky because you have to learn to do two things that may not make sense right away, but trust me and do it anyway. It will make sense in a few exercises.

Most of what software does is the following:

  1. Take some kind of input from a person.
  2. Change it.
  3. Print out something to show how it changed.

So far you have only been printing, but you haven't been able to get any input from a person, or change it. You may not even know what "input" means, so rather than talk about it, let's have you do some and see if you get it. In the next exercise we'll do more to explain it.

123456789
print "How old are you?",age = raw_input()print "How tall are you?",height = raw_input()print "How much do you weigh?",weight = raw_input()print "So, you're %r old, %r tall and %r heavy." % (    age, height, weight)

Note

Notice that we put a , (comma) at the end of each print line.This is so that print doesn't end the line with a newline character and goto the next line.

What You Should See

$ python ex11.pyHow old are you? 38How tall are you? 6'2"How much do you weigh? 180lbsSo, you're '38' old, '6\'2"' tall and '180lbs' heavy.

Study Drills

  1. Go online and find out what Python's raw_input does.
  2. Can you find other ways to use it? Try some of the samples you find.
  3. Write another "form" like this to ask some other questions.
  4. Related to escape sequences, try to find out why the last line has '6\'2"' with that \' sequence. See how the single-quote needs to be escaped because otherwise it would end the string?

Common Student Questions

How do I get a number from someone so I can do math?
That's a little advanced, but try x = int(raw_input()) which gets the number as a string from raw_input() then converts it to an integer using int().
I put my height into raw input like this raw_input("6'2") but it doesn't work.
You don't put your height in there, you type it directly into your Terminal. First thing is, go back and make the code exactly like mine. Next, run the script, and when it pauses, type your height in at your keyboard. That's all there is to it.
Why do you have a newline on line 8 instead of putting it on one line?
That's so that the line is less than 80 characters long, which is a style that Python programmers like. You could put it on one line if you like.
What's the difference between input() and raw_input()?
The input() function will try to convert things you enter as if they were Python code, but it has security problems so you should avoid it.
When my strings print out there's a u in front of them, as in u'35'.
That's how Python tells you that the string is Unicode. Use a %s format instead and you'll see it printed like normal.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
sys.stdin.readline()和raw_input()的区别
Python input()和raw
Python2.x 和 Python3.x 中 raw_input( ) 和 input( ) 区别
Python完全新手教程 -- 2
Python 异常处理 Python 基础教程 try..except
python如何输入中文
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服