简体   繁体   English

如何检查字符串是否包含字符和空格(空字符)?

[英]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.

相关问题 如何检查字符串是否包含 substring 和 Python 中的未知字符? - How to check if string includes substring with an unknown character in Python? 如何将字符与Python中某个字符串中的所有字符进行比较? - How do I compare a character to all the characters in some string in Python? 如何使用字符映射表替换字符串中的字符? - How do I replace characters in a string using a character map? 如何轻松检查字符串是否包含不应包含在其中的字符? - How do I easily check whether a string contains a character that should not be in there? 如何检查字符串中的字符是否为非字母? - How do I check if a character in a string is a non-letter? 如何检查一个或多个特定字符是否为字符串? - How do i check if one or more certain characters are an a string? 如何检查文件中的字符串是否包含某些字符? - How do I check if a string in a file contains certain characters? 如何检查字符串是否仅包含字母数字字符和短划线? - How do I check if a string only contains alphanumeric characters and dashes? 如果字符是空格,如何不加密? - How do I not encrypt a character if it is a space? 我如何使用正则表达式获取两个字符内的字符串并删除该字符串内的某些字符 - how do i use regex to get a string inside two character and remove certain characters inside that string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM