Zvight The Behemoth
Zvight The Behemoth

Reputation: 1

Python code does not work, while counter does not work

I am currently working on a project for class and we have come up with a problem. When we run the code, it usually crashes. I am guessing it runs infinitely. This program is also using Tkinter. Here is the code:

import tkinter as tk

from tkinter import *
# import the random module
import random``

# set control variables
guess = 0

counter = 0

number=random.randint(0,100)

themain=number

def myGuess():

    guess = float(enter1.get())
    counter= 10


    while guess != number:
        counter = counter - 1

        if guess == number:
            result = "Congratulations!"
            howmany= "You have ", counter, "Tries to spare"
            label2.config(text=result)
            label3.config(text=howmany)
        elif guess > number:
            result = "To high"
            howmany= "You have ", counter, "Tries left"
            label2.config(text=result)
            label3.config(text=howmany)
        elif guess < number:
            result = "To low"
            howmany= "You have ", counter, "Tries left"
            label2.config(text=result)
            label3.config(text=howmany)





root=tk.Tk()
root.title("Guessing Game")
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

label1 = tk.Label(root, text=' Enter value:')

enter1 = tk.Entry(root, bg='red')

btn1 = tk.Button(root, text=' Enter Number', command=myGuess)

label2 = tk.Label(root, text='')

label3 = tk.Label(root, text='')

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

label1.grid(row=0, column=0)

enter1.grid(row=0, column=1, padx=5, pady=5)

btn1.grid(row=2, column=0, pady=5)

label2.grid(row=2, column=1)

label3.grid(row=3, column=0)

I hope you guys can help! 

Upvotes: 0

Views: 85

Answers (3)

Svend Feldt
Svend Feldt

Reputation: 778

You should completely remove the while loop. - It makes no sense. The code runs "correct" without it and does what you need.

However you seem to need a check of the number of tries have been reached.

Upvotes: 0

Clark
Clark

Reputation: 21

For the below line, what is enter1 ?

  • guess = float(enter1.get())

Upvotes: 0

gior91
gior91

Reputation: 1775

It seems that you never change the value of guess. For crash we need more information like the traceback.

Upvotes: 1

Related Questions