5
\$\begingroup\$

I'm learning python, and I'm trying to practice what I learned recently.

I wanted to make a quiz game using pandas. What do you think? Is there anything I could do better?

import random
import pandas as pd
pd.options.mode.chained_assignment = None  # default='warn'

"""We need one excel file(kerdesek.xlsx), then we will add questions from B1 to x1 columns (B1,C1 etc...).
Our answers will be right below them.The correct answer will always be the second row(B2, C2 etc...). We will shuffle them later, so it won't be in the same position when we print them to the user. In this code we can write four answers down.
So it will look like something like this:
Question1  Question2  Question3
Correct1    correct      same
answer2    ....       ....
answer3    ....       ....
answer4    answer4     ....
"""


class Quiz:

    df = pd.read_excel("kerdesek.xlsx")  # reading our question and answers xlsx

    randomvalue = random.randint(1, 4)  # random number(from 1 to 4)
    df2 = df.iloc[0:, randomvalue]  # choosing Columns and listing
    random.shuffle(df2)  # randomize list

    df3 = pd.read_excel("kerdesek.xlsx")
    inputChoice = df3.iloc[0:, randomvalue].head(1) # choosing from the original column and also choosing the fist row(answer)
    question_ask = df2  # randomized questions list

    print("Answers:--------------")
    print(question_ask)  # Printing df2 var. random column list
    print("---------------------")
    choosing = str(input("Your answer:: "))  # user input

    if choosing in str(inputChoice):  # if the inputChoice value is in the user input value, then the answer is correct
        print("correct answer")

    else:
        print("wrong answer")  # it wasn't correct whops

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Simpler

I see no reason to pace your code inside a class. You can delete this line, and remove the level of indentation from the lines below it:

class Quiz:

Documentation

It is great that you added a docstring at the top of the code. I reformatted it to remove the really long line and added punctuation and whitespace to make it a little easier to read:

"""
We need one Excel file (kerdesek.xlsx), then we will add questions 
from B1 to x1 columns (B1, C1, etc.).
Our answers will be right below them.
The correct answer will always be the second row (B2, C2, etc.). 
We will shuffle them later, so it won't be in the same position when 
we print them to the user. In this code we can write four answers down.
So it will look like something like this:

Question1  Question2  Question3
Correct1    correct      same
answer2    ....       ....
answer3    ....       ....
answer4    answer4     ....
"""

Comments

The comment in the following line is mysterious and can probably be deleted:

pd.options.mode.chained_assignment = None  # default='warn'

Several of the other comments can be deleted because it is obvious from the code what the code it doing, such as:

df = pd.read_excel("kerdesek.xlsx")  # reading our question and answers xlsx

randomvalue = random.randint(1, 4)  # random number(from 1 to 4)

End-of-line comments that make the line really long should be placed on its own line above the code. Change:

inputChoice = df3.iloc[0:, randomvalue].head(1) # choosing from the original column and also choosing the fist row(answer)

to:

# choosing from the original column and also choosing the fist row (answer)
inputChoice = df3.iloc[0:, randomvalue].head(1)

DRY

I don't think there is a need to read the input file twice. I think you can delete df3 and just use df in its place. This makes the code more efficient, saving time and memory.

Naming

The randomvalue variable name does not convey any meaning. For example, if it corresponds to a number of answers, a better name would be num_answers.

The PEP 8 style guide recommends using snake_case to name variables. Change inputChoice to input_choice.

Grouping

It would be better to group all operations using df2 together. I suggest moving this line:

question_ask = df2  # randomized questions list

to:

random.shuffle(df2)
question_ask = df2 
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.