更清晰的代码来解决在 Python 中打印 Int 值
Cleaner code to get around Printing an Int value in Python
我有一段代码要求用户年龄。用户的输入类型为int
。此外,如果用户不想输入任何内容,他们可以按 'Enter' 退出程序(如下):
print("What is your age?")
answer = int(input())
if answer == '':
print("Don't want to disclose? That's fine.")
但是,当我按下 Enter 时,出现以下错误:
Traceback (most recent call last):
File "C:/Users/test/PycharmProjects/exceptions/testinput.py", line 2, in <module>
answer = int(input())
ValueError: invalid literal for int() with base 10: ''
Process finished with exit code 1
我知道我可以做一个 try 块并且代码像我想要的那样工作:
print("What is your age?")
try:
answer = int(input())
if answer == '':
print()
except ValueError:
print("Don't want to disclose? That's fine.")
我的问题是,除了 try
块之外,还有其他方法可以打印 int
类型的空白值吗?
当然:在转换为 int:
之前检查输入
answer = input("What is your age? ")
if answer == '':
print("Don't want to disclose? That's fine.")
else:
...
我会从 int
函数中删除 input
调用。
print("What is your age?")
answer = input()
answer = int(answer) if answer else None
if not answer: print("Don't want to disclose? That's fine.")
另一种方法是三思而后行,使用if-else
并在需要时在else
内进行int
转换:
answer = input("What is your age?")
if answer == "":
print("Don't want to disclose? That's fine.")
else:
answer = int(answer)
但是现在你需要考虑 answer
是否是构成字符串的有效整数,例如如果用户输入 foobar
怎么办? (提示: 现在您还需要使用 str.isdecimal
或类似方法进行检查)。
输入函数将输入转换为String
,如doc
中所述
input([prompt]) If the prompt argument is present, it is written to
standard output without a trailing newline. The function then reads a
line from input, converts it to a string (stripping a trailing
newline), and returns that. When EOF is read, EOFError is raised.
因此,当您什么都不输入时,您正在尝试将 int 类型转换为字符串 (''
),类似于 int('xyz')
,因此得到值错误异常
ValueError: invalid literal for int() with base 10: 'xyz'
如果数字包含在 String
类型中,例如int('2')
,其类型转换良好。
您或许可以这样来代替:
print("What is your age?")
answer = input()
if answer == '':
print("Don't want to disclose? That's fine.")
else:
int(answer)
我有一段代码要求用户年龄。用户的输入类型为int
。此外,如果用户不想输入任何内容,他们可以按 'Enter' 退出程序(如下):
print("What is your age?")
answer = int(input())
if answer == '':
print("Don't want to disclose? That's fine.")
但是,当我按下 Enter 时,出现以下错误:
Traceback (most recent call last):
File "C:/Users/test/PycharmProjects/exceptions/testinput.py", line 2, in <module>
answer = int(input())
ValueError: invalid literal for int() with base 10: ''
Process finished with exit code 1
我知道我可以做一个 try 块并且代码像我想要的那样工作:
print("What is your age?")
try:
answer = int(input())
if answer == '':
print()
except ValueError:
print("Don't want to disclose? That's fine.")
我的问题是,除了 try
块之外,还有其他方法可以打印 int
类型的空白值吗?
当然:在转换为 int:
之前检查输入answer = input("What is your age? ")
if answer == '':
print("Don't want to disclose? That's fine.")
else:
...
我会从 int
函数中删除 input
调用。
print("What is your age?")
answer = input()
answer = int(answer) if answer else None
if not answer: print("Don't want to disclose? That's fine.")
另一种方法是三思而后行,使用if-else
并在需要时在else
内进行int
转换:
answer = input("What is your age?")
if answer == "":
print("Don't want to disclose? That's fine.")
else:
answer = int(answer)
但是现在你需要考虑 answer
是否是构成字符串的有效整数,例如如果用户输入 foobar
怎么办? (提示: 现在您还需要使用 str.isdecimal
或类似方法进行检查)。
输入函数将输入转换为String
,如doc
input([prompt]) If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.
因此,当您什么都不输入时,您正在尝试将 int 类型转换为字符串 (''
),类似于 int('xyz')
,因此得到值错误异常
ValueError: invalid literal for int() with base 10: 'xyz'
如果数字包含在 String
类型中,例如int('2')
,其类型转换良好。
您或许可以这样来代替:
print("What is your age?")
answer = input()
if answer == '':
print("Don't want to disclose? That's fine.")
else:
int(answer)