Python 类型错误:必须是 str,而不是 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40761501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:58:11  来源:igfitidea点击:

TypeError: must be str, not int

python

提问by user7080118

print ('welcome, uhhh whats your name?')
name = str(input('tell me here:    '))
print ('thats better')
import time
print ('welcome,',name,'this is the forst step on your long journey...')
time.sleep(0.5)
print ('well no, this is just a widget app...')
time.sleep(0.5)
print ('so umm just enter the widget you want')
print (' by the way this will repeat forever sooo, just kill the program when you have had enough')
while True:
    wid = str(input('choose from these(CAPS SENSITIVE): dice, usernamegen,   '))
    import random
    if wid == 'dice':
        print ('YOU HAVE CHOSEN...')
        print ('DICE!')
        dice = int(input('how many would you like to roll(MAX3):   '))
        if dice == 1:
            print ('you rolled a',(random.randint(1,6)))
        elif dice == 2:
            print ('you rolled a',(random.randint(1,6)))
            print ('you rolled a',(random.randint(1,6)))
        elif dice == 3:
            print ('you rolled a',(random.randint(1,6)))
            print ('you rolled a',(random.randint(1,6)))
            print ('you rolled a',(random.randint(1,6)))
        else:
            print ('next time enter an option')
    elif wid == 'usernamegen':
        print ('This app will generate your online username')
        lname = input('enter your last name here    ')
        print('wait 3 seconds')
        username = name[:1]+lname[:3]+random.randint(1,99999)
        time.sleep(3)
        print (username)

this was the code that I used but I got this error

这是我使用的代码,但出现此错误

  line 33, in <module>
    username = str(name[:1]+lname[:3]+random.randint(1,99999))
TypeError: must be str, not int

回答by Gaurav Dhama

name[:1]+lname[:3]+str(random.randint(1,99999))

Use it as mentioned above. This should work.

如上所述使用它。这应该有效。