【问题标题】:Trying to make this python program work试图让这个 python 程序工作
【发布时间】:2014-03-26 15:58:20
【问题描述】:

这就是我想要做的。我在计算机上的一个特殊目录中创建了很多文件夹来隐藏东西。我有 4 级文件夹,每个文件夹的文件夹编号为 1 - 4。

例子:

1>1>1>1
1>1>1>2
...
1>2>1>1
...
4>1>1>1
...
4>4>4>4

我写了一个python程序来请求pin,然后打开pin对应的文件目录。 [例如.. pin#4322 将打开 4>3>2>2]。我遇到的唯一问题是我不能将输入限制为仅限数字 1 - 4,当我输入超出此范围的数字时,Internet Explorer 会打开(UGH!IE)。

这是代码.... (Python 2.7.6)

pin=str(raw_input("What is your 4-digit pin number? "))
intpin=int(pin)
#==============##==============#
pin1=pin[0:1]
pin2=pin[1:2]
pin3=pin[2:3]
pin4=pin[3:4]
#==============##==============#
ipin1=int(pin1)
ipin2=int(pin2)
ipin3=int(pin3)
ipin4=int(pin4)
#==============##==============#
print("")
print pin1
print pin2
print("")
path=("D:/Documents/Personal/"+pin1+"/"+pin2+"/"+pin3+"/"+pin4)
import webbrowser as wb
wb.open(path)
#==============##==============#
print("Thank You!")
print("Your window has opened, now please close this one...")

【问题讨论】:

    标签: python input range directory


    【解决方案1】:

    您可以测试输入以确保都是数字 1-4:

    bol = False
    while bol == False:
        pin=str(raw_input("What is your 4-digit pin number? "))
        for digit in pin:
            if int(digit) in [1,2,3,4]:
                bol = True
            else:
                print "invalid pin"
                bol = False
                break
    

    添加到代码开头的这个应该可以工作。你的代码肯定会更简洁,但这不是我纠正你的地方。

    【讨论】:

    • 没有必要做str(raw_input...,因为raw_input总是输出一个字符串类型,不管你输入一个数字。此外,此检查不会保护您免受五位或更多位数字的影响。我可以在这里输入4444444444,它仍然有效。
    • 我只是复制了 OP 的 pin 变量赋值行。而且长度的实现很简单,只要测试一下len(pin)=4。然而,这不是问题的一部分,只是限制在 range(1,5)。
    【解决方案2】:

    您可以使用正则表达式。正则表达式永远是好朋友。

    import re
    if not re.match("^([1-4])([1-4])([1-4])([1-4])$", pin):
            print "Well that's not your pin, is it?"
            import sys
            sys.exit()
    

    【讨论】:

    【解决方案3】:

    首先,raw_input 总是输出一个字符串,不管你输入一个数字。你不需要做str(raw_input...。证明如下:

    >>> d = raw_input("What is your 4-digit number? ")
    What is your 4-digit number? 5
    >>> print type(d)
    <type 'str'>
    

    其次,接受的答案不能保护您免受超过 4 位数的输入。即使是12341234 的输入也会被接受,因为它无法检查传入字符串的长度。

    这里的一种解决方法是不检查字符串,而是检查等效整数。你想要的范围只是[1111, 4444]。此时,可以使用assert,因此当他们输入低于或高于该值的任何内容时,您可以引发断言错误。但是,这样做的一个缺点是可以传入类似1111.4 的东西,它仍然满足包含检查(尽管由于 ValueError 导致转换失败)。

    考虑到上述情况,这是您的代码的另一种选择。

    def is_valid(strnum):
    
        # Returns false when inputs like 1.2 are given.
        try:
            intnum = int(strnum)
        except ValueError, e:
            return False
    
        # Check the integer equivalent if it's within the range.
        if 1111 <= intnum <= 4444:
            return True
        else: return False
    
    strnum = raw_input("What is your 4-digit PIN?\n>> ")
    # print is_valid(strnum)
    
    if is_valid:
        # Your code here...
    

    一些测试如下:

    # On decimal/float-type inputs.
    >>> 
    What is your 4-digit PIN?
    >> 1.2
    False
    >>> 
    # On below or above the range wanted.
    >>> 
    What is your 4-digit PIN?
    >> 5555
    False
    >>> 
    What is your 4-digit PIN?
    >> 1110
    False
    >>> 
    What is your 4-digit PIN?
    >> 1234
    True
    >>> 
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      • 1970-01-01
      相关资源
      最近更新 更多