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
标红的就是程序运行中输入的部分