'Tiktok algo simulator
I am making a TikTok algorithm and here is the code it essentially takes the input of the user and determines what rank they are.
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))
but for some reason, I'm encountering an error that states:
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
Solution 1:[1]
This is because the rank is an int and to concatenate it you need to convert it to str.
One way is to :
return str(rank)
The second method is to use f-strings:
print(f"you rank on the platform is {magic_algorithm(share_count, like_count, comment_count, posts)}")
Solution 2:[2]
print("you rank on the platform is " + str(magic_algorithm(share_count, like_count, comment_count, posts)))
This works. You need to convert the int to str.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Abhyuday Vaish |
| Solution 2 | user2736738 |
