Python Forum
my first attempt, please advise me of any thing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
my first attempt, please advise me of any thing
#1
My first bit of code, I've only just started to learn, I understand this is very basic but this is the best way I learn.

I'm not sure how this should look but its how it currently looks, I'm just about to apply a tkinkler GUI and have it displaying, and then add some form of scoreboard.

please be as critical as possible, AND any advise on something else I could build as very basic although to learn, thanks.

import random
from tkinter import *


answer = random.randint(2, 25)

two = "4 / 2", "a baby conceive two years and nine months ago, the answer is the baby's age. ", "864 - 482 - 380 ="
three = "426 / 142 =", "1098 - 685 - 410 =", "3 * 1 ="
four = "2 Squared", "793 - 406 - 383 =", "2 * 2 ="
five = "4596 - 102 - 4489 =", "30 / 6 =", "1 * 5 ="
six = "28 - 16 - 6 =", "3 * 2 =", "10 / 2"
seven = "56 - 7 - 42 =", "0.7 * 10 = ", "63 / 9 ="
eight = "2 Cube", "36 - 12 - 16 =", "55 / 7 ="
nine = "3 Squared", "586 - 428 - 149 =", "99 / 11 ="
ten = "586 - 926 - 350 =", "80 / 8 =", "10 * 1 ="
eleven = "312 - 52 - 249 =", "121 / 11 = 11", "5.5 * 2 ="
twelve = "Medium of 9, 12, 7, 16 and 13", "456 - 215 - 229 =", "144 / 12 ="
thirteen = "844 - 726 - 105 =", "6.5 * 2 =", "6 + 7 ="
fourteen = "652 - 486 - 152 =", "7 * 2 = ", "3.5 * 4 ="
fifteen = "986 - 785 - 186 =", "10 + 5 =", "5 * 2 ="
sixteen = "4 Square", "786 - 582 - 188 =", "4 * 4 ="
seventeen = "147 - 52 - 78 =", "8 + 9 =", "3.4 * 5 ="
eighteen = "3874 - 3772 - 3754 =", "3 / 6 =", "2.25 * 8 ="
nineteen = "10528 - 7854 - 2655 =", "4.75 * 4 =", "1216 / 64 ="
twenty = "206 - 150 - 36 =", "5 * 4 =", "100 / 5 ="
twenty_one = "478 - 264 - 193 =", "7 * 3 =", "7 / 3 ="
twenty_two = "9875 - 5248 - 4605 =", "5.5 * 4 =", "352 / 16 ="
twenty_three = "548 - 321 - 23 =", "5.75 * 4 =", "368 / 16 ="
twenty_four = "457 - 326 - 107 =", "6 * 4 =", "8 * 3 ="
twenty_five = "5 Squared", "The highest number in our quiz", "if you * this number by 5 you will get 125"


if answer == 2:
        print(random.choice(two))
if answer == 3:
        print(random.choice(three))
if answer == 4:
        print(random.choice(four))
if answer == 5:
        print(random.choice(five))
if answer == 6:
        print(random.choice(six))
if answer == 7:
        print(random.choice(seven))
if answer == 8:
        print(random.choice(eight))
if answer == 9:
        print(random.choice(nine))
if answer == 10:
        print(random.choice(ten))
if answer == 11:
        print(random.choice(eleven))
if answer == 12:
        print(random.choice(twelve))
if answer == 13:
        print(random.choice(thirteen))
if answer == 14:
        print(random.choice(fourteen))
if answer == 15:
        print(random.choice(fifteen))
if answer == 16:
        print(random.choice(sixteen))
if answer == 17:
        print(random.choice(seventeen))
if answer == 18:
        print(random.choice(eighteen))
if answer == 19:
        print(random.choice(nineteen))
if answer == 20:
        print(random.choice(twenty))
if answer == 21:
        print(random.choice(twenty_one))
if answer == 22:
        print(random.choice(twenty_two))
if answer == 23:
        print(random.choice(twenty_three))
if answer == 24:
        print(random.choice(twenty_four))
if answer == 25:
        print(random.choice(twenty_five))

guess = input("What do you think the answer is ? ")

if str(guess) == str(answer):
        print("well done!")
else:
        print("wrong!")
Reply
#2
I didn't realize all those lines were tuples rather than just long strings. I realize the tuple parenthesis are optional, but I strongly recommend you use them in cases like these, as it makes the code a lot more readable.

Here's my refactor of your code, with comments though admittedly not tested (minus some questions; should be easy to add back in):
from random import choice 

# I changed this so that individual questions are stored with their answers.
# I don't organize questions with the same answer as being together, instead
# preferring to use comments to track the kind of question it is.
questions_answers = [
        # arithmetic
        ("4 / 2", 2),
        ("864 - 482 - 380 =", 2),
        # word problems
        ("a baby conceive two years and nine months ago, the answer is the baby's age. ", 2),
        ("2 Squared", 4),
        ("3 Squared", 9),
]

# this is called tuple unpacking, if you're unfamiliar with it
question, answer = random.choice(question_answers)
 
guess = input("What do you think the answer is ? ")

# I turn the guess into an int and compare that, instead of turning both to strs.
# My thought process is, I want to know if they have the right number, so I don't
# want it to say "wrong!" if for example they had trailing whitespace (which the
# int function ignores). The int function will also raise an exception when the
# guess is not an int. This might be too ugly for you in terms of output, but
# you can catch the exception and do whatever you want (e.g. complain, ask for a
# new attempt).
if int(guess) == answer:
        print("well done!")
else:
        print("wrong!")
Reply
#3
Thank you so much for your reply, I'm going to try to implement and learn some of the stuff you said in the morning, I've got another question for anybody willing to answer though...

no matter what I come back in the entry box, it returns wrong, but as you can see in my code, it should return correct!

def result():
        if str(entry_box) == str(answer):
                #print("well done!")
                btnSCS = Button(window, text="Correct!")
                btnSCS.grid(column=2, row=3)
        else:
                #print("wrong!")
                btnSCSS = Button(window, text="Wrong!")
                btnSCSS.grid(column=2, row=3)
I am not sure why it is coming out like this, and cannot work it out, can somebody please explain what I'm doing wrong here? thanks for all replies!

import random
from tkinter import *

window = Tk()#creating window
window.title("Maths")#title of window
window.minsize(width=40, height=40)
window.maxsize(width=400000, height=400000)



answer = random.randint(2, 2)

two = "4 / 2 =", "a baby conceive two years and nine months ago, the answer is the baby's age. ", "864 - 482 - 380 ="
three = "426 / 142 =", "1098 - 685 - 410 =", "3 * 1 ="
four = "2 Squared", "793 - 406 - 383 =", "2 * 2 ="
five = "4596 - 102 - 4489 =", "30 / 6 =", "1 * 5 ="
six = "28 - 16 - 6 =", "3 * 2 =", "10 / 2"
seven = "56 - 7 - 42 =", "0.7 * 10 = ", "63 / 9 ="
eight = "2 Cube", "36 - 12 - 16 =", "55 / 7 ="
nine = "3 Squared", "586 - 428 - 149 =", "99 / 11 ="
ten = "586 - 926 - 350 =", "80 / 8 =", "10 * 1 ="
eleven = "312 - 52 - 249 =", "121 / 11 = 11", "5.5 * 2 ="
twelve = "Medium of 9, 12, 7, 16 and 13", "456 - 215 - 229 =", "144 / 12 ="
thirteen = "844 - 726 - 105 =", "6.5 * 2 =", "6 + 7 ="
fourteen = "652 - 486 - 152 =", "7 * 2 = ", "3.5 * 4 ="
fifteen = "986 - 785 - 186 =", "10 + 5 =", "5 * 2 ="
sixteen = "4 Square", "786 - 582 - 188 =", "4 * 4 ="
seventeen = "147 - 52 - 78 =", "8 + 9 =", "3.4 * 5 ="
eighteen = "3874 - 3772 - 3754 =", "3 / 6 =", "2.25 * 8 ="
nineteen = "10528 - 7854 - 2655 =", "4.75 * 4 =", "1216 / 64 ="
twenty = "206 - 150 - 36 =", "5 * 4 =", "100 / 5 ="
twenty_one = "478 - 264 - 193 =", "7 * 3 =", "7 / 3 ="
twenty_two = "9875 - 5248 - 4605 =", "5.5 * 4 =", "352 / 16 ="
twenty_three = "548 - 321 - 23 =", "5.75 * 4 =", "368 / 16 ="
twenty_four = "457 - 326 - 107 =", "6 * 4 =", "8 * 3 ="
twenty_five = "5 Squared", "The highest number in our quiz", "if you * this number by 5 you will get 125"

def answers():

        if answer == 2:
                lbl = Label(window, text=random.choice(two))  # generate text
                lbl.grid(column=2, row=4)  # place text location
                btn1.grid_forget()
                entry_box()
                submit_btn()
                #print(random.choice(two))
        if answer == 3:
                print(random.choice(three))
        if answer == 4:
                print(random.choice(four))
        if answer == 5:
                print(random.choice(five))
        if answer == 6:
                print(random.choice(six))
        if answer == 7:
                print(random.choice(seven))
        if answer == 8:
                print(random.choice(eight))
        if answer == 9:
                print(random.choice(nine))
        if answer == 10:
                print(random.choice(ten))
        if answer == 11:
                print(random.choice(eleven))
        if answer == 12:
                print(random.choice(twelve))
        if answer == 13:
                print(random.choice(thirteen))
        if answer == 14:
                print(random.choice(fourteen))
        if answer == 15:
                print(random.choice(fifteen))
        if answer == 16:
                print(random.choice(sixteen))
        if answer == 17:
                print(random.choice(seventeen))
        if answer == 18:
                print(random.choice(eighteen))
        if answer == 19:
                print(random.choice(nineteen))
        if answer == 20:
                print(random.choice(twenty))
        if answer == 21:
                print(random.choice(twenty_one))
        if answer == 22:
                print(random.choice(twenty_two))
        if answer == 23:
                print(random.choice(twenty_three))
        if answer == 24:
                print(random.choice(twenty_four))
        if answer == 25:
                print(random.choice(twenty_five))
"""
def guess():
        guess = input("What do you think the answer is ? ")
"""
btn1 = Button(window, text="Are you ready?", command=answers)
btn1.grid(column=0, row=1)


def entry_box():
        e1 = Entry(window)
        e1.grid(row=0, column=1)

def submit_btn():
        btnS = Button(window, text="Submit!!", command=result)
        btnS.grid(column=0, row=8)

def result():
        if str(entry_box) == str(answer):
                #print("well done!")
                btnSCS = Button(window, text="Correct!")
                btnSCS.grid(column=2, row=3)
        else:
                #print("wrong!")
                btnSCSS = Button(window, text="Wrong!")
                btnSCSS.grid(column=2, row=3)

lbl = Label(window, text="Welcome to my maths game!")  # generate text
lbl.grid(column=0, row=0)  # place text location







#btn2 = Button(window, text="Exit!", command=exit)
#btn2.grid(column=25, row=25)


"""
w = Entry(window)
w.grid(column=0, row=6)
"""

window.mainloop()
Reply
#4
I don't really know tkinter, but I learned that print statements are a debuggers best friend. Add the line I've indicated and you'll see that they can never be correct.

def result():
        print(str(entry_box), answer) # <--------  add this for hint
        if str(entry_box) == str(answer):
                #print("well done!")
                btnSCS = Button(window, text="Correct!")
                btnSCS.grid(column=2, row=3)
        else:
                #print("wrong!")
                btnSCSS = Button(window, text="Wrong!")
                btnSCSS.grid(column=2, row=3)
Reply
#5
(Jan-25-2020, 03:36 AM)michael1789 Wrote: I don't know Tkinter, but I learned that print statements are a debuggers best friend. Add the line I've indicated and you'll see that they can never be correct.

def result():
        print(str(entry_box), answer) # <--------  add this for hint
        if str(entry_box) == str(answer):
                #print("well done!")
                btnSCS = Button(window, text="Correct!")
                btnSCS.grid(column=2, row=3)
        else:
                #print("wrong!")
                btnSCSS = Button(window, text="Wrong!")
                btnSCSS.grid(column=2, row=3)



I've added a print statement and it just returns this "<function entry_box at 0x036ADFA0> 2", I don't know what this means and google shows nothing, is there any way of understanding what this could mean?

i have changed the code a little and tried to get it just to return good if I set it to a number, yet this doesn't work either, thanks for any suggestions
Reply
#6
Again, not familiar with Tkinter, but looking at what you have it looks like printing entry_box it's giving you this information e1 = Entry(window) e1.grid(row=0, column=1) back. The description of what entry box means. You need to figure out when you hit the "submit_button" what it is returning and make sure that is the content of the Entry(window).

So the specific fix I can't tell you, but how to find it is to make sure your submit button is linked to the input window correctly so that the contents of the window are set as a variable you know. As it is, it looks like when you are hitting the submit button it is only to to run the function result(). That's not what you want. It need to take what is in the entry window and set it to a variable player_response, and player_response needs to replace str(entry_box) as what you are comparing to str(answer).

I bet it's simple or someone who knew the answer would have already chimed in and told you. If I were you I'd start by looking at a similar example on line. Your command for the submit button needs to set a variable then feed that as result(player_response).
Reply
#7
i think you would code this a lot better after you learn dictionaries.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020