如何测试每个特定的数字或字符

时间:2015-04-18 21:01:29

标签: python string function if-statement input

我希望收到用户输入的 5位,然后为每个特定数字打印一些内容。

例如,如果用户输入12345,我想首先打印1的特定输出,然后输出2的另一输出,等等。

我该怎么做呢?如果可能的话,我更愿意创建一个函数。

#!/usr/bin/python3

zipcode = int(raw_input("Enter a zipcode: "))

if zipcode == 1:
       print ":::||"
elif zipcode == 2:
       print "::|:|"
elif zipcode == 3:
       print "::||:"
elif zipcode == 4:
       print ":|::|"
elif zipcode == 5:
       print ":|:|:"
elif zipcode == 6:
       print ":||::"
elif zipcode == 7:
       print "|:::|"
elif zipcode == 8:
       print "|::|:"
elif zipcode == 9:
       print "|:|::"
elif zipcode == 0:
       print "||:::"

5 个答案:

答案 0 :(得分:7)

您可以使用dictionary,然后遍历输入:

zipcode = raw_input("Enter a zipcode: ")

codes={1:":::||",2:"::|:|",3:"::||:",4:":|::|",5:":|:|:",6:":||::",7:"|:::|",8:"|::|:",9:"|:|::",0:"||:::"}

for num in zipcode:
    print codes[int(num)], #add a comma here if you want it on the same line

这会给你:

>>> 
Enter a zipcode: 54321
:|:|: :|::| ::||: ::|:| :::||

编辑:

没有空格:

zipcode = raw_input("Enter a zipcode: ")

codes={1:":::||",2:"::|:|",3:"::||:",4:":|::|",5:":|:|:",6:":||::",7:"|:::|",8:"|::|:",9:"|:|::",0:"||:::"}

L = [] #create a list

for num in zipcode:
    L.append(codes[int(num)]) #append the values to a list

print ''.join(L) #join them together and then print

现在打印出来了:

>>> 
Enter a zipcode: 54321
:|:|::|::|::||:::|:|:::||

答案 1 :(得分:6)

一个很好的解决方法

  • 将它们存储在tuple(而不是字典中,因为您的所有值都是顺序的,listtuple在这种情况下比通过键访问更好和价值观)

    list_bars = (":::||","::|:|",...)
    

    通过这种方式,您不需要众多ifelif内容

  • 不要将其转换为int将其保留为str本身。使用它可以迭代字符串而不是转换的数字。

最后在一个地方获取所有代码,

zipcode = raw_input("Enter a zipcode: ")
list_bars = (":::||","::|:|","::||:",":|::|",":|:|:",":||::","|:::|","|::|:","|:|::","||:::")
for i in zipcode:
    print(list_bars[int(i)-1])

现在进行一个小型演示

Enter a zipcode: 123
:::||
::|:|
::||:

使用timeit模块测试listtupledictionary之间的差异作为数据结构

bhargav@bhargav:~$ python -m timeit 'list_bars = [":::||","::|:|","::||:",":|::|",":|:|:",":||::","|:::|","|::|:","|:|::","||:::"]; [list_bars[int(i)-1] for i in "12345"]'
100000 loops, best of 3: 3.18 usec per loop
bhargav@bhargav:~$ python -m timeit 'list_bars={1:":::||",2:"::|:|",3:"::||:",4:":|::|",5:":|:|:",6:":||::",7:"|:::|",8:"|::|:",9:"|:|::",0:"||:::"}; [list_bars[int(i)] for i in "12345"]'
100000 loops, best of 3: 3.61 usec per loop
bhargav@bhargav:~$ python -m timeit 'list_bars = (":::||","::|:|","::||:",":|::|",":|:|:",":||::","|:::|","|::|:","|:|::","||:::"); [list_bars[int(i)-1] for i in "12345"]'
100000 loops, best of 3: 2.6 usec per loop

正如您所看到的,与其他人相比,tuple最快的

答案 2 :(得分:4)

将邮政编码保留为字符串,创建从输入到输出的映射:

def print_zip(zipcode):

    mapping = {
        '1': ':::||',
        '2': '::|:|',
        ...etc...
    }

    for char in zipcode:
        try:
            print mapping[char]
        except KeyError:
            print 'Oops, {} not valid in a zipcode!'.format(char)


zipcode = raw_input('Enter a zipcode: ')
print_zip(zipcode)

答案 3 :(得分:4)

您可以为此创建字典,然后使用.get

访问其元素
def print_for_zipcode():
    zipcode = raw_input("Enter a zipcode: ")
    relationship = {"1": ":::||",
        "2": "::|:|",
        "3": "::||:",
        "4": ":|::|",
        "5": ":|:|:",
        "6": ":||::",
        "7": "|:::|",
        "8": "|::|:",
        "9": "|:|::",
        "0": "||:::"}
    for ch in zipcode:
        print relationship.get(ch, "Not Found")

实际运行会像这样:

>>> print_for_zipcode()
Enter a zipcode: 123412
:::||
::|:|
::||:
:|::|
:::||
::|:|

答案 4 :(得分:3)

迭代然后对字符串中的每个项执行函数:

def something(zipcode):
    if zipcode == 1:
       print ":::||"
    elif zipcode == 2:
           print "::|:|"
    elif zipcode == 3:
           print "::||:"
    elif zipcode == 4:
           print ":|::|"
    elif zipcode == 5:
           print ":|:|:"
    elif zipcode == 6:
           print ":||::"
    elif zipcode == 7:
           print "|:::|"
    elif zipcode == 8:
           print "|::|:"
    elif zipcode == 9:
           print "|:|::"
    elif zipcode == 0:
           print "||:::"

for letter in raw_input():
    something(int(letter))