我遇到的问题是,我不知道是否有更好的方法来判断字符串是否可以在python中转换为int。
try:
tempScores = int(tempScores)
except ValueError:
tempScores = 10
发布于 2021-01-05 12:20:37
这是正确的。我只会在命名中使用下划线和单数(因为它是一个分数,而不是很多),根据Python惯例:temp_score
。
示例:
for temp_score in [1, '2', '3e2', '4a', 'b5']:
try:
temp_score = int(temp_score)
except ValueError:
temp_score = 10
print(temp_score)
输出:
1
2
10
10
10
还请参见:
https://stackoverflow.com/questions/65585958
复制相似问题