import random print(" Welcome to the guess my number") print("I'm thinking of a number between 1 and 50") print(" Welcome to the guess my number") print("try guessing in 10 attempts") num=random.randint(1,50) guess = int(input()) tries = 1 while guess != num and tries !=10: if tries ==3: print ("Do you even have a brain?") elif tries == 6: print ("Wow, you are such a Dumbass!") elif tries ==9: print ("Buh-bye!") if guess > num: print("lower") else: print ("higher") guess = int(input()) tries += 1 if num == guess: print ("You guessed it!, The number is ",num,"and it took you ",tries,"tries") else: print ("You have failed")
The first step is to import the GUI library you mean to use. Since a set of bindings for Tk is included with the Python standard library, that's probably the one you'll want to use; let us know if you want a different toolkit such as wxWindows or Qt.
Assuming for the moment that you're using tkinter, You probably want to start with one of the tutorials on the Python website. To give a thumbnail sketch of what you'd need to do, you need to import tkinter,
import tkinter as tk
(I chose to shorten the tkinter package name rather than import it en masse, because even in a simple program like this it is better to limit namespace pollution as much as possible.)
Geberally, you will want to define a subclass of the
Frame widget next, as a way of organizing the widgets that will go in said frame. In this case, you will want to make the number to be guessed at and the number of guesses member variables of the frame as well:
class GuessingGame(tk.Frame): def __init__(self, master=none) tk.Frame.__init__(self, master) self.target = random.randint(1, 50) self.tries = 0 self.correct = False self.pack() self.createWidgets()
You would then define the methods for setting up the widgets, event handlers and so forth.
Once you've defined the
GuessingGame class, you would then create a Tk root widget in the main program:
if __name__ == "__main__": root = tk.Tk() game = GuessingGame(master=root) game.mainloop() root.destroy()
As I said, this is just a quick sketch of the process.
Edited by Schol-R-LEA
The Tkinter widgets you are dealing with in your program are ...
tk.Entry() for data input
tk.Label() to display info and results
tk.Button() to start an action in the form of a function or method
You can also bind the entry instance to a function after, let's say, the Enter key has been used.
Edited by vegaseat