输入一个号码并输出一个名字?

时间:2014-05-23 19:02:26

标签: python list python-3.x

好的,所以我在学校有这个任务,我是一个完全的初学者,但我大部分都失望了,我需要询问数字并输出相应的名称(它是“d”部分)我设法得到C部分工作,但当我尝试对D做同样的事情时,它拒绝工作。我知道我可能做错了但是我说我是一个完全的初学者。

另外,我可以尝试将我的c + d更改为“if”,以便我可以添加另一个“else”,这样如果输入的名称/号码不在任何列表中,它将返回“无效“但我似乎无法将它们更改为if语句。

无论如何这里是我正在努力工作的代码,就像我说的那样,C有效,但D拒绝:

flag="F"
choice=""

#Looping-----------------------------------------------------------------------

while choice != "F" and choice !="f":
    print( " A. Setup Name, Number and CallsPerDay \n"
           " B. Display all names and numbers \n"
           " C. Insert name to find out number \n"
           " D. Insert number and find out name \n"
           " E. Display Min, Max and Average CallsPerDay \n"
           " F. Finish the Program")

    choice=input("Select an option: \n\n")

#Selection---------------------------------------------------------------------

    if choice=="A" or choice =="a":
        if flag=="F":
            names=["gordon", "david", "graeme", "joyce", "douglas", "brian", "suzanne", "karen"]
            numb=[273429, 273666, 273512, 273999, 273123, 273224, 273324, 273424]
            CPD=[30, 10, 15, 2, 5, 1, 3, 6]
            length=len(numb)
            print("Names, Numbers and CallsPerDay have now been set up \n")
            flag="T"
        elif flag=="T":
            print("Lists already set up \n")

#---------------------------------------------------------------------------------

    elif choice=="B" or choice=="b":
        if flag=="F":
            print('Run option A first!')
        else:        
            for i in range(0,length,1):
                print(names[i],numb[i], CPD[i], "\n")

#-------------------------------------------------------------------------------

    elif choice=="C" or choice=="c":
        if flag=="F":
            print('Run option A first!')
        else:
            wanted=input('Name please ').lower()
            i=0
            while names[i] != wanted:
                i=i+1
            print('Number',numb[i])



#----------Part that refuses to work------------------------

    elif choice=="D" or choice=="d":
        if flag=="F":
            print('Run option A first!')
        else:
            wanted=input('Number Please: ')
            i=0
            while numb[i] != wanted:
                i=i+1
            print('Number',names[i])

以下是我在尝试执行此操作时遇到的错误:

 A. Setup Name, Number and CallsPerDay 
 B. Display all names and numbers 
 C. Insert name to find out number 
 D. Insert number and find out name 
 E. Display Min, Max and Average CallsPerDay 
 F. Finish the Program
Select an option: 

a
Names, Numbers and CallsPerDay have now been set up 

 A. Setup Name, Number and CallsPerDay 
 B. Display all names and numbers 
 C. Insert name to find out number 
 D. Insert number and find out name 
 E. Display Min, Max and Average CallsPerDay 
 F. Finish the Program
Select an option: 

d
Number Please: 223666
Traceback (most recent call last):
  File "G:\Lvl 5\sofware\menuNEWex - Copy.py", line 62, in <module>
    while numb[i] != wanted:
IndexError: list index out of range
>>> 

它应该输出David,因为它们在列表中都是#2

1 个答案:

答案 0 :(得分:2)

这里有一些问题。首先,您需要将wanted从字符串转换为整数,以便您的比较起作用:

# Not this, because the return of input is a string
wanted=input('Number Please: ')

# But this. Note this will throw a ValueError if converting to an int fails!
wanted = int(input('Number please: '))

# This one has error handling!
try:
    wanted = int(input('Number please: '))
except ValueError:
    print("That's not a number")

此外,如果您输入的号码不在列表numb中,则当i变得大于上一个索引时,您的循环仍会中断。请尝试使用index方法,因为它将返回数字的索引或抛出ValueError。但要小心 - 该索引是列表中元素的第一个索引。如果重复相同的数字,您将需要另一种处理冲突的方法:

try:
    i = numb.index(wanted)
    print('Number', names[i])
except ValueError:
    print("No such number")

您还应该考虑在查找有效值的while循环中包装输入请求。例如,您需要上述部分的数字:

i = None
while i is not None:
    try:
        i = int(input("Number: "))
    except ValueError:
        # i is still None
        print("Must enter a number!")

运行示例会为您提供:

Number: a
Must enter a number!
Number: b
Must enter a number!
Number: 33

如果在该循环中检查索引,则可以同时检查整数和有效值。