您可以使用输入来接受整数和字符串吗?
Can you use input to accept both an integer and a string?
我有一个小脚本,我一直在练习 Python。我无法让我的输入接受 if 语句的数字并接受小写字符串。
如果用户键入“99”然后关闭程序,我想告诉我的脚本任何输入。到目前为止,它在我有 int(input())
的地方工作,但它在我有 input()
的地方不起作用。我做错了什么还是我做不到?
现在我的 if
声明如下:
if choice1 == 99:
break
我是否应该通过引用将 99 变成一个字符串?
也许是这样的:
if choice1 == "99":
break
这是脚本:
global flip
flip = True
global prun
prun = False
def note_finder(word):
prun = True
while prun == True:
print ('\n','\n','Type one of the following keywords: ','\n','\n', keywords,)
choice2 = input('--> ').lower()
if choice2 == 'exit':
print ('Exiting Function')
prun = False
start_over(input)
elif choice2 == 99: # this is where the scrip doesnt work
break # for some reason it skips this elif
elif choice2 in notes:
print (notes[choice2],'\n')
else:
print ('\n',"Not a Keyword",'\n')
def start_over(word):
flip = True
while flip == True:
print ('# Type one of the following options:\n# 1 \n# Type "99" to exit the program')
choice1 = int(input('--> '))
if choice1 == 99:
break
elif choice1 < 1 or choice1 > 1:
print ("Not an option")
else:
flip = False
note_finder(input)
while flip == True:
print ('# Type one of the following options:\n# 1 \n# Type "99" to exit the program')
choice1 = int(input('--> '))
if choice1 == 99:
break
elif choice1 < 1 or choice1 > 1:
print ("Not an option")
else:
flip = False
note_finder(input)
所以input()
总是returns一个字符串。您可以在此处查看文档:
https://docs.python.org/3/library/functions.html#input
你可以这样做:
choice2 = input('--> ')
if choice2.isnumeric() and (int(choice2) == 99):
break
这可以避免您键入检查并捕获不重要的错误。
请参阅下文了解 isnumeric 如何处理不同的数字类型:
In [12]: a='1'
In [13]: a.isnumeric()
Out[13]: True
In [14]: a='1.0'
In [15]: a.isnumeric()
Out[15]: False
In [16]: a='a'
In [17]: a.isnumeric()
Out[17]: False
我有一个小脚本,我一直在练习 Python。我无法让我的输入接受 if 语句的数字并接受小写字符串。
如果用户键入“99”然后关闭程序,我想告诉我的脚本任何输入。到目前为止,它在我有 int(input())
的地方工作,但它在我有 input()
的地方不起作用。我做错了什么还是我做不到?
现在我的 if
声明如下:
if choice1 == 99:
break
我是否应该通过引用将 99 变成一个字符串?
也许是这样的:
if choice1 == "99":
break
这是脚本:
global flip
flip = True
global prun
prun = False
def note_finder(word):
prun = True
while prun == True:
print ('\n','\n','Type one of the following keywords: ','\n','\n', keywords,)
choice2 = input('--> ').lower()
if choice2 == 'exit':
print ('Exiting Function')
prun = False
start_over(input)
elif choice2 == 99: # this is where the scrip doesnt work
break # for some reason it skips this elif
elif choice2 in notes:
print (notes[choice2],'\n')
else:
print ('\n',"Not a Keyword",'\n')
def start_over(word):
flip = True
while flip == True:
print ('# Type one of the following options:\n# 1 \n# Type "99" to exit the program')
choice1 = int(input('--> '))
if choice1 == 99:
break
elif choice1 < 1 or choice1 > 1:
print ("Not an option")
else:
flip = False
note_finder(input)
while flip == True:
print ('# Type one of the following options:\n# 1 \n# Type "99" to exit the program')
choice1 = int(input('--> '))
if choice1 == 99:
break
elif choice1 < 1 or choice1 > 1:
print ("Not an option")
else:
flip = False
note_finder(input)
所以input()
总是returns一个字符串。您可以在此处查看文档:
https://docs.python.org/3/library/functions.html#input
你可以这样做:
choice2 = input('--> ')
if choice2.isnumeric() and (int(choice2) == 99):
break
这可以避免您键入检查并捕获不重要的错误。
请参阅下文了解 isnumeric 如何处理不同的数字类型:
In [12]: a='1'
In [13]: a.isnumeric()
Out[13]: True
In [14]: a='1.0'
In [15]: a.isnumeric()
Out[15]: False
In [16]: a='a'
In [17]: a.isnumeric()
Out[17]: False