python—用户交互
用户交互:
和使用者进行交流互动 input输入的字符均会被识别为字符串 需要将输入的内容进行一次类型转换
input()
# -*- coding: utf-8 -*-'''@File:用户交互.py@Author:HY @Version :1.0@Desc:input()输入'''name_input = ""print(type(name_input),name_input)name_input = input()print(type(name_input), name_input)"""@OutPut_1:hellohello""""""@OutPut_2:123123"""
# -*- coding: utf-8 -*-'''@File:用户交互.py@Author:HY @Version :1.0@Desc:如果输入的类型与预设不匹配打印"输入错误,重新输入"'''try:print("请输入名字")name_input = str(input())print("请输入年龄")age_input = int(input())except ValueError as i:print(i)print("输入错误,重新输入")"""@OutPut_1:请输入名字name请输入年龄12""""""@OutPut_2:请输入名字123请输入年龄23.3invalid literal for int() with base 10: '23.3'输入错误,重新输入"""
print()
# -*- coding: utf-8 -*-'''@File:用户交互.py@Author:HY @Version :1.0@Desc:print()输出'''text = "这是常见的输出函数 print"print(text)"""@OutPut:这是常见的输出函数 print"""
格式化输出
%s
较为通用的占位符
# -*- coding: utf-8 -*-'''@File:用户交互.py@Author:HY @Version :1.0@Desc:格式化输出%s在格式化输出的时候使用%s一定要注意按照顺序传参在输出的内容后边一定是直接使用%()进行衔接中间不需要逗号进行分割'''print("hello my name is %s,in the hotel %s room,have anything need you can tell of %s."% ("Nise", "211", "123456789"))print("hello my name is %s,in the hotel %s room,have anything need you can tell of %s."% ("211", "Nise", 123456789))"""@OutPut:hello my name is Nise,in the hotel 211 room,have anything need you can tell of 123456789.hello my name is Nise,in the hotel 211 room,have anything need you can tell of 123456789."""
# -*- coding: utf-8 -*-'''@File:用户交互.py@Author:HY @Version :1.0@Desc:使用字典进行传输参数就可以不用按照顺序进行传输参数直接使用key进行查找参数'''print("hello my name is %(name)s,in the hotel %(room)s room,have anything need you can tell of %(phone)s."% ({"name":"Nise", "phone":"211", "room":"123456789"}))"""@OutPut:hello my name is Nise,in the hotel 123456789 room,have anything need you can tell of 211."""
%d
只允许传输整数类型
# -*- coding: utf-8 -*-"""@File:用户交互.py@Author:HY @Version :1.0@Desc:格式化输出%d"""try:print("hello my name is %s,in the hotel %d room,have anything need you can tell of %s."% ("Nise", "211", "123456789")) except TypeError as e:print(e)print("hello my name is %s,in the hotel %s room,have anything need you can tell of %d."% ("211", "Nise", 123456789))"""@OutPut:%d format: a number is required, not strhello my name is 211,in the hotel Nise room,have anything need you can tell of 123456789."""
format()
format传输的值也都是字符串格式的类似于%s
# -*- coding: utf-8 -*-'''@File:用户交互.py@Author:HY @Version : 1.0@Desc:使用format的时候应该是在字符串的后面使用方法调用my_str.format()'''print("hello my name is {},in the hotel {} room,have anything need you can tell of {}.".format("Nise","211","123456789"))"""@OutPut:hello my name is Nise,in the hotel 211 room,have anything need you can tell of 123456789."""
# -*- coding: utf-8 -*-'''@File:用户交互.py@Author:HY @Version :1.0@Desc:使用format的时候应该是在字符串的后面使用方法调用my_str.format()'''print("hello my name is {1},in the hotel {0} room,have anything need you can tell of {1}.".format("Nise", "211"))print("hello my name is {1},in the hotel {0}{0}{0} room,have anything need you can tell of {1}.".format("Nise", "211"))"""@OutPut:hello my name is 211,in the hotel Nise room,have anything need you can tell of 211.hello my name is 211,in the hotel NiseNiseNise room,have anything need you can tell of 211."""
# -*- coding: utf-8 -*-'''@File:用户交互.py@Author:HY @Version :1.0@Desc:保留小数点两位'''a="{num:.2f}".format(num=12.245)print(a)"""@OutPut:12.24"""f# -*- coding: utf-8 -*-'''@File:用户交互.py@Author:HY @Version :1.0@Desc:None'''name = "nise"home = "towm"info = f"my name is {name},my home in {home}"print(info)"""@OutPut:my name is nise,my home in towm"""