本质上,我创建了ui,每件事似乎都很好,直到我按下按钮来调整我的排名。
from email.mime import image
import random
import os
from struct import pack
import webbrowser
import tkinter
# 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 >20:
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,97)
else:
rank = random.randint(98,100)
return(rank)
def calculate_ranking():
#read in entry values
like_count = int(like_entry.get())
comment_count = comments_entry.get()
share_count = share_entry.get()
posts = posts_entry.get()
#pass values into magic algorithm
#print result to result label
rank = str(magic_algorithm(like_count, share_count, posts,comment_count))
root = tkinter.Tk()
root.title("Main Tiktok Page")
root.geometry ("400x600")
likes_label = tkinter.Label(root, text="number of likes:")
likes_label.pack()
like_entry = tkinter.Entry(root)
like_entry.pack()
#shares
share_label = tkinter.Label(root, text="number of shares:")
share_label.pack()
share_entry = tkinter.Entry(root)
share_entry.pack()
#comments
comments_label = tkinter.Label(root, text="Comment:")
comments_label.pack()
comments_entry = tkinter.Entry(root)
comments_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
root.mainloop()但是,当我输入所有参数时,它会给我一个错误:
line 48, in calculate_ranking
rank = str(magic_algorithm(like_count, share_count, posts,comment_count))
, line 18, in magic_algorithm
**if share_count >= 10000 and like_count >= 5000 and comment_count > 200 and posts >20:
TypeError: '>=' not supported between instances of 'str' and 'int'**发布于 2022-05-04 17:29:12
错误信息应该会给你一个提示。这意味着您要将字符串与整数与>=进行比较。这意味着其中一个变量,比如comment_count是一个字符串。但它应该是个整数。
因此,改变这一点:
like_count = int(like_entry.get())
comment_count = comments_entry.get()
share_count = share_entry.get()
posts = posts_entry.get()对此:
like_count = int(like_entry.get())
comment_count = int(comments_entry.get())
share_count = int(share_entry.get())
posts = int(posts_entry.get())https://stackoverflow.com/questions/72117093
复制相似问题