1.1.3 Python的输入和输出函数

raw_input(如果是使用Python 3.x,请转换为input())和print。
首先查看怎么使用
>>> help(raw_input)
Help on built-in function raw_input in module __builtin__:

raw_input(...)
raw_input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
On Unix, GNU readline is used if enabled. The prompt string, if given,
is printed without a trailing newline before reading.
测试:
>>> name=raw_input("你叫啥?")
你叫啥?ergou
>>> name
'ergou'
再试试print,不知道怎么用继续help()
>>> print "hello world"
hello world
>>> a="zhangsan"
>>> b="lisi"
>>> print a+b
zhangsanlisi
>>> print a,b
zhangsan lisi
根据上面的2个函数,写一个对话小程序:
#!/usr/bin/env python
# coding=utf-8
name = raw_input("What is your name?")
age = raw_input("How old are you?")

print "Your name is:"+name
print "You are "+ age + "years old"

after_ten = int(age) + 10
print "You will be "+ str(after_ten)+ "years old after ten years"
运行结果:
>>>
What is your name? zhangsan
How old are you? 29
Your name is:zhangsan
You are 29years old
You will be 39years old after ten years
标红的就是程序运行中输入的部分
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值