【问题标题】:How do I check if a string includes characters and space (empty character)?如何检查字符串是否包含字符和空格(空字符)?
【发布时间】: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


    【解决方案1】:

    您必须使用input() 函数更改行。我们将添加while loop 并在其下方添加input()。如果换行符和空格的输入拆分器与输入相同,则意味着input_string 是正确的,while loop 将中断。如果不是,用户将不得不输入一个不包含这些字符的字符串。

    while True:
        input_string = input('Enter the word(s) you would like displayed:\n')
        if ''.join(input_string.split()) == input_string:
            break
    

    【讨论】:

      【解决方案2】:

      我做replace():

      inp = input('Enter your text'),replace(' ','')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-25
        • 2021-08-17
        • 2021-06-12
        相关资源
        最近更新 更多