您现在的位置是:课程教程文章
python中如何获取用户输入
2023-12-18 23:28课程教程文章 人已围观
呆瓜半小时入门python数据分析
呆瓜半小时入门python数据分析呆瓜半小时入门python数据分析 课程主要内容 python数据分析,数据可视化实战...
2021软考
2021软考付费用户可以观看直到通过考试,而且以后每次考试后的真题解析视频也可以免...
微信小程序获取微信用户openid(含源代码
微信小程序获取微信用户openid(含源代码介绍如何获取微信小程序用户的openid 01-演示和使用说明 02-获取openid的坑 03-服务...
Python快速打造后台管理系统实战案例
Python快速打造后台管理系统实战案例课程购买咨询和资料获取请加老师微信 2020363447 课程背景: 企业里面很多系统...

整数输入
#!/usr/bin/python3
usr_ip = input("Enter an integer number: ")# 需要将输入的字符串显式地指定为需要的类型
usr_num = int(usr_ip)
sqr_num = usr_num * usr_num
print("Square of entered number is: {}".format(sqr_num))让我们给定一个整数和字符串来测定这个程序
Python docs - 整数文本
$ ./user_input_int.py Enter an integer number: 23 Square of entered number is: 529 $ ./user_input_int.py Enter an integer number: abc Traceback (most recent call last): File "./user_input_int.py", line 6, in <module> usr_num = int(usr_ip) ValueError: invalid literal for int() with base 10: 'abc'
浮点数输入
#!/usr/bin/python3
usr_ip = input("Enter a floating point number: ")
# 需要将输入的字符串显式地指定为我们需要的类型
usr_num = float(usr_ip)
sqr_num = usr_num * usr_num
# 限制小数点位数
print("Square of entered number is: {0:.2f}".format(sqr_num))E 科学计数法在需要时可以使用
Python文档 - 浮点数文本
Python文档 - 浮点数
$ ./user_input_float.py Enter a floating point number: 3.232 Square of entered number is: 10.45 $ ./user_input_float.py Enter a floating point number: 42.7e5 Square of entered number is: 18232900000000.00 $ ./user_input_float.py Enter a floating point number: abc Traceback (most recent call last): File "./user_input_float.py", line 6, in <module> usr_num = float(usr_ip) ValueError: could not convert string to float: 'abc'
字符串输入
#!/usr/bin/python3
usr_name = input("Hi there! What's your name? ")
usr_color = input("And your favorite color is? ")
print("{}, I like the {} color too".format(usr_name, usr_color))不像Perl,字符串输入不需要进行类型转换和注意换行符
$ ./user_input_str.py Hi there! What's your name? learnbyexample And your favorite color is? blue learnbyexample, I like the blue color too课程教程:python中如何获取用户输入
上一篇:一个例子解释python装饰器
下一篇:没有了