程式一定有錯但是我不知道在哪裡請各位大神幫幫我
import tkinter as tk
import random
def play():
rankc = int(rank_en.get())
if rankc == 1 :
easy(rank1)
if rankc == 2 :
normal(rank1)
if rankc == 3 :
hard(rank1)
def easy(rank):
n = random.randint(1,10)
a = 0
while True:
a = a+1
gue = gus_en.get()
if gue.isdigit():
gue = int(gue)
if gue > n:
print(gue,"太大了")
elif gue < n:
print(gue,"太小了")
else :
print("恭喜你猜對了,你猜了",a,'次')
break
def normal(rank):
n = random.randint(1,100)
a = 0
while True:
a = a+1
gue = gus_en.get()
if gue.isdigit():
gue = int(gue)
if gue > n:
print(gue,"太大了")
elif gue < n:
print(gue,"太小了")
else :
print("恭喜你猜對了,你猜了",a,'次')
break
def hard(rank):
n = random.randint(1,200)
a = 0
while True:
a = a+1
gue = gus_en.get()
if gue.isdigit():
gue = int(gue)
if gue > n:
print(gue,"太大了")
elif gue < n:
print(gue,"太小了")
else :
print("恭喜你猜對了,你猜了",a,'次')
break
rank1 = []
win = tk.Tk()
win.title('猜數字遊戲')
win.geometry('600x600')
win.configure(background ='gray' )
ha = tk.Label(win,text = '終極密碼',font = ('標楷體',30))
ha.pack()
rankf=tk.Frame(win)
rankf.pack()
rank = tk.Label(rankf,text = '選擇難度 1.簡單(1~10) 2.普通(1~100) 3.困難(1~200) :',font = ('標楷體',15))
rank.pack(side = tk.LEFT)
rank_en = tk.Entry(rankf)
rank_en.pack(side = tk.LEFT)
run = tk. Button(win)
run.pack()
p_button=tk.Button(win,text = '開始',command = play)
p_button.pack()
gus=tk.Frame(win)
gus.pack()
guse = tk.Label(gus,text = '猜的數字:',font = ('標楷體',15))
guse.pack(side = tk.LEFT)
gus_en = tk.Entry(gus)
gus_en.pack(side = tk.LEFT)
r_label = tk.Label(win)
r_label.pack()
win.mainloop()
必須在while迴圈中持續更新主式窗才不會造成式窗當掉。
更改後的程式碼如下。
import tkinter as tk
import random
def play():
rankc = int(rank_en.get())
if rankc == 1 :
easy(rank1)
if rankc == 2 :
normal(rank1)
if rankc == 3 :
hard(rank1)
def easy(rank):
n = random.randint(1,10)
a = 0
while True:
a = a+1
gue = gus_en.get()
if gue.isdigit():
gue = int(gue)
if gue > n:
print(gue,"太大了")
elif gue < n:
print(gue,"太小了")
else :
print("恭喜你猜對了,你猜了",a,'次')
break
win.update()
def normal(rank):
n = random.randint(1,100)
a = 0
while True:
a = a+1
gue = gus_en.get()
if gue.isdigit():
gue = int(gue)
if gue > n:
print(gue,"太大了")
elif gue < n:
print(gue,"太小了")
else :
print("恭喜你猜對了,你猜了",a,'次')
break
win.update()
def hard(rank):
n = random.randint(1,200)
a = 0
while True:
a = a+1
gue = gus_en.get()
if gue.isdigit():
gue = int(gue)
if gue > n:
print(gue,"太大了")
elif gue < n:
print(gue,"太小了")
else :
print("恭喜你猜對了,你猜了",a,'次')
break
win.update()
rank1 = []
win = tk.Tk()
win.title('猜數字遊戲')
win.geometry('600x600')
win.configure(background ='gray')
ha = tk.Label(win,text = '終極密碼',font = ('標楷體',30))
ha.pack()
rankf=tk.Frame(win)
rankf.pack()
rank = tk.Label(rankf,text = '選擇難度 1.簡單(1~10) 2.普通(1~100) 3.困難(1~200) :',font = ('標楷體',15))
rank.pack(side = tk.LEFT)
rank_en = tk.Entry(rankf)
rank_en.pack(side = tk.LEFT)
run = tk. Button(win)
run.pack()
p_button=tk.Button(win,text = '開始',command = play)
p_button.pack()
gus=tk.Frame(win)
gus.pack()
guse = tk.Label(gus,text = '猜的數字:',font = ('標楷體',15))
guse.pack(side = tk.LEFT)
gus_en = tk.Entry(gus)
gus_en.pack(side = tk.LEFT)
r_label = tk.Label(win)
r_label.pack()
win.mainloop()
不過你的while會在不斷的增加計數變數a
的值,
導致a
值遠大於猜測次數。
建議加個按鈕來送出猜測值。