我在Python Jupyter笔记本中的代码有什么问题?
当我运行代码并输入“1”或“2”时,它会给出响应 抱歉,我们不了解您的选择 我该如何解决这个问题,以便它能给我正确的答案 我试着把“1”和“2”做成一个字符串我在Python Jupyter笔记本中的代码有什么问题?,python,jupyter,Python,Jupyter,当我运行代码并输入“1”或“2”时,它会给出响应 抱歉,我们不了解您的选择 我该如何解决这个问题,以便它能给我正确的答案 我试着把“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 custom
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\u customer()
或existing\u customer()
尝试将response
转换为string,然后进行比较
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()
或:
示例:
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'>
输出:
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
蟒蛇2还是蟒蛇3?同时输入
1
,而不是“1”
检查响应类型。你会得到答案的。你试过让1
和2
不是字符串吗?(比如,如果response==1:
)您的代码在终端和服务器上都运行良好jupyter@Jeril因为您使用的是python 3。Python2有input
和raw\u input
概念我在使用Python3你输入的东西像带引号的input 1,像“1”@DaRidgok thx有效,我也有一个非常类似的问题:
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