用pytho输入用户姓名地址年龄用print函数输出
时间: 2023-05-18 11:07:02 浏览: 142
当然可以,以下是代码示例:
name = input("请输入您的姓名:")
address = input("请输入您的地址:")
age = input("请输入您的年龄:")
print("您的姓名是:", name)
print("您的地址是:", address)
print("您的年龄是:", age)
相关问题
编写程序,实现输入用户姓名、年龄和地址,然后使用print()函数输出
编写一个程序,实现用户输入姓名、年龄和地址,并使用print()函数将这些信息输出。
```python
name = input("请输入您的姓名: ")
age = input("请输入您的年龄: ")
address = input("请输入您的地址: ")
print("姓名: ", name)
print("年龄: ", age)
print("地址: ", address)
```
运行程序时,用户首先会被提示输入他们的姓名,然后输入年龄和地址。然后,使用print()函数将收集到的信息输出到屏幕上。
例如:
```
请输入您的姓名: 张三
请输入您的年龄: 25
请输入您的地址: 北京市
姓名: 张三
年龄: 25
地址: 北京市
```
python 用print函数输出数据到txt
要使用Python的print函数将数据输出到txt文件中,可以按照以下步骤进行操作:
1. 打开txt文件并创建文件对象。
```python
file = open("output.txt", "w")
```
其中,"output.txt"是要输出的txt文件名。如果该文件不存在,将会创建新的txt文件。
2. 使用print函数将数据写入txt文件。
```python
print("Hello, World!", file=file)
print("This is a line of text.", file=file)
```
这里的print函数中,第一个参数是要输出的内容,第二个参数是文件对象,指定输出到哪个文件中。
3. 关闭文件。
```python
file.close()
```
完整代码如下:
```python
# 打开txt文件并创建文件对象
file = open("output.txt", "w")
# 使用print函数将数据写入txt文件
print("Hello, World!", file=file)
print("This is a line of text.", file=file)
# 关闭文件
file.close()
```
运行以上代码后,输出的内容将被写入到指定的txt文件中。