开发者

Using a if statement condition to see NULL user input and print: "sometext ..."

开发者 https://www.devze.com 2023-02-26 18:33 出处:网络
Python with its indents and no semi-colons and brackets is taking some time for me toget used to ... coming from C++. This, I\'m sure is an easy fix, but I can\'t seem to find the problem. Your help i

Python with its indents and no semi-colons and brackets is taking some time for me to get used to ... coming from C++. This, I'm sure is an easy fix, but I can't seem to find the problem. Your help is greatly appreciated. Here is what I have. When 开发者_Python百科I run this code it acts like the second 'if' statement does not exist. Even if I comment out the first 'if' statement, the print line in the second 'if' statement is never printed to screen:

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) 


The return value of raw_input() is always a string, and never None. If you want to check for an empty string "", you can use

if not string_name:
    # whatever


raw_input always returns a string and never None. Check out raw_input help.

raw_input([prompt]) -> string

Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading.

0

精彩评论

暂无评论...
验证码 换一张
取 消