我正在制作一个TikTok算法,这是一个代码,它基本上接受用户的输入并确定它们的等级。
import random
share_count = int(input("How many shares did you get on your last tiktok "))
like_count = int(input("How many likes did you get on your last tiktok "))
comment_count = int(input("How many comments did you get on your last tiktok "))
posts = int(input("How many posts do you have on your tiktok page "))
def magic_algorithm(share_count, like_count, comment_count, posts):
if share_count >= 10000 and like_count > 5000 and comment_count > 200 and posts >25:
rank = 1
elif share_count >= 7000 and like_count > 2000 and comment_count > 60 and posts >25:
rank = random.randint(2,10)
elif share_count >= 6000 and like_count > 1500 and comment_count > 50 and posts >25:
rank = random.randint(11,20)
elif share_count >= 5000 and like_count > 1300 and comment_count > 33 and posts >20:
rank = random.randint(21,40)
elif share_count >= 4000 and like_count > 1000 and comment_count > 25 and posts >15:
rank = random.randint(41,55)
elif share_count >= 3000 and like_count > 500 and comment_count > 15 and posts >11:
rank = random.randint(56,70)
elif share_count >= 2000 and like_count > 300 and comment_count > 10 and posts >11:
rank = random.randint(71,80)
elif share_count >= 1000 and like_count > 150 and comment_count > 10 and posts >11:
rank = random.randint(81,90)
elif share_count >= 400 and like_count > 100 and comment_count > 5 and posts >11:
rank = random.randint(91,95)
else:
rank = random.randint(96,100)
return rank
print("you rank on the platform is " + magic_algorithm(share_count, like_count, comment_count, posts)) 但出于某种原因,我遇到了一个错误:
print("you rank on the platform is " + magic_algorithm(share_count, like_count, comment_count, posts))TypeError: can only concatenate str (not "int") to str发布于 2022-05-02 05:30:40
这是因为rank是一个int,为了连接它,您需要将它转换为str。
一种方法是:
return str(rank) 第二种方法是使用f-strings
print(f"you rank on the platform is {magic_algorithm(share_count, like_count, comment_count, posts)}")发布于 2022-05-02 05:26:27
print("you rank on the platform is " + str(magic_algorithm(share_count, like_count, comment_count, posts)))这个很管用。您需要将int转换为str。
https://stackoverflow.com/questions/72082622
复制相似问题