python - (猜数游戏 GUI)接收相同的结果命令(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: ", (rannum))
incorrectLabel = tk.Label(window, text="incorrect, 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", 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()
解决方案
我发现了我的问题,我的代码是正确的,但我的变量出错了
不正确
while not count == 0:
print(rannum)
if count== rannum:
正确的
while not count == 0:
print(rannum)
if userGuess == rannum:
推荐阅读
- variables - Haskell:用变量名替换字符
- json - 有没有办法在整个日志条目中搜索文本
- node.js - 反应测试:“TypeError:MutationObserver 不是构造函数”
- html - 表格行的边界半径和阴影
- windows - 什么是`powershell -c`或任何执行参数(例如powershell -nop -NonI)?
- python - 全新的python编码和一般编码
- xml - 如何在我的 RSS 中指定 HUB 以符合 WebSub?
- python - 将对象转换为字符串 Python 的最快方法
- build - 将未请求的代码添加到 index.html
- css - SCSS 变量未跨文件加载