Skip to content
Advertisement

Python project basing on picture to guess the answer for the flag name , I think that the while loop is the problem

My code snippet:

import tkinter
from tkinter import Label, Canvas, PhotoImage, Frame, Entry, Button

# Create the function instruction.
def instruction():
    tkinter.messagebox.showinfo('Welcome To the Flag Guessing Game',
                                'You have 3 attempts to play this game '
                                'There are 6 countries in this game '
                                'you can answer AU for Australia,  JP for Japan , '
                                'NO for Norway,'
                                ' NRA for Nigeria, '
                                'NL for Netherland, '
                                'UK for United Kingdom')


# Create the function for show the picture on frame.
def image_start():
    first_image = Label(frame1, image=Image1)
    first_image.place(relx=0.5, rely=0.15, relwidth=0.7, relheight=0.6, anchor='n')


def image_two():
    second_image = Label(frame1, image=Image2)
    second_image.place(relx=0.5, rely=0.15, relwidth=0.7, relheight=0.6, anchor='n')


def image_three():
    second_image = Label(frame1, image=Image3)
    second_image.place(relx=0.5, rely=0.15, relwidth=0.7, relheight=0.6, anchor='n')


def image_forth():
    second_image = Label(frame1, image=Image4)
    second_image.place(relx=0.5, rely=0.15, relwidth=0.7, relheight=0.6, anchor='n')


def image_fifth():
    second_image = Label(frame1, image=Image5)
    second_image.place(relx=0.5, rely=0.15, relwidth=0.7, relheight=0.6, anchor='n')


def image_sixth():
    second_image = Label(frame1, image=Image6)
    second_image.place(relx=0.5, rely=0.15, relwidth=0.7, relheight=0.6, anchor='n')


turns = 3
# Create the function for button and check the entry,
while turns > 0:
    def flag_check(entry):
        # create the global variable turns
        global turns
        # if the answer is AU, it will be right and move to next image
        if entry == "AU":
            tkinter.messagebox.showinfo('Answer', 'Congratulation, you got it!')
            image_two()
            if entry == "JP":
                tkinter.messagebox.showinfo('Answer', 'Congratulation, you got it!')
                image_three()
            else:
                tkinter.messagebox.showinfo('Answer', 'You are wrong')
                turns -= 1
                if entry == 'Netherlands' or entry == 'NL':
                    tkinter.messagebox.showinfo('Answer', 'Congratulation, you got it!')
                    image_forth()
                else:
                    tkinter.messagebox.showinfo('Answer', 'You are wrong')
                    turns -= 1
                    if entry == 'Nigeria' or entry == 'NRA':
                        tkinter.messagebox.showinfo('Answer', 'Congratulation, you got it!')
                        image_fifth()
                    else:
                        tkinter.messagebox.showinfo('Answer', 'You are wrong')
                        turns -= 1
                        if entry == 'United Kingdom' or entry == 'UK':
                            tkinter.messagebox.showinfo('Answer', 'Congratulation, you got it!')
                            image_sixth()
                        else:
                            tkinter.messagebox.showinfo('Answer', 'You are wrong')
                            turns -= 1
                            if entry == 'Norway' or entry == 'NO':
                                tkinter.messagebox.showinfo('Answer', 'Congratulation, you got it!')
                            else:
                                tkinter.messagebox.showinfo('Answer', 'You are wrong')
                                turns -= 1
        else:
            tkinter.messagebox.showinfo('Answer', 'You are wrong')
            turns -= 1
else:
    tkinter.messagebox.showinfo('Answer', 'You are wrong 3 times, Sorry Game Over')
# Create the root windows.
root = tkinter.Tk()
width = 1000
height = 800
Image1 = PhotoImage(file='Australia.png')
Image2 = PhotoImage(file='Japan.png')
Image3 = PhotoImage(file='Netherlands.png')
Image4 = PhotoImage(file='Nigeria.png')
Image5 = PhotoImage(file='UK.png')
Image6 = PhotoImage(file='Norway.png')
# Create the canvas with the height and width we want, here I use 1000 and 800 which stored in the variable above
canvas = Canvas(root, height=height, width=width)
canvas.pack()
# Create the background of the game by using PhotoImage.
background = PhotoImage(file='USA.png')
label_background = Label(root, image=background)
label_background.place(relwidth=1, relheight=1)
# Create the frame on the top which content the question and instruction for game.
frame1 = Frame(root, bg='#0a202b', bd=10)
frame1.place(relx=0.5, rely=0.05, relwidth=0.85, relheight=0.7, anchor='n')
label = Label(frame1)
label.place(relwidth=1, relheight=1)
# Create the button and entry in frame2 for user can submit the answer.
frame2 = Frame(root, bg='#0a202b', bd=10)
frame2.place(relx=0.5, rely=0.8, relwidth=0.85, relheight=0.15, anchor='n')
entry = Entry(frame2, font=40)
entry.place(relwidth=0.6, relheight=1)
button = Button(frame2, text="Submit", font=12, bg='grey', command=lambda: flag_check(entry.get()))
button.place(relx=0.65, relwidth=0.35, relheight=1)
image_start()
title = Label(root, text="Welcome To the Flag Guessing Game", font=120, bg='white')
title.place(relx=0.5, rely=0.1, relwidth=0.6, relheight=0.05, anchor='n')
# info = Button(root, text="Welcome To the Flag Guessing Game", font=100, command=instruction)
info = Button(root, text="Click here for instruction", font=100, bg='grey', command=instruction)
info.place(relx=0.5, rely=0.65, relwidth=0.2, relheight=0.05, anchor='n')
textbox = Label(frame2, text="Your answer: ", font=100, bg='white')
textbox.place(relx=0.02, rely=0.03, relwidth=0.3, relheight=0.2)
# Call the first image show on frame1.
root.mainloop()

I want to program a guessing game basing on the image of the flags from different countries. There are 6 countries which I picked. The user will have maximum 3 turns, if they are wrong 3 times, the game will end. The user cannot move to the next image until they guess correctly the current image right. I think the problem is the loop.

Advertisement

Answer

Your style of writing code is not that much right, if you are doing same things every time then you can turn that same things into a function and can call function is you need it and you are doing everything statically which is not a good practice. I have change most of the code you may not exactly understand but play with it and you will eventually understand it.

Explanation : Variable n is for level like things and b is to making sure program will not check 2 condition while checking answer. msg function is for success message and msgwrng function is for wrong answer. I have changed the variable name of Entry from entry to entrybox. I haven’t changed other things which should be changed but I think you will understand from here and change those code also.

Here’s your code. If you find anything wrong or didn’t understand anything ask in comment section.

If it helped you don’t forget to mark this as answer, it will help for future programmer also.

from tkinter import *
from tkinter import messagebox

turns = 3

def msg():
    messagebox.showinfo('Answer', 'Congratulation, you got it!')

def msgwrng():
    messagebox.showinfo('Answer', 'You are wrong')

# Create the function for button and check the entry,

def flag_check(entry):
    # create the global variable turns 
    global turns,n
    if turns>0:
        b=1
        if n==1:
            if entry == "AU":
                msg()
                image_two()
                n+=1
                b=0
            else:
                msgwrng()
                turns -= 1
        if n==2 and b==1:
            if entry == "JP":
                msg()
                image_three()
                n+=1
                b=0
            else:
                msgwrng()
                turns -= 1
        if n==3 and b==1:
            if entry == 'Netherlands' or entry == 'NL':
                msg()
                image_forth()
                n+=1
                b=0
            else:
                msgwrng()
                turns -= 1
        if n==4 and b==1:
            if entry == 'Nigeria' or entry == 'NRA':
                msg()
                image_fifth()
                n+=1
                b=0
            else:
                msgwrng()
                turns -= 1
        if n==5 and b==1:
            if entry == 'United Kingdom' or entry == 'UK':
                msg()
                image_sixth()
                n+=1
                b=0
            else:
                msgwrng()
                turns -= 1
        if n==6 and b==1:
            if entry == 'Norway' or entry == 'NO':
                msg()
            else:
                msgwrng()
                turns -= 1
    else:
        messagebox.showerror("Finished","All answer were wrong")
    entrybox.delete(0,END)


n=1
#create the root windows
root = Tk()
width = 1000
height = 800


entrybox = Entry(frame2, font=40)
entrybox.place(relwidth=0.6, relheight=1)

button = Button(frame2, text="Submit", font=12, bg = 'grey', command=lambda: flag_check(entrybox.get()))
button.place(relx=0.65, relwidth=0.35, relheight=1)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement