我正在创建一个练习python的简单脚本;它会计算名称中的字符,也可以检测连字符和空格;但我有一个问题 示例代码:
text = input('enter hyphen and space:')
if '-' in text:
print('hyphen in text')
elif ' ' in text:
print('space in text')
elif ' ' and '-' in text:
print('hyphen and space in text')
else:
print('Nothing :( ')
当用户在文本中同时写下空格和连字符时,它会显示hyphen in text
我怎样才能防止这种情况发生?
主要代码:
name = input('Enter your name:')
from time import sleep
if ' ' in name:
print('Looks like your name contain spaces')
sleep(0.6)
op1 = input('Count spaces in it? y/n\n')
sleep(0.6)
if op1.lower() == 'y':
name = len(name)
print('Numbers in your name: ',name)
if op1.lower() != 'y':
name = name.replace(' ','')
name = len(name)
print('Numbers in your name: ',name)
elif '-' in name:
print('Looks like your name contain hyphens ( - )')
sleep(0.6)
op = input('Count hyphens in it? y/n\n')
sleep(0.6)
if op.lower() == 'y':
name = len(name)
print('Numbers in your name: ',name)
if op.lower() != 'y':
name = name.replace('-','')
name = len(name)
print('Numbers in your name: ',name)
if '-' and ' ' in name:
print('Looks like your name contain both hyphen/s and space/s')
sleep(0.6)
print('options:\n')
sleep(0.8)
print('1) Count hyphen/s')
print('2) Count space/s')
print('3) Count both')
print('4) Skip both')
sleep(1)
op = input('1 / 2 / 3 \nEnter your choice:')
op = op.lower()
sleep(0.6)
if op == '1':
name = name.replace(' ','')
name = len(name)
print('Numbers in your name: ',name)
if op == '2':
name = name.replace('-','')
name = len(name)
print('Numbers in your name: ',name)
if op == '3':
print('Numbers in your name: ',name)
if op == '4':
name = name.replace('-','')
name = name.replace(' ','')
name = len(name)
print('Numbers in your name: ',name)
else:
name = len(name)
print('Numbers in your name: ',name)
答案 0 :(得分:2)
在顶部设置复杂条件,也不要忘记重复检查in text
,因为
' ' and '-' in text
如果True
中只有连字符,那么将为text
,因为' '
是True
- 就像字符串一样,你应该这样做:
' ' in text and '-' in text
最后我们可以有像
这样的东西if ' ' in text and '-' in text:
print('hyphen and space in text')
elif '-' in text:
print('hyphen in text')
elif ' ' in text:
print('space in text')
else:
print('Nothing :( ')
答案 1 :(得分:2)
当您的输入流入if语句时,将检查第一个条件。该条件为'-' in text
,如果True
将评估为text = "- "
,然后退出if语句。我们要找的是首先检查最具体的案例,即' ' and '-' in text
。所以正确的程序看起来像这样:
text = input('enter hyphen and space:')
if ' ' in text and '-' in text:
print('hyphen and space in text')
elif '-' in text:
print('hyphen in text')
elif ' ' in text:
print('space in text')
else:
print('Nothing :( ')
答案 2 :(得分:1)
条件语句的顺序是这里的问题,因为它总是会进入第一个if,即if '-' in text
,如示例中的连字符和空格都存在。此外,由于检查这两种情况的条件是elif
,因此永远不会执行。
所以你必须要有自己的病情,
" " and "_" in text
在第一个。
永远记住,如果,elif(可选),else按顺序工作,并且只根据你的条件执行块的一部分。
注意:如果任何非字符串输入像整数一样给出,则代码将抛出错误。所以我添加了raw_input
,你总是可以使用它将它转换为你想要的数据类型,甚至可以使用异常处理。
所以你的实际代码应该是这样的,
name = raw_input('Enter your name:') from time import sleep if '-' and ' ' in name: print('Looks like your name contain both hyphen/s and space/s') sleep(0.6) print('options:\n') sleep(0.8) print('1) Count hyphen/s') print('2) Count space/s') print('3) Count both') print('4) Skip both') sleep(1) op = input('1 / 2 / 3 \nEnter your choice:') op = op.lower() sleep(0.6) if op == '1': name = name.replace(' ','') name = len(name) print('Numbers in your name: ',name) if op == '2': name = name.replace('-','') name = len(name) print('Numbers in your name: ',name) if op == '3': print('Numbers in your name: ',name) if op == '4': name = name.replace('-','') name = name.replace(' ','') name = len(name) print('Numbers in your name: ',name) elif ' ' in name: print('Looks like your name contain spaces') sleep(0.6) op1 = input('Count spaces in it? y/n\n') sleep(0.6) if op1.lower() == 'y': name = len(name) print('Numbers in your name: ',name) if op1.lower() != 'y': name = name.replace(' ','') name = len(name) print('Numbers in your name: ',name) elif '-' in name: print('Looks like your name contain hyphens ( - )') sleep(0.6) op = input('Count hyphens in it? y/n\n') sleep(0.6) if op.lower() == 'y': name = len(name) print('Numbers in your name: ',name) if op.lower() != 'y': name = name.replace('-','') name = len(name) print('Numbers in your name: ',name) else: name = len(name) print('Numbers in your name: ',name)