Python Forum
Random quiz generator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random quiz generator
#11
this is how one of quiz files looks like:

[Image: 6hkugj.png]

I still didn't figure out how to add "?" at the end of the question.
Reply
#12
quizFile.write('{:>2} What is the capital of {}?\n'.format(questionNum+1, states[questionNum]))
See the question sign? You create the sentence as you want it to look.
So we start with the placeholder for the number of the question then the question with the placeholder for the state including the question sign at the end of the sentence and finally the new line.

You can indent the options also. Line 51, 52. Line 53 is removed and a new line character is added at the end of '\t{} {}\n'
        for index, opt in enumerate('ABCD'):
            quizFile.write('\t{} {}\n'.format(opt, answerOptions[index]))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#13
If I remember well adding ? there was my first option but there was mess again ( or I got smth wrong ). Now adding it again gives the correct outcome.

and thank you for:
for index, opt in enumerate('ABCD'):
            quizFile.write('\t{} {}\n'.format(opt, answerOptions[index]))
offered answers now look much much better, actually, quiz files look like on real test.
Reply
#14
Good. If you want to learn more about formatting you should do it. Controlling the output and making it easy to read is essential.
Here are a couple of web pages I google it just now:
https://www.programiz.com/python-program...ing/format
https://www.digitalocean.com/community/t...n-python-3

The official documentation about it:
https://docs.python.org/3/tutorial/inputoutput.html

Since Python 3.6 is here quite a long you have to know more about the new f-strings:
https://realpython.com/python-f-strings/
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#15
That you, this will be my priority.
Reply
#16
now, comparing f-string with .format() the question arises: Can everything that can be done with .format() also be rendered with f-string?

https://www.programiz.com/python-program...ing/format
this link goes into details with .format() and manipulating numbers, date, time etc. The question is how to do that with f-string...but this is a different topic.
Reply
#17
They work the same way.

>>> s = 'Friday'
>>> print(f'Today is {s[:3]:.>8}.')
Today is .....Fri.
>>> print('Today is {0:.>8}.'.format(s[:3]))
Today is .....Fri.
But are a way faster. The internal implementation is much more efficient.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,421 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Random Sentance Generator Liquid_Ocelot 1 3,990 May-19-2017, 10:59 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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