输入一个数字,输出一个名字?

2024-05-05 17:34:19 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,我在学校有这个任务,我是一个完全的初学者,但我有它的大部分下来,我需要要求的数字,并让它输出相应的名称(它的“d”部分)我设法让C部分工作,但当我试图做同样的事情,以d它拒绝工作。我知道我可能做错了什么,但正如我所说,我是一个完全的初学者。你知道吗

另外,我是否可以尝试将我的c+d改为“if”,这样我就可以添加另一个“else”,这样如果输入的名称/编号不在任何列表中,它就会返回“invalid”,但我似乎无法将它们改为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])

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

 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,因为他们都在名单上


Tags: andnamenumberifnamesdisplayfindout
1条回答
网友
1楼 · 发布于 2024-05-05 17:34:19

这里有一些问题。首先,您需要将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

如果将检查索引放在该循环中,则会同时检查整数和有效值。你知道吗

相关问题 更多 >