python内置了一个输入函数 raw_input(),
# 与孩子一起学编程05章
# 2013年8月21日16:03:04
# 学习raw_input()输入函数
print "Enter your name: "
name = raw_input()
print "Hi", name, "how are you today."
如果在print语句后加上一个“,”会怎样,试试看
print "Enter your name: "
name = raw_input()
print "Hi", name, "how are you today."
print "Enter your name: ",
name = raw_input()
print "Hi", name, "how are you today."
在python中编译执行后,是这样
Enter your name:
watt
Hi watt how are you today.
Enter your name: watt
Hi watt how are you today.
>>>
注意,逗号是在结束引号的后面。逗号可以把多个print语句合并到同一行上。
print "my",
print "name",
print "is",
print "watt."
编译后输出
>>>
my name is watt.
>>>
每个词后都会增加一个空格
还可以这样写
>>> name = raw_input("Enter your name is: ")
Enter your name is: watt
>>>
>>> fahr = float(raw_input())
88
>>>
使用raw_input()函数来做温度转换
print "This program converts Fahrenheit to Celsius"
print "Type in a temperature in Fahrenheit: ",
fahrenheit = float(raw_input())
celsius = (fahrenheit - 32) * 5.0 / 9
print "That is",
print celsius,
print "degrees Celsius"
编译后
>>>
This program converts Fahrenheit to Celsius
Type in a temperature in Fahrenheit: 78
That is 25.5555555556 degrees Celsius
>>>
结合int()使用raw_input()
respones = raw_input("How many students are in your class: ")
numberOfStudents = int(respones)
还可以打开一个互联网的文件
import urllib
file = urllib.urlopen('http://helloworldbook.com/data/message.txt')
message = file.read()
print message
越来越有意思了,嗯最近学的时间不是很有规律,呵呵
以后尽量多挤一点时间学学。