如何同时转换多个整数?
How to convert multiple integers at the same time?
我想改进这段代码的行数。具体来说,我将所有字符串值转换为整数的部分。我想将该行简化为更简单的行。
import sys
def main():
try:
A,B,C,D =input("Enter values A,B,C,D separated by a comma: ").split(",")
A=int(A) # These lines convert all the values into integers.
B=int(B)
C=int(C)
D=int(D)
print("Accepted values") if (A>0 and B>0 and C>0 and D>0 and A%2==0 and B>C and C>D and C+D > A+B) else print("Non-accepted values")
except ValueError:
sys.exit("Invalid input. You have not entered integer values correctly separated by a comma.\n Program ends.")
if __name__== "__main__" :
main()
您可以使用 built-in 函数 map
将函数(例如 int
)应用于序列中的每个项目。
a,b,c,d = map(int, input("Enter values: ").split(","))
您可以使用 for 循环来实现。
numbers = input("Please Enter A number seperated by comma").split(",")
for i in range(len(numbers)):
numbers[i] = int(numbers[i])
这应该给你正在寻找的东西:
A,B,C,D =list(map(int,input("Enter values A,B,C,D separated by a comma: ").split(",")))
A,B,C,D =int(input("Enter values A,B,C,D separated by a comma: ")).split(",")
你必须在输入之前添加 int...
那就解决了
我想改进这段代码的行数。具体来说,我将所有字符串值转换为整数的部分。我想将该行简化为更简单的行。
import sys
def main():
try:
A,B,C,D =input("Enter values A,B,C,D separated by a comma: ").split(",")
A=int(A) # These lines convert all the values into integers.
B=int(B)
C=int(C)
D=int(D)
print("Accepted values") if (A>0 and B>0 and C>0 and D>0 and A%2==0 and B>C and C>D and C+D > A+B) else print("Non-accepted values")
except ValueError:
sys.exit("Invalid input. You have not entered integer values correctly separated by a comma.\n Program ends.")
if __name__== "__main__" :
main()
您可以使用 built-in 函数 map
将函数(例如 int
)应用于序列中的每个项目。
a,b,c,d = map(int, input("Enter values: ").split(","))
您可以使用 for 循环来实现。
numbers = input("Please Enter A number seperated by comma").split(",")
for i in range(len(numbers)):
numbers[i] = int(numbers[i])
这应该给你正在寻找的东西:
A,B,C,D =list(map(int,input("Enter values A,B,C,D separated by a comma: ").split(",")))
A,B,C,D =int(input("Enter values A,B,C,D separated by a comma: ")).split(",")
你必须在输入之前添加 int...
那就解决了