与孩子一起学编程05章

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

越来越有意思了,嗯最近学的时间不是很有规律,呵呵

以后尽量多挤一点时间学学。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值