类型错误:只能在数字循环中将 str(而不是 "int" )连接到 str



我正在尝试编写一个机器人程序,以便在Discord上执行自动操作。我已经写了以下代码来做到这一点:

import clipboard
import time
numnum = input("  What do you want the delay from each next number? (in seconds) nt    Please type it here:    ")
startingnum = input("   What do you want the starting number to be? nt    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

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

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

  1. 当您请求用户输入时,numnum和startingnum将存储字符串值,而不是int值。因此,使用int(input(…((作为整数类型的输入
  2. 将string和int组合时,请使用逗号(,(而不是+

以下是代码片段:

import clipboard
import time
numnum = int(input("  What do you want the delay from each next number? (in seconds) nt    Please type it here:    ")) #Change 1
startingnum = int(input("   What do you want the starting number to be? nt    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)

这是一个非常直接的语法问题您正在尝试连接字符串'Your clip Board...'和整数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) nt    Please type it here:    ")
startingnum = input("   What do you want the starting number to be? nt    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 = input("  What do you want the delay from each next number? (in seconds) nt    Please type it here:    ")
startingnum = input("   What do you want the starting number to be? nt 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)

相关内容

最新更新