(数字猜测游戏GUI)收到与<;elif>;命令(Python/Tkinter)

(数字猜测游戏GUI)收到与<;elif>;命令(Python/Tkinter),python,tkinter,Python,Tkinter,我在运行elif命令时收到了相同的结果 这是一个数字猜测游戏(GUI版本),游戏的目的是让用户输入1-100之间的数字。如果用户使用elif命令猜测从高到低,我将尝试显示Tk.Label 我在这篇文章中包括了我遇到问题的函数、输出和程序的完整代码 作用 def takeGuess(): global wins global losses global tries global diceResult global rannum count = dic

我在运行elif命令时收到了相同的结果

这是一个数字猜测游戏(GUI版本),游戏的目的是让用户输入1-100之间的数字。如果用户使用elif命令猜测从高到低,我将尝试显示Tk.Label

我在这篇文章中包括了我遇到问题的函数、输出和程序的完整代码

作用

def takeGuess():
    global wins
    global losses
    global tries
    global diceResult
    global rannum
    count = diceResult
    userGuess = int(enterGuess.get())
    while not count == 0:
        print(rannum)
        if userGuess == rannum:
            print("correct")
            wins += 1
            correctLabel = tk.Label(window,text="correct")
            correctLabel.pack()
            break

        elif count <= rannum:
                print("incorrect, you guessed to low")
                print(count - 1)
                print("the number was:", (rannum))
                incorrectLabel = tk.Label(window, text="incorrect, you guessed to low")
                incorrectLabel.pack()

                tries += 1
                count -= 1

                break

        elif count >= rannum:
                print("incorrect, you guessed to high")
                print(count - 1)
                print("the number was: ", (rannum))
                incorrectLabel = tk.Label(window, text="incorrect, you guessed to high")
                incorrectLabel.pack()

                tries += 1
                count -= 1

                break
完整的代码

import random
import tkinter as tk
import time
import sys
import datetime
import os


window = tk.Tk()
window.title("Shanes Number Guessing Game")
window.geometry("600x500")
window.configure(bg='#74eb34')

# GUI Image
#logo = tk.PhotoImage(file="C:\Python-Tkinter pics\\numberguess.png")
#photo1 = tk.Label(image=logo)
#photo1.image = logo
#photo1.pack()

# score
tries = 0
wins = 0

# user enters their username
userNameLabel = tk.Label(window, text="please enter your name below", bg='#74eb34', font='bold')
userNameEntry = tk.Entry(window)
name = str(userNameEntry.get())
userNameLabel.pack()
userNameEntry.pack()

def HiName():
    HiNameLabel = tk.Label(window,text="Gday " + str(userNameEntry.get()) + "\n" + "I want to play a game" + "\n" + "roll the dice to get a number" + "\n" + "The computer will then guess a number between 1-100" + "\n" + "your dice number determines how many guesses you get" + "\n" + "lets play" , bg='#74eb34')
    HiNameLabel.pack()

HiNameButton = tk.Button(window,text="record name", command=HiName)
HiNameButton.pack()

# User enters their guess in a entry box
enterGuessLabel = tk.Label(window, text="enter guess below", bg='#74eb34', font='bold')
enterGuessLabel.pack()
enterGuess = tk.Entry(window, text=0)
enterGuess.pack()

diceResult = random.randrange(1, 6)


# Throw dice
def throwDice():
    global diceResult
    global tries

    print(diceResult)
    diceLabel = tk.Label(window, text="the number of your dice is: " + str(diceResult),font="bold", bg='#74eb34')
    diceLabel.pack()
    tries += diceResult


def takeGuess():
    global wins
    global losses
    global tries
    global diceResult
    global rannum
    count = diceResult
    userGuess = int(enterGuess.get())
    while not count == 0:
        print(rannum)
        if userGuess == rannum:
            print("correct")
            wins += 1
            correctLabel = tk.Label(window,text="correct")
            correctLabel.pack()
            break

        elif count <= rannum:
                print("incorrect, you guessed to low")
                print(count - 1)
                print("the number was:", (rannum))
                incorrectLabel = tk.Label(window, text="incorrect, you guessed to low")
                incorrectLabel.pack()

                tries += 1
                count -= 1

                break

        elif count >= rannum:
                print("incorrect, you guessed to high")
                print(count - 1)
                print("the number was: ", (rannum))
                incorrectLabel = tk.Label(window, text="incorrect, you guessed to high")
                incorrectLabel.pack()

                tries += 1
                count -= 1

                break

    # GUI Buttons


diceButton = tk.Button(window, text="roll dice", command=throwDice)
diceButton.pack()

guessButton = tk.Button(window, text="take guess", command=takeGuess)
inputGuess = guessButton
guessButton.pack()

rannum = random.randrange(1, 100)

# Timestamp
timestamp = time.strftime("%B %d, %Y %H:%M:%S")
print(timestamp)


# open file
def file():
    os.system("statistics.txt")

def saveStats():
    with open("statistics.txt", "a") as output:
        output.write(str(userNameEntry.get()) + " played a game on: " + time.strftime("%H:%M:%S %Y-%m-%d") + " and got the results: " + "\n")


saveButton = tk.Button(window,text="Save Stats", command=saveStats)
saveButton.pack()

fileButton = tk.Button(window, text="open file", command=file)
fileButton.pack()

window.mainloop()
随机导入
将tkinter作为tk导入
导入时间
导入系统
导入日期时间
导入操作系统
window=tk.tk()
窗口标题(“Shanes猜数字游戏”)
窗几何(“600x500”)
window.configure(bg='#74eb34')
#图形用户界面图像
#logo=tk.PhotoImage(file=“C:\Python-Tkinter-pics\\numberguess.png”)
#照片1=商标标签(图像=商标)
#photo1.image=logo
#照片1.pack()
#得分
尝试=0
wins=0
#用户输入他们的用户名
usernamelab=tk.Label(窗口,text=“请在下面输入您的姓名”,bg='#74eb34',font='bold')
userNameEntry=tk.Entry(窗口)
name=str(userNameEntry.get())
usernamelab.pack()
userNameEntry.pack()
def HiName():
HiNameLabel=tk.Label(window,text=“Gday”+str(userNameEntry.get())+“\n”+”我想玩一个游戏“+”\n“+”掷骰子得到一个数字“+”\n“+”然后计算机会猜出一个介于1-100“+”\n“+”之间的数字。你的骰子数决定了你猜了多少次“+”\n“+”让我们玩吧,bg='.'74eb34'))
HiNameLabel.pack()
HiNameButton=tk.Button(窗口,text=“记录名”,命令=HiName)
HiNameButton.pack()
#用户在输入框中输入他们的猜测
enterGuessLabel=tk.Label(窗口,text=“在下面输入猜测”,bg='#74eb34',font='bold')
enterGuessLabel.pack()
enterGuess=tk.Entry(窗口,文本=0)
enterGuess.pack()
diceResult=random.randrange(1,6)
#掷骰子
def throwDice():
全局骰子结果
全球尝试
打印(结果)
diceLabel=tk.Label(窗口,text=“您的骰子编号为:”+str(diceResult),font=“bold”,bg=”#74eb34'))
diceLabel.pack()
尝试+=结果
def takeGuess():
全球胜利
全球损失
全球尝试
全局骰子结果
全局rannum
计数=结果
userGuess=int(enterGuess.get())
而不是计数==0:
打印(rannum)
如果userGuess==rannum:
打印(“正确”)
wins+=1
correctlab=tk.Label(窗口,text=“correct”)
correctLabel.pack()
打破
elif count=rannum:
打印(“不正确,您猜到高”)
打印(计数-1)
打印(“编号为:,(rannum))
incorrectLabel=tk.Label(窗口,text=“不正确,您猜到高”)
不正确的标签.pack()
尝试次数+=1
计数-=1
打破
#GUI按钮
diceButton=tk.Button(窗口,text=“掷骰子”,command=throwDice)
diceButton.pack()
guessButton=tk.Button(窗口,text=“takeGuess”,command=takeGuess)
inputGuess=猜测按钮
猜按钮。包()
rannum=random.randrange(1100)
#时间戳
timestamp=time.strftime(“%B%d,%Y%H:%M:%S”)
打印(时间戳)
#打开文件
def文件():
操作系统(“statistics.txt”)
def saveStats():
以open(“statistics.txt”、“a”)作为输出:
output.write(str(userNameEntry.get())+“在:“+time.strftime”(%H:%M:%S%Y-%M-%d”)+”上玩了一个游戏,得到了结果:“+”\n”)
saveButton=tk.Button(窗口,text=“Save Stats”,command=saveStats)
saveButton.pack()
fileButton=tk.Button(窗口,text=“打开文件”,命令=文件)
fileButton.pack()
window.mainloop()

我发现了我的问题,我的代码是正确的,但是我的变量出错了

不正确

while not count == 0:
    print(rannum)
    if count== rannum:
正确的

while not count == 0:
    print(rannum)
    if userGuess == rannum:

哇!一开始你的问题让我很困惑,因为写得这么好的代码有这个bug,所以每次运行它时,它总是说你猜得太低了。你检查你的代码。。。。。你犯了一个愚蠢的错误,请看(而不是userGuess你写了count):

elif userGuessrannum:
打印(“不正确,您猜到高”)
打印(计数-1)
打印(“编号为:,(rannum))
incorrectLabel=tk.Label(窗口,text=“不正确,您猜到高”)
不正确的标签.pack()
尝试次数+=1
计数-=1
打破

稍微改进了您的游戏:

import random
import tkinter as tk
import time
import sys
import datetime
import os


window = tk.Tk()
window.title("Shanes Number Guessing Game")
#window.geometry("600x500")
window.configure(bg='#74eb34')

# GUI Image
#logo = tk.PhotoImage(file="C:\Python-Tkinter pics\\numberguess.png")
#photo1 = tk.Label(image=logo)
#photo1.image = logo
#photo1.pack()

# score
tries = 0
wins = 0

# user enters their username
userNameLabel = tk.Label(window, text="please enter your name below", bg='#74eb34', font='bold')
userNameEntry = tk.Entry(window)
name = str(userNameEntry.get())
userNameLabel.pack()
userNameEntry.pack()

def HiName():
    HiNameLabel = tk.Label(window,text="Gday " + str(userNameEntry.get()) + "\n" + "I want to play a game" + "\n" + "roll the dice to get a number" + "\n" + "The computer will then guess a number between 1-100" + "\n" + "your dice number determines how many guesses you get" + "\n" + "lets play" , bg='#74eb34')
    HiNameLabel.pack()

HiNameButton = tk.Button(window,text="record name", command=HiName)
HiNameButton.pack()

# User enters their guess in a entry box
enterGuessLabel = tk.Label(window, text="enter guess below", bg='#74eb34', font='bold')
enterGuessLabel.pack()
enterGuess = tk.Entry(window, text=0)
enterGuess.pack()

diceResult = random.randrange(1, 6)


# Throw dice
def throwDice():
    global diceResult
    global tries

    print(diceResult)
    diceLabel = tk.Label(window, text="the number of your dice is: " + str(diceResult),font="bold", bg='#74eb34')
    diceLabel.pack()
    tries += diceResult


def takeGuess():
    global wins
    global losses
    global tries
    global diceResult
    global rannum
    count = diceResult
    userGuess = int(enterGuess.get())
    while not count == 0:
        print(rannum)
        if userGuess == rannum:
            rannum = random.randrange(1, 100)
            print("correct")
            wins += 1
            correctLabel = tk.Label(window,text="correct")
            correctLabel.pack()
            break

        elif userGuess < rannum:
                print("incorrect, you guessed to low")
                rannum = random.randrange(1, 100)
                print(count - 1)
                print("the number was:", (rannum))
                incorrectLabel = tk.Label(window, text="incorrect, you guessed to low ")
                lb = tk.Label(window, text = rannum).pack()
                incorrectLabel.pack()
                

                tries += 1
                count -= 1

                break

        elif userGuess > rannum:
        
                print("incorrect, you guessed to high")
                rannum = random.randrange(1, 100)
                print(count - 1)
                print("the number was: ", (rannum))
                incorrectLabel = tk.Label(window, text="incorrect, you guessed to high ")
                lbd = tk.Label(window, text = rannum).pack()
                incorrectLabel.pack()

                tries += 1
                count -= 1

                break

    # GUI Buttons


diceButton = tk.Button(window, text="roll dice", command=throwDice)
diceButton.pack()

guessButton = tk.Button(window, text="take guess", command=takeGuess)
inputGuess = guessButton
guessButton.pack()

rannum = random.randrange(1, 100)

# Timestamp
timestamp = time.strftime("%B %d, %Y %H:%M:%S")
print(timestamp)


# open file
def file():
    os.system("statistics.txt")

def saveStats():
    with open("statistics.txt", "a") as output:
        output.write(str(userNameEntry.get()) + " played a game on: " + time.strftime("%H:%M:%S %Y-%m-%d") + " and got the results: " + "\n")


saveButton = tk.Button(window,text="Save Stats", command=saveStats)
saveButton.pack()

fileButton = tk.Button(window, text="open file", command=file)
fileButton.pack()

window.mainloop()
随机导入
将tkinter作为tk导入
导入时间
导入系统
导入日期时间
导入操作系统
window=tk.tk()
窗口标题(“Shanes猜数字游戏”)
#窗几何(“600x500”)
window.configure(bg='#74eb34')
#图形用户界面图像
#logo=tk.PhotoImage(file=“C:\Python-Tkinter-pics\\numberguess.png”)
#照片1=商标标签(图像=商标)
#photo1.image=logo
#照片1.pack()
#得分
尝试=0
wins=0
#用户输入他们的用户名
usernamelab=tk.Label(窗口,text=“请在下面输入您的姓名”,bg='#74eb34',font='bold')
userNameEntry=tk.Entry(窗口)
name=str(userNameEntry.get())
usernamelab.pack()
userNameEntry.pack()
def HiName():
HiNameLabel=tk.Label(window,text=“Gday”+str(userNameEntry.get())+“\n”+”我想玩一个游戏“+”\n“+”掷骰子得到一个数字“+”\n“+”然后计算机会猜出一个介于1-100“+”\n“+”之间的数字。你的骰子数决定了你猜了多少次“+”\n“+”让我们玩吧,bg='.'74eb34'))
HiNameLabel.pack()
HiNameButton=tk.Button(窗口,text=“记录名”,命令=HiName)
HiNameButton.pack()
#用户在输入框中输入他们的猜测
enterGuessLabel=tk.Label(窗口,text=“在下面输入猜测”,bg='#74eb34',font='bold')
enterGuessLabel.pack()
enterGuess=tk.Entry(窗口,文本=0)
enterGuess.pack()
dic
elif  userGuess < rannum:
            print("incorrect, you guessed to low")
            print(count - 1)
            print("the number was:", (rannum))
            incorrectLabel = tk.Label(window, text="incorrect, you guessed to low")
            incorrectLabel.pack()

            tries += 1
            count -= 1

            break

elif  userGuess > rannum:

        print("incorrect, you guessed to high")
        print(count - 1)
        print("the number was: ", (rannum))
        incorrectLabel = tk.Label(window, text="incorrect, you guessed to high")
        incorrectLabel.pack()

        tries += 1
        count -= 1

        break
import random
import tkinter as tk
import time
import sys
import datetime
import os


window = tk.Tk()
window.title("Shanes Number Guessing Game")
#window.geometry("600x500")
window.configure(bg='#74eb34')

# GUI Image
#logo = tk.PhotoImage(file="C:\Python-Tkinter pics\\numberguess.png")
#photo1 = tk.Label(image=logo)
#photo1.image = logo
#photo1.pack()

# score
tries = 0
wins = 0

# user enters their username
userNameLabel = tk.Label(window, text="please enter your name below", bg='#74eb34', font='bold')
userNameEntry = tk.Entry(window)
name = str(userNameEntry.get())
userNameLabel.pack()
userNameEntry.pack()

def HiName():
    HiNameLabel = tk.Label(window,text="Gday " + str(userNameEntry.get()) + "\n" + "I want to play a game" + "\n" + "roll the dice to get a number" + "\n" + "The computer will then guess a number between 1-100" + "\n" + "your dice number determines how many guesses you get" + "\n" + "lets play" , bg='#74eb34')
    HiNameLabel.pack()

HiNameButton = tk.Button(window,text="record name", command=HiName)
HiNameButton.pack()

# User enters their guess in a entry box
enterGuessLabel = tk.Label(window, text="enter guess below", bg='#74eb34', font='bold')
enterGuessLabel.pack()
enterGuess = tk.Entry(window, text=0)
enterGuess.pack()

diceResult = random.randrange(1, 6)


# Throw dice
def throwDice():
    global diceResult
    global tries

    print(diceResult)
    diceLabel = tk.Label(window, text="the number of your dice is: " + str(diceResult),font="bold", bg='#74eb34')
    diceLabel.pack()
    tries += diceResult


def takeGuess():
    global wins
    global losses
    global tries
    global diceResult
    global rannum
    count = diceResult
    userGuess = int(enterGuess.get())
    while not count == 0:
        print(rannum)
        if userGuess == rannum:
            rannum = random.randrange(1, 100)
            print("correct")
            wins += 1
            correctLabel = tk.Label(window,text="correct")
            correctLabel.pack()
            break

        elif userGuess < rannum:
                print("incorrect, you guessed to low")
                rannum = random.randrange(1, 100)
                print(count - 1)
                print("the number was:", (rannum))
                incorrectLabel = tk.Label(window, text="incorrect, you guessed to low ")
                lb = tk.Label(window, text = rannum).pack()
                incorrectLabel.pack()
                

                tries += 1
                count -= 1

                break

        elif userGuess > rannum:
        
                print("incorrect, you guessed to high")
                rannum = random.randrange(1, 100)
                print(count - 1)
                print("the number was: ", (rannum))
                incorrectLabel = tk.Label(window, text="incorrect, you guessed to high ")
                lbd = tk.Label(window, text = rannum).pack()
                incorrectLabel.pack()

                tries += 1
                count -= 1

                break

    # GUI Buttons


diceButton = tk.Button(window, text="roll dice", command=throwDice)
diceButton.pack()

guessButton = tk.Button(window, text="take guess", command=takeGuess)
inputGuess = guessButton
guessButton.pack()

rannum = random.randrange(1, 100)

# Timestamp
timestamp = time.strftime("%B %d, %Y %H:%M:%S")
print(timestamp)


# open file
def file():
    os.system("statistics.txt")

def saveStats():
    with open("statistics.txt", "a") as output:
        output.write(str(userNameEntry.get()) + " played a game on: " + time.strftime("%H:%M:%S %Y-%m-%d") + " and got the results: " + "\n")


saveButton = tk.Button(window,text="Save Stats", command=saveStats)
saveButton.pack()

fileButton = tk.Button(window, text="open file", command=file)
fileButton.pack()

window.mainloop()