我在Python Jupyter笔记本中的代码有什么问题?

2024-04-27 04:25:13 发布

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

当我运行代码并输入“1”或“2”时,它会给出响应

Sorry, we didn't understand your selection.

我如何解决这个问题,使它给我正确的答案?你知道吗

我试过把“1”和“2”串起来。你知道吗

def cs_service_bot():
    # Replace `pass` with your code
    response = input("Hello! Welcome to the DNS Cable Company's Service Portal. Are you a new or existing customer? \n [1] New Customer \n [2] Existing Customer \n Please enter the number corresponding to your choice:")
    if response == "1":
        return new_customer()
    elif response == "2":
        return existing_customer()
    else:
        print("Sorry, we didn't understand your selection.")
        return cs_service_bot()

cs_service_bot()

没有错误消息,它不会给出我正在寻找的答案,这些函数是:new_customer()还是existing_customer()


Tags: 答案newyourreturnresponsebotservicecustomer
2条回答

尝试将中的response转换为字符串,然后进行比较。你知道吗

def cs_service_bot():
    # Replace `pass` with your code
    response = input("Hello! Welcome to the DNS Cable Company's Service Portal. Are you a new or existing customer? \n [1] New Customer \n [2] Existing Customer \n Please enter the number corresponding to your choice:")
    if str(response) == "1":
        return new_customer()
    elif str(response) == "2":
        return existing_customer()
    else:
        print("Sorry, we didn't understand your selection.")
        return cs_service_bot()

cs_service_bot()

如果您使用的是python2,输入返回整数, 所以你需要:

response = input("Hello! Welcome to the DNS Cable Company's Service Portal. Are you a new or existing customer? \n [1] New Customer \n [2] Existing Customer \n Please enter the number corresponding to your choice:")
if response == 1:
    return new_customer()

或:

response = input("Hello! Welcome to the DNS Cable Company's Service Portal. Are you a new or existing customer? \n [1] New Customer \n [2] Existing Customer \n Please enter the number corresponding to your choice:")
if str(response) == "1":
    return new_customer()

示例:

Python 2.x
s1 = raw_input("Enter raw_input to test raw_input() function: ")
>> 3
print (type(s1))  <type 'str'>
s11 = raw_input("Enter raw_input to test raw_input() function: ")
>> a
print (type(s11))  <type 'str'>


s2 = input("Enter input to test input() function: ")
>> 3 
print (type(s2))  <type 'int'>
s22 = input("Enter input to test input() function: ")
>> a
print (type(s22))  <type 'str'>


Python 3.x
s3 = input("Enter input to test input() function: ")
>> 1
print (type(s3))  <type 'str'>
s33 = input("Enter input to test input() function: ")
>> a
print (type(s33))  <type 'str'>

编辑:

def new_customer():
    print ("new_customer")

def existing_customer():
    print ("existing_customer")

def cs_service_bot():
    # Replace `pass` with your code
    response = input("Hello! Welcome to the DNS Cable Company's Service Portal. Are you a new or existing customer? \n [1] New Customer \n [2] Existing Customer \n Please enter the number corresponding to your choice:")
    if response == "1":
        return new_customer()
    elif response == "2":
        return existing_customer()
    else:
        print("Sorry, we didn't understand your selection.")
        return cs_service_bot()

cs_service_bot()

输出:

Hello! Welcome to the DNS Cable Company's Service Portal. Are you a new or existing customer? 
 [1] New Customer 
 [2] Existing Customer 
 Please enter the number corresponding to your choice:1
new_customer

相关问题 更多 >