如何在 Python 中的方法( .split(....) )中传递多个分隔符 [对于一个字符串中的两个或多个变量]
How to pass several separators in method ( .split(....) ) in Python [For two and more variables in one string]
# Task - input: amount of students for three classes ; output: amount of required desks
import math
First_Class, Second_Class, Third_Class = input("Enter amount of students for each class : ").split(sep=",")
Amount_Of_Desks = math.ceil(int(First_Class) / 2) + math.ceil(int(Second_Class) / 2) + math.ceil(int(Third_Class) / 2)
print(f" Sum amount of required desks is : {Amount_Of_Desks}")
我只想在一个字符串中使用两个分隔符
在调用拆分函数
之前,您可以使用 replace 函数将分隔符转换为一个分隔符
import math
First_Class, Second_Class, Third_Class = input("Enter amount of students for each class : ").replace(';',',').split(sep=",")
Amount_Of_Desks = math.ceil(int(First_Class) / 2) + math.ceil(int(Second_Class) / 2) + math.ceil(int(Third_Class) / 2)
print(f" Sum amount of required desks is : {Amount_Of_Desks}")
# Task - input: amount of students for three classes ; output: amount of required desks
import math
First_Class, Second_Class, Third_Class = input("Enter amount of students for each class : ").split(sep=",")
Amount_Of_Desks = math.ceil(int(First_Class) / 2) + math.ceil(int(Second_Class) / 2) + math.ceil(int(Third_Class) / 2)
print(f" Sum amount of required desks is : {Amount_Of_Desks}")
我只想在一个字符串中使用两个分隔符
在调用拆分函数
之前,您可以使用 replace 函数将分隔符转换为一个分隔符import math
First_Class, Second_Class, Third_Class = input("Enter amount of students for each class : ").replace(';',',').split(sep=",")
Amount_Of_Desks = math.ceil(int(First_Class) / 2) + math.ceil(int(Second_Class) / 2) + math.ceil(int(Third_Class) / 2)
print(f" Sum amount of required desks is : {Amount_Of_Desks}")