简体   繁体   中英

Tkinter while loop guess game

# Tkinter guessing game
from tkinter import *
import random


window = Tk()

# Bot Generator
bot = random.randint(1, 20+1)
print(bot)


def submit():
    tries = 5
    while tries >= 0:
        e1 = (int(guessE.get()))
        if e1 == bot:
            print("You win")
            break
            
        elif e1 != bot:
            print("Try again")
            tries -= 1
            break


def clear():
    guessE.delete(0, "end")
    
    
# Guess Label
guessL = Label(window, text="Guess a number (between 1 and 20}")
guessL.place(x=75, y=50)

# Guess Entry
guessE = Entry(window, width=25, bg="lightgrey")
guessE.place(x=95, y= 80)

# Submit Button
submitBtn = Button(window, width=10, height=2, text="Submit", command=submit)
submitBtn.place(x=85, y=115)

# Clear Button
clearBtn = Button(window, width=10, height=2, text="Clear", command=clear)
clearBtn.place(x=175, y=115)


window.geometry("350x350")
window.resizable(0, 0)
window.attributes("-topmost", True)
window.mainloop()

I am trying to create a guessing game with Tkinter, I have created all the entries and labels, the clear button is working. I am struggling with creating a while loop, I need the program to stop accepting submitted entries once 5 tries have been used. Any ideas on how I can solve this?

Like already mentioned by @Paul Rooney, i also think that a while loop is a bad design for this. However, here is a working example with a global variable and some minor changes (disabling entry and button when 0 tries are reached):

# Tkinter guessing game
from tkinter import *
import random


window = Tk()

# Bot Generator
bot = random.randint(1, 20+1)
print(bot)

# define outside of function to not overwrite each time
tries = 5


def submit():
    global tries  # to make tries a global variable
    while tries > 0:  # this matches your tries amount correctly
        e1 = (int(guessE.get()))
        if e1 == bot:
            print("You win")
            break
            
        elif e1 != bot:
            print("Try again")
            tries -= 1
            break

    # print status, disable the widgets
    if tries == 0:
        print("No more tries left")
        guessE.configure(state="disabled")
        submitBtn.configure(state="disabled")


def clear():
    guessE.delete(0, "end")
    
    
# Guess Label
guessL = Label(window, text="Guess a number (between 1 and 20}")
guessL.place(x=75, y=50)

# Guess Entry
guessE = Entry(window, width=25, bg="lightgrey")
guessE.place(x=95, y= 80)

# Submit Button
submitBtn = Button(window, width=10, height=2, text="Submit", command=submit)
submitBtn.place(x=85, y=115)

# Clear Button
clearBtn = Button(window, width=10, height=2, text="Clear", command=clear)
clearBtn.place(x=175, y=115)


window.geometry("350x350")
window.resizable(0, 0)
window.attributes("-topmost", True)
window.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM