【发布时间】:2020-07-20 10:05:11
【问题描述】:
为了好玩,我创建了一个“虚拟招牌”程序。如果输入仅由使用 input_string.isalpha() 的字符组成,我可以让它工作,但是如果我输入一个带有空格或空字符的字符串(例如:hello world),它会进入“else”并且不会t 运行 get_letter 函数。
我从 get_letter 函数中省略了部分以节省空间,但它从 a 到 z。
这是程序:
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()
【问题讨论】:
标签: python string function loops for-loop