'Inequality not supported between instances of 'str' and 'int'
So I basically made a algorithm which takes the users input and decided on a rank from 1-100 and puts it on a UI. THe problem here is the find function and when i run the code it and enter in a input it returns back with the error:
elif (share_count > 400 and like_count > 100 and posts >11 and comment_count.find("ok")!= -1):
TypeError: '>' not supported between instances of 'str' and 'int'
and
rank = tiktok_algorithm(like_count, comment_count, shares, posts)
import tkinter, os, webbrowser, random
#algorithm
def tiktok_algorithm(share_count, like_count, comment_count, posts):
if (share_count >= 10000 and like_count >= 5000 and posts > 20 and comment_count.find("amazing")!= -1):
rank = 1
elif (share_count > 7000 and like_count > 2000 and posts > 25 and comment_count.find("amazing")!= -1):
rank = random.randint(2,10)
elif (share_count > 6000 and like_count > 1500 and posts > 25 and comment_count.find("amazing")!= -1):
rank = random.randint(11,20)
elif (share_count > 5000 and like_count > 1300 and posts > 20 and comment_count.find("amazing")!= -1):
rank = random.randint(21,40)
elif (share_count > 4000 and like_count > 1000 and posts > 15 and comment_count.find("nice")!= -1):
rank = random.randint(41,55)
elif (share_count > 3000 and like_count > 500 and posts > 11 and comment_count.find("cool")!= -1):
rank = random.randint(56,70)
elif (share_count > 2000 and like_count > 300 and posts > 11 and comment_count.find("ok")!= -1):
rank = random.randint(71,80)
elif (share_count > 1000 and like_count > 150 and posts > 11 and comment_count.find("ok")!= -1):
rank = random.randint(81,90)
elif (share_count > 400 and like_count > 100 and posts > 11 and comment_count.find("ok")!= -1):
rank = random.randint(91,97)
else:
rank = random.randint(98,100)
return(rank)
def calculate_ranking():
# read in the entry values
like_count = int(likes_entry.get())
comment_count = (comment_entry.get())
posts = int(posts_entry.get())
shares = int(share_entry.get())
rank = tiktok_algorithm(like_count, comment_count, shares, posts)
# "print" result to result label
result_label.config(text="Your tiktok ranking is: " + str(rank))
root = tkinter.Tk()
root.title("Magic Tiktok Algorithm")
root.geometry("400x600")
# likes
likes_label = tkinter.Label(root, text="Number of Likes:")
likes_label.pack()
likes_entry = tkinter.Entry(root)
likes_entry.pack()
# comment
comment_label = tkinter.Label(root, text="Comment:")
comment_label.pack()
comment_entry = tkinter.Entry(root)
comment_entry.pack()
#shares
share_label = tkinter.Label(root, text="number of shares:")
share_label.pack()
share_entry = tkinter.Entry(root)
share_entry.pack()
#posts
posts_label = tkinter.Label(root, text="Posts")
posts_label.pack()
posts_entry = tkinter.Entry(root)
posts_entry.pack()
#image
img_path = os.path.dirname(__file__) + "\\tiktok-tricks-09.PNG"
tiktok_image = tkinter.PhotoImage(file=img_path)
resized_tiktok_img = tiktok_image.subsample(4,4)
image_label = tkinter.Label(root, image=resized_tiktok_img)
image_label.pack()
# rank button
rank_button = tkinter.Button(root, text="Get Ranking", command=calculate_ranking)
rank_button.pack()
# result
result_label = tkinter.Label(root, text = "Your tiktok ranking is:")
result_label.pack()
# keep the UI running
root.mainloop()
I keep getting this error, I have tried casting it to an integer, but it still doesn't work can anyone help.
Solution 1:[1]
You forgot int
keyword at function calculate_ranking
in line 30
at comment_count
variable:
def calculate_ranking():
like_count = int(likes_entry.get())
comment_count = (comment_entry.get())
posts = int(posts_entry.get())
shares = int(share_entry.get())
you should type:
def calculate_ranking():
like_count = int(likes_entry.get())
comment_count = int(comment_entry.get())
posts = int(posts_entry.get())
shares = int(share_entry.get())
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 | Shihab |