使用if语句条件查看NULL用户输入并打印:"sometext ..."

suf*_*ffa 0 python if-statement while-loop

Python的缩进,没有分号和括号,花了一些时间让我习惯......来自C++.这个,我肯定是一个简单的修复,但我似乎无法找到问题.非常感谢您的帮助.这就是我所拥有的.当我运行此代码时,它就像第二个'if'语句不存在一样.即使我注释掉第一个'if'语句,第二个'if'语句中的打印行也永远不会打印到屏幕上:

import re


while True:
   stringName = raw_input("Convert string to hex & ascii(type stop to quit): ").strip()
   if stringName == 'stop':
      break
   if stringName is None: print "You must enter some text to proceed!"

   print "Hex value: ", stringName.encode('hex')
   print "ASCII value: ", ', '.join(str(ord(c)) for c in stringName) 
Run Code Online (Sandbox Code Playgroud)

Sve*_*ach 9

返回值raw_input()始终是字符串,永远不会None.如果要检查空字符串"",可以使用

if not string_name:
    # whatever
Run Code Online (Sandbox Code Playgroud)