[英]How do I check if a string includes characters and space (empty character)?
I've created a "virtual signboard" program for fun.为了好玩,我创建了一个“虚拟招牌”程序。 I can get it to work if the input is only made out of characters using input_string.isalpha(), however if I input a string with a space or empty character (ex:hello world), it goes into "else" and doesn't run the get_letter function.
如果输入仅由使用 input_string.isalpha() 的字符组成,我可以让它工作,但是如果我输入一个带有空格或空字符的字符串(例如:hello world),它会进入“else”并且不会t 运行 get_letter function。
I've omitted parts from the get_letter function to save space but it goes from a to z.我已经从 get_letter function 中省略了部分以节省空间,但它从 a 到 z。
This is the program:这是程序:
import time
def get_letter(letters):
while True:
for i in letters:
if i.lower()=='a':
print(' * ')
time.sleep(0.1)
print(' ***')
time.sleep(0.1)
print('* *')
time.sleep(0.1)
print('* *')
time.sleep(0.1)
print('*****')
time.sleep(0.1)
print('* *')
time.sleep(0.1)
print('* *')
time.sleep(0.1)
print(' ')
time.sleep(0.1)
elif i.lower()=='b':
print('****')
time.sleep(0.1)
print('* *')
time.sleep(0.1)
print('* *')
time.sleep(0.1)
print('****')
time.sleep(0.1)
print('* *')
time.sleep(0.1)
print('* *')
time.sleep(0.1)
print('****')
time.sleep(0.1)
print(' ')
time.sleep(0.1)
[goes all the way to z]
elif i.lower() == ' ':
print('')
time.sleep(0.1)
print('')
time.sleep(0.1)
print('')
time.sleep(0.1)
print('')
time.sleep(0.1)
print('')
time.sleep(0.1)
print('')
time.sleep(0.1)
def instructions():
#instructions
print('Welcome to virtual signboard\n')
time.sleep(0.5)
print('Type in what you would like to display on your virtual signboard. Start and end with a space.')
time.sleep(0.5)
print('Ex:" hello world "')
time.sleep(0.5)
global input_string
input_string=input('Enter the word(s) you would like displayed:\n')
instructions()
try:
if input_string.isalpha(): #check if input is a to z characters
get_letter(input_string)
else:
print('\nOnly a-z characters are accepted')
instructions()
except KeyboardInterrupt: #to stop signboard press ctrl c
sys.exit()
You have to change the line with an input()
function.您必须使用
input()
function 更改该行。 We will add a while loop
and below it the input()
.我们将添加一个
while loop
,并在其下方添加input()
。 If input splitter from the newlines and spaces is the same as the input, it means that input_string
is proper and the while loop
will broke.如果换行符和空格的输入拆分器与输入相同,则表示
input_string
正确, while loop
将中断。 If it is not, the user will have to enter a string that does not contain those characters.如果不是,用户将不得不输入一个不包含这些字符的字符串。
while True:
input_string = input('Enter the word(s) you would like displayed:\n')
if ''.join(input_string.split()) == input_string:
break
Id do replace()
:我做
replace()
:
inp = input('Enter your text'),replace(' ','')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.