Python 作为函数参数的输入
我真的很抱歉,我对这件事很陌生。我为一个“机器人”编写了一个脚本,在用户响应中添加一个,但我的代码没有运行。我是个新手,不知道是否有人能帮我理解我做错了什么Python 作为函数参数的输入,python,Python,我真的很抱歉,我对这件事很陌生。我为一个“机器人”编写了一个脚本,在用户响应中添加一个,但我的代码没有运行。我是个新手,不知道是否有人能帮我理解我做错了什么 import time print("hello, I am the addition bot, I add one to any number you know") time.sleep(0.7) print("Quick!") time.sleep(0.2) print("My boss is coming, please giv
import time
print("hello, I am the addition bot, I add one to any number you know")
time.sleep(0.7)
print("Quick!")
time.sleep(0.2)
print("My boss is coming, please give me a number!")
time.sleep(0.4)
print("Quick!")
time.sleep(1)
def addOneTo(number):
return number + 1
oldNumber = input()
print(addOneTo(oldNumber))
input()
返回字符串,需要将其转换为int
def addOneTo(number):
return int(number) + 1
您可能需要要求用户键入一个号码,即:
input("Please type a number: ")
当您说“但是我的代码没有运行”时,发生了什么?您预期会发生什么(请参阅)?另外,您使用的是哪种python版本?在请求调试帮助时,最好解释您正在尝试做什么以及您面临的问题。“我的代码没有运行”太笼统了。也就是说,
int(input())
是您的解决方案。我认为最好在输入后直接转换:int(input(…)
。但是这种转换只在python3中是必需的。对于python2,原始代码应按预期工作。