Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:只能在数字循环中将str(而不是“int”连接到str_Python - Fatal编程技术网

Python TypeError:只能在数字循环中将str(而不是“int”连接到str

Python TypeError:只能在数字循环中将str(而不是“int”连接到str,python,Python,我正在尝试编写一个机器人来对Discord执行自动操作。为此,我编写了以下代码: import clipboard import time numnum = input(" What do you want the delay from each next number? (in seconds) \n\t Please type it here: ") startingnum = input(" What do you want the sta

我正在尝试编写一个机器人来对Discord执行自动操作。为此,我编写了以下代码:

import clipboard
import time

numnum = input("  What do you want the delay from each next number? (in seconds) \n\t    Please type it here:    ")
startingnum = input("   What do you want the starting number to be? \n\t    Please type it here:    ")
for i in range startingnum , 99999999999999):
    print('Your clip Board is set to ' + i )
    clipboard.copy(i)
    time.sleep(numnum)
print('terminal will close in 5 seconds')
time.sleep(5)
但是,此代码会生成以下错误:

TypeError: can only concatenate str (not "int") to str

我不知道为什么会发生这个错误,有人能告诉我为什么会在我的代码中发生这个错误吗?

这是一个非常直接的语法问题 您正在尝试连接字符串
“您的剪贴板…”
和整数
i

为了正确连接,请将整数类型转换为字符串

for i in range (startingnum , 100):
    #typecast int --> str using the built in str() function
    print('Your clip Board is set to ' + str(i) ) 
将i转换为字符串

import clipboard
import time

numnum = input("  What do you want the delay from each next number? (in seconds) \n\t    Please type it here:    ")
startingnum = input("   What do you want the starting number to be? \n\t    Please type it here:    ")
for i in range startingnum , 99999999999999):
    print('Your clip Board is set to ' + str(i) )
    clipboard.copy(i)
    time.sleep(numnum)
print('terminal will close in 5 seconds')
time.sleep(5)

以下是您需要进行的两项更改:

  • 当您请求用户输入时,numnum和startingnum将存储字符串值,而不是int值。因此,使用int(input(…)作为整数类型的输入
  • 组合字符串和int时,请使用逗号(,)而不是+
  • 以下是代码片段:

    import clipboard
    import time
    
    numnum = int(input("  What do you want the delay from each next number? (in seconds) \n\t    Please type it here:    ")) #Change 1
    startingnum = int(input("   What do you want the starting number to be? \n\t    Please type it here:    "))
    for i in range(startingnum , 99999999999999):
        print('Your clip Board is set to ' , i ) #Change2
        clipboard.copy(i)
        time.sleep(numnum)
    print('terminal will close in 5 seconds')
    time.sleep(5)
    

    TypeError:只能将str(而不是“int”)连接到str
    您能解释一下这个句子中您不理解的特定单词吗?请删除问题中不必要的句子。并且清楚地告诉你错误发生的地方。当你在网上查找错误信息时,从你找到的许多参考资料中,你不明白什么?看到了吗
    numnum = input("  What do you want the delay from each next number? (in seconds) \n\t    Please type it here:    ")
    startingnum = input("   What do you want the starting number to be? \n\t Please type it here:    ")
    for i in range (startingnum , 99999999999999):
        print('Your clip Board is set to ' + str(i) )
        clipboard.copy(i)
        time.sleep(numnum)
    print('terminal will close in 5 seconds')
    time.sleep(5)