[英]How to disable calculation in str()
def LastDigit(num):
last_digit = str(num)[-1]
return last_digit
If I input 1235-4578-9899-2231 (without quotation mark), the function will return the last digit.如果我输入 1235-4578-9899-2231(不带引号),function 将返回最后一位。 The trouble is that if I input without quotation mark, Python will do automatic subtraction, instead of treating my input as a string in a whole.
麻烦的是,如果我输入不带引号,Python 会做自动减法,而不是把我的输入当成一个整体。
This is not possible.这是不可能的。
1235-4578-9899-2231
is code featuring subtraction operators. 1235-4578-9899-2231
是具有减法运算符的代码。 The interpreter cannot know that you mean it to be a string without quote marks.解释器无法知道您的意思是它是一个没有引号的字符串。
You can fix it by converting your function argument which is num into string before calling the function.您可以通过在调用 function 之前将 num 的 function 参数转换为字符串来修复它。
def LastDigit(num):
last_digit = str(num)[-1]
return print(last_digit)
LastDigit("1235-4578-9899-2231")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.