当前位置: 首页 > news >正文

交互:在终端中输入用户信息

交互:在终端中输入用户信息

Python可以允许用户在终端中输入一些信息。

Input功能

接受输入字符串

# -*- coding: utf-8 -*-
name = input("Please input your name:")
print("Hello " + name + "!")

其运行结果如下:

Please input your name:Cedar
Hello Cedar!

接受输入数字

age = input("How old are U:")
print("age " + age );
if age > 18:print("U are %d" % age)

其运行结果如下:

How old are U:33
age 33
Traceback (most recent call last):File "F:\python_test\input.py", line 8, in <module>if age > 18:^^^^^^^^
TypeError: '>' not supported between instances of 'str' and 'int'

从上面的结果来看,age 这个变量接收的输入应该是字符串,而不是数值类型。这个地方应该使用int()来将类型转换。

age = input("How old are U:")
print("age " + age );
age = int(age)
if age > 18:print("U are %d" % age)

其运行结果如下:

How old are U:33
age 33
U are 33
http://www.hskmm.com/?act=detail&tid=17229

相关文章:

  • 电脑迁移技巧:适用于 Windows 10/11 的免费磁盘克隆优秀的工具
  • Java学习日记9.18
  • 一种CDN动态加速首次访问加速方法
  • 9.25
  • 字典
  • CF1716题解
  • 使用vosk模型进行语音识别
  • AI Agent如何重塑人力资源管理?易路iBuilder平台实战案例深度解析
  • docker-compose + macvlan + Elasticsearch - 9.1.4 + Kibana - 9.1.4
  • WinForm 计时器 Timer 学习笔记
  • RocketMQ入门:基本概念、安装、本地部署与集群部署 - 详解
  • 【LeetCode】122. 买卖股票的最佳时机 II
  • VSCode 使用技巧笔记
  • 【LeetCode】55. 跳跃游戏
  • Ansible + Docker 部署 Apache Kafka 3.9 集群
  • 【LeetCode】45. 跳跃游戏 II
  • 深入了解一波JVM内存模型
  • 什么是UDFScript用户自定义脚本
  • 【LeetCode】121. 买卖股票的最佳时机
  • CCPC2024-Zhengzhou G Same Sum(线段树)
  • Openwrt-DDNS 配置详解
  • 实用指南:Metal - 2. 3D 模型深度解析
  • 【2025.9.16】关于举办PostgreSQL数据库管理人才研修与评测班的通知
  • Java锁相关问题
  • CDN中使用边缘函数实现自定义编程
  • 第一次课程中的所有动手动脑的问题以及课后实验性的问题
  • 敏捷开发的几个阶段
  • 隐藏在众目睽睽之下:从PEB中解除恶意DLL的链接
  • 设计模式六大原则 - 实践
  • 运营商 API 安全最佳实践、案例与方案推荐(2025)|千万级接口的全链路实战