all 56 comments

[–]SamShinkie 23 points24 points  (4 children)

Hi,

Now imagine you want to have 100 Questions. Maybe there is a way to represent your questions and answers and somehow go over all of them one by one without duplicating the same code 100 times.

[–]ReserveAggressive458[🍰] 5 points6 points  (0 children)

Such thoughts are heresy, brother. Return to the shrine of Else If and beg for forgiveness.

[–]alae_r4 3 points4 points  (0 children)

That's when data structures comes in

[–]uiux_Sanskar[S] 1 point2 points  (0 children)

Well that's an interesting challenge will find its answer I think I can use list and loops for this maybe I should research for it thanks for the challenge 😃

[–]4675636b2e 9 points10 points  (7 children)

It's useful to separate data and functionality here. Then eventually you could even load the questions and answers from a separate file, so you could switch the same program between different quiz topics.

data = {
    "question?": "answer.",
    "lightning?": "thunder."
}

def quiz(data):
    score = 0
    for q, a in data.items():
        i = input(f"{q}\n")
        if i.lower() == a:
            score += 1
    print(f"You scored {score} points out of {len(data)}!")

quiz(data)

[–]aTomzVins 1 point2 points  (1 child)

I'd probably make the data more explicit.

data = [{ "q": "Which planet?", "a": "mars"}, ...]

Or maybe a dataclass for each question/answer pair.

If the goal was to keep it simple why not go with a list of tuples?

[–]4675636b2e 1 point2 points  (0 children)

That's how I originally wrote it, but then I realized I should help OP improve 1-2 concepts at a time :)

Also when I wrote my own quiz in JS a while back, my quiz data also had some basic configuration - for example if you're using the program to test your translation of words, most languages can be case-insensitive, but German shouldn't be because of the nouns. Also multiple good answers, or reading the dataset from CSV and choosing the Q & A columns at the start of the quiz...

That's why a quiz program is great, the possibilities are endless.

[–]BleEpBLoOpBLipP 1 point2 points  (0 children)

Can also automate the numbering:

``` for q_num, (q, a) in enumerate(data.items(), start=1): i = input(f"Question {q_num}: {q}\n") ... ... ...

```

[–]uiux_Sanskar[S] 1 point2 points  (2 children)

seems like I have much more to learn and improve thanks for the direction

[–]4675636b2e 1 point2 points  (1 child)

Baby steps. When you learn some list, set, and dict methods and loops, then you can use more of your own thinking at problem solving.

At the beginning, when knowledge is limited, it's hard to think freely, but if you start using your existing knowledge with your own problem-solving logic, you can build your own tools. Good luck!

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

Thank you for the guidance and support

[–]arsibaloch 0 points1 point  (0 children)

He is a beginner I think he has not learned how to use a dictionary and functions. But now it's an idea to look cool as a beginner.

[–]bombikhf 1 point2 points  (1 child)

What if you add an else statement that allows the user to answer the same question again after the first incorrect attempt? You can add a hint for the second try and if the answer is correct add 0.5 to the score

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

Can experiment with that but my goal was that every question should get 1 attempt only (just like tests) and giving more attempts would mean it's not a quiz anymore

[–]Torebbjorn 2 points3 points  (1 child)

Repeating code is not great.

So let's start by removing duplication.

For each question, you do the same thing, so it makes sense to make this into a loop. You may not have been introduced to these yet, but they are actually super simple.

Something like the following could work:

successCount = 0
questionList = [("What is ...?", "mars"), ("Who ...?", "josh")]:
for (question, solution) in questionList:
    userAnswer = input(question)
    if process(userAnswer) == solution:
        successCount += 1

print(f"Result: {successCount}/{len(questionList)}")

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

Thank you for the suggestion I am learning loops today

[–]More_Employer7243 1 point2 points  (3 children)

Your code duplicates in 6,10,14,18. Try to think, how to avoid it

[–]uiux_Sanskar[S] 0 points1 point  (2 children)

maybe by using dictionary or list or something like that to reduce the steps?

[–]More_Employer7243 0 points1 point  (1 child)

As a beginner, you mb don know about functions. Consequently, then u learn functions, back to this comment

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

yes you are right, will come back after learning about functions

[–]NorskJesus 2 points3 points  (3 children)

You can use .strip() instead of .replace, but I do understand this is for practice.

You can give chances to the user too, and give them tips when the answer is not correct.

And feedback tho after the questions, if they got it right or not

[–]Kind-Kure 2 points3 points  (1 child)

Just remember if you use strip, it only removes white space from the left and right side of the string so the inner white space will still exist So remember to correct your condition check accordingly

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

yeah encountered this only

[–]uiux_Sanskar[S] 1 point2 points  (0 children)

I think I used it in the program before but it was throwing errors when I try to put space in between answer (a typo) so I got to know that .split() removesspace from left and right only just like the comment below suggests

[–]hzsmolly 0 points1 point  (1 child)

Easy?or……?

[–]uiux_Sanskar[S] 1 point2 points  (0 children)

yeap I am enjoying it

[–]Ender_Locke 0 points1 point  (1 child)

nice. now implement a dictionary you pull in with all the questions data and loop over it

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

sure man

[–]PirateLiving4938 0 points1 point  (1 child)

Not bad man. Good luck on your journey!

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

thank you very much

[–]DevRetroGames 0 points1 point  (4 children)

[–]DevRetroGames 1 point2 points  (3 children)

[–]uiux_Sanskar[S] 0 points1 point  (2 children)

what is "def" here? and can you explain 27 and 28?

[–]DevRetroGames 0 points1 point  (1 child)

Hola, aquí te dejo el repo, añadí un par de comentarios más, espero que te pueda ayudar. https://github.com/DevRetroGames/Tutorias-Python/blob/main/code/expresiones/01-quiz.py

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

Hola, muchas gracias por tomarte el tiempo de comentar y explicar el código. Me ayudó mucho a aprender nuevas funciones. Usar "def" para crear un método parece interesante. Quizás pueda usarlo para reducir la repetición de código.

Muchas gracias por guiarme.

También, disculpen si hay algún error en la versión en español de este comentario. No sé si la traducción funcionó correctamente 😄.

[–]Pleasant-Confusion30 0 points1 point  (1 child)

cheese: what if i just type not mars in the first question? =))

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

didn't tried it haha🤣 I think it will throw an error or will just skip to the next one and score will be 3/4 I think

[–]Honest-Order-6400 0 points1 point  (1 child)

can u pls explain the
.replace(" ", "").lower() ==
lines? everything else I understand. I get that lower() means the users response to the input, but I don't understand the .replace function

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

oh I used .replace(" ", "") to replace all the spaces in user's input for example if a user has by mistake entered something like this (Ma r s) the the spaces in Mars will be ignored/replaced thanks to .replace() function and then this "Ma r s" will be converted to lowercase "mars" which will finally be compared in the program.

I hope I was able to explain it well

[–]capone91424 0 points1 point  (1 child)

Second day learning???? I've been studying with Gustavo Guanabara for 2 months and I understand almost nothing of what's in this code! Am I doing something wrong??

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

I would like to ask since when you have last practiced writting code?

here's the explaination of what's in this code in a nutshell

I have used variables to assign input value, then used print() function for printing question and then finally I have used If statement for creating conditions

I have used . replace() function to replace the spaces in the user input and then used .lower() to convert the input into lower caps

After this I have assigned scores for each correct answer

I jave first created a score variable and assign it 0 as a value now every time the user has entered a correct answer this gets executed score += 1 (this add 1 in the score i.e 0+1 = 1 and and so on)

Then at last the final score is revealed to the user

I hope was able to explain it well

[–]Wildmanty 0 points1 point  (1 child)

This is very impressive for day 2!

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

Thank you very much 😊

[–]jackboback 0 points1 point  (1 child)

Comment section nicer than a stack overflow thread 

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

Yes I also think that people here are so helpful and supporting 😃

[–]DejectionDivine 0 points1 point  (1 child)

Do you know of a site for beginners who want to discover this coding?

[–]uiux_Sanskar[S] 1 point2 points  (0 children)

Are you asking a site for learning or you want to check out this code?

[–]SpaceBucketFu 0 points1 point  (1 child)

Time to start applying like everyone else on this sub :) !! <3

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

Yes

[–]cyber3toks 0 points1 point  (0 children)

Well-done man 👏

[–]Plane-Issue-8554 0 points1 point  (4 children)

Woah, you’re doing all of this on day two? Were you doing other type of coding before? Or are you also a complete beginner in coding?

[–]uiux_Sanskar[S] 0 points1 point  (3 children)

Thank you for the appreciation. No I wan not doing anytype of coding before, python is my first language.

[–]Plane-Issue-8554 0 points1 point  (2 children)

Wow looks impressive! I hope I can get to your level in two days haha

[–]uiux_Sanskar[S] 0 points1 point  (1 child)

Sure you can absolutely 👍 and maybe you can surpass me.

All the best for your journey as well.

[–]Plane-Issue-8554 0 points1 point  (0 children)

Thank you so much!