开发者

Code check program - Python

BRIEF: I am trying to write a simple program that checks if a given code is valid. The code is valid if all the three characters entered are digits between 0 and 9. If the code is valid, your progam displays a message Thank you. We will process your order!. The output of your program should be an error message The verification code was not valid. Please check your credit card code again. if the input is not valid.

ATTEMPT:

开发者_JAVA百科
lol = raw_input("what's your code?")
rofl=lol.split()
for int in rofl:
 if 9>int(rofl)>0:
   print "Thank you. We will process your order!"
 else:
   print 'The verification code was not valid. Please check your credit card code again.'

PURPOSE:

To learn - I found another way to do this involving just checking the whole thing for being under 1000 and over 0, and it worked perfectly. But I'm just trying to learn more.

REQUEST:

That you give me explicit direction on how to fix my code and provide any additional relevant advice.

CLOSE:

Thank you in advance.


What you're trying to do is to check if given input matches a given pattern (three digits in your case), that's basically what regular expressions are made for.

Using a regex, your code would look like this:

import re
code = raw_input("what's your code? ")
if re.match(r'^\d{3}$', code):
    print "Code OK"
else:
    print "Wrong Code!"

In the regex, ^ means "the beginning of the string", \d means "a digit" (it could also be [0-9]), {3} means "repeated 3 times", and $ means "the end of the string".

For more informations about regular expressions, have a look at the re module documentation.


you could:

a = raw_input()

if a.isdigit() and 0<int(a)<1000 and len(a)==3:
    print "yeah!"
else:
    print "Nope."

isdigit() is a cool built-in function that checks strings to see if they are made up entirely of digits! You don't have to wrap the int(a) in a try/except because 'and' acts as a short-circuit operator that will prevent int(a) from being evaluated if isdigit() returns false.

EDIT: added len(a)==3.


Your first problem is that .split() doesn't split the string into characters, it splits the string at whitespace.

"12 34 56".split() == ["12", "34", "56"]

  • To go through each character in the input string, don't use .split(), just use for character in string.
  • I find it hard to read code with variable names like lol and rofl, so I've changed this.
  • Your check of 9 > int(digit) > 0 excludes 9 and 0. I assume this is an accident, so I'm using 0 <= int(digit) <= 9 and including them instead.
  • Your current code prints an error message each time it sees an invalid character. Instead I'll break out of the loop after I print it the first time, and print the success message if the loop completes without being broken.
  • If int(digit) fails to convert digit to an int, it will raise an exception. Instead, I'll just compare the character itself to see if it's between "0" and "9", instead of converting it.
code = raw_input("what's your code?")

for digit in code:
    if not ("0" <= digit <= "9"):
        print "The verification code was not valid. Please check your credit card code again."
        break
else:
    print "Thank you. We will process your order!"

A compact alternative to using a loop is to use a generator expression with the any function:

code = raw_input("what's your code?")

if all("0" <= digit <= "9" for digit in code):
    print "Thank you. We will process your order!"
else:
    print "The verification code was not valid. Please check your credit card code again."


lol = raw_input("what's your code?")
rofl=lol.split()
# split() doesn't perform as you think.
#         it separates parts of string according to space, tab and line ends.

for int in rofl:
# this is a loop using 'int' as variable name, not a good idea at all 
#   as int is a standard type  
 if 9>int(rofl)>0:
# rofl is a list.
# if you don't redefine int, and use "for d in lol" instead to loop the digits
# then int(d) witll throw an exception if it's not a digit
   print "Thank you. We will process your order!"
 else:
   print 'The verification code was not valid. Please check your credit card code again.'
# You don't test there is exactly 3 digits.
# And you reply to your test in the loop, so once per item in the loop
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜