我正在尝试构建一个默认输入为空的函数,并将继续重复(如果用户输入任何内容,直到用户输入一个字符。问题是我被困在试图让输入问题重复出现在循环中。
#### Default
usr_int =("")
def str_analysis():
usr_int = (input("enter word or integer: "))
while (usr_int == ("")):
if usr_int.isdigit():
if (usr_int <= 99):
return (usr_int, "is a smaller number than expected")
elif (usr_int >= 100):
return (usr_int, "is a pretty big number")
elif usr_int.isalpha():
if usr_int.isdigit():
return (usr_int, "has multiple character types")
else:
return (usr_int, "is all alphabetical characters!")
else:
usr_int = (input("enter word or integer: "))
out_2 = str_analysis()
print(out_2)
答案 0 :(得分:0)
几个月前,我想做一些与你现在该做的事情相同的事情。
我找到了一个功能
# Required library import
import msvcrt as m
while True :
# The required function to use KeyButtonHIT
if m.kbhit() :
ch = m.getch().decode()
else :
ch = ''
# Rest of the code in a loop
您可以类似地使用kbhit函数进行递归。
答案 1 :(得分:0)
#### Default
usr_int =("")
def str_analysis():
usr_int = (input("enter word or integer: "))
while True:
if usr_int.isdigit():
if (int(usr_int) <= 99):
return (usr_int, "is a smaller number than expected")
elif (int(usr_int) >= 100):
return (usr_int, "is a pretty big number")
elif usr_int.isalpha():
if usr_int.isdigit():
return (usr_int, "has multiple character types")
else:
return (usr_int, "is all alphabetical characters!")
else:
usr_int = (input("enter word or integer: "))
out_2 = str_analysis()
print(out_2)