JMorris
JMorris

Reputation: 11

How strip python

I'm writing a quiz code for school. The code iterates over a text file and loads the questions and answers from it. The user will select a difficulty to do the quiz on. The number of options for answers will vary depending on the difficulty. I have split each question and possible answer in the text file with commas.

from random import shuffle

file = open("maths.txt" , "r")
for line in file:
    question = line.split(",")
    print(question[0])
    if difficulty in ("e", "E"):
        options = (question[1], question[2])

    if difficulty in ("m", "M"):
        options = (question[1], question[2], question[3])

    if difficulty in("h", "H"):
        options = (question[1], question[2], question[3], question[4])

    options = list(options)
    shuffle(options)
    print(options)

    answer = input("Please enter answer: ")

    if answer in (question[1]):
        print("Correct!")
    else:
        print("incorrect")
  file.close()

This is what a line of the text file would look like: Question 1. What is 4+5?,9,10,20,11

The first option (question[1]) will always be the correct answer, therefore I would like to shuffle the options. With this code the options are outputted with square brackets, newline characters and quotation marks. Does anyone know how I can strip these? I tried to use: line.split(",").strip() however this seemed to do nothing at all. Thanks

Upvotes: 0

Views: 109

Answers (4)

M. Volf
M. Volf

Reputation: 1482

Something like this?

from random import shuffle
def maths_questions():
  file = open("maths.txt" , "r")
  for line in file:
    question = line.strip().split(",") # every line in file contains newline. add str.strip() to remove it
    print(question[0])

    if difficulty in ("e","E"):
        options = [question[1],question[2]]
    elif difficulty in ("m","M"):
        options = [question[1],question[2],question[3]]
    elif difficulty in("h","H"):
        options = [question[1],question[2],question[3],question[4]]
    # why to create tuple and then convert to list? create list directly

    shuffle(options) #shuffle list
    print("Options: ", ", ".join(options)) # will print "Options: opt1, opt2, opt3" for M difficulty

    answer=input("Please enter answer: ")

    if answer in (question[1]):
            print("Correct!")
    else:
        print("Incorrect, please try again...")
  file.close()

Python docs:

str.join(iterable)

Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method.

Upvotes: 2

The Sleepy Penguin
The Sleepy Penguin

Reputation: 100

To remove characters from a string, use .rstrip("put text to remove here") to remove characters from the right end of the string and .lstrip("text to remove") to remove characters from the left of the string.

Upvotes: 1

Mr. Nun.
Mr. Nun.

Reputation: 850

The problem is that you are trying to print a list object. Instead, you should print each option. you'd probably be better printing some formatting around it:

for option_num, option in enumerate(options):
    print("{} - {}").format(option_num, option)

please read about enumerate and format to understand exactly what happens here

Upvotes: 3

Vitor Vanacor
Vitor Vanacor

Reputation: 455

for option in options:
    print(option)

Upvotes: 1

Related Questions