数字猜测游戏GUI使用<elif>命令接收相同的结果Python / Tkinter

问题描述

运行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: ",you guessed to high")
                incorrectLabel.pack()

                tries += 1
                count -= 1

                break

输出

"C:\Program Files\python37\python.exe" "E:/Shanes Number Guessing Game (GUI).py"
August 31,2020 13:29:11
44
incorrect,you guessed to low
1
the number was: 44
44
incorrect,you guessed to low
1
the number was: 44

完整的完整代码

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",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,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()

解决方法

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

不正确

select id,[1] as hier1,[2] as hier2,[3] as hier3
from 
(
  select t1.hier,t1.lvl,t1.id
  from tblsample t1
) src
pivot
(
  max(hier)
  for lvl in ([1],[2],[3])
) piv;

正确

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

哇!最初,您的问题使我感到困惑,因为编写得如此出色的代码存在此错误,以至于每次运行它时,总是说您猜得太低了。您检查您的代码.....您在那里犯了一个愚蠢的错误,请参阅(而不是用userGuess编写了计数):

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,you guessed to high")
        incorrectLabel.pack()

        tries += 1
        count -= 1

        break
,

稍微改善了您的游戏:

fun ByteArray.skip(skipSize: Int): IntArray {
  val cleanedArr = IntArray(this.size / skipSize)
  var pointer = 0
  for (i in this.indices step skipSize) {
    cleanedArr[pointer] = this[i].toInt()
    pointer++
  }

  return cleanedArr
}