1

I have a question for python coding

I am very new to coding my apologies if my lingo is off or hard to understand

I am trying to learn how to create a multiple question test where the user can only input a string as the input in the for loop, and if they put any other datatype it restarts the for loop ( asks the question again)

Here is what I have

class question:

    def __init__(self, prompt, answer):
        self.prompt = prompt
        self.answer = answer

question_prompt = [

    "what is 2+2? \n a 3 \n b 4 \n c 5 \n d 6 \n input answer here ",
    "what is 3+3? \n a 4 \n b 5 \n c 6 \n d 7 \n input answer here ",
    "what is 10-2?\n a 7 \n b 9 \n c 8 \n d 6 \n input answer here "

]

questions = [

    question(question_prompt[0],"b"),
    question(question_prompt[1],"c"),
    question(question_prompt[2],"c")

]

def run_test(questions):

    score = 0
    for question in questions:
        answer = str(input(question.prompt))
        if answer == question.answer:
            score += 1
       ## if xxxxxxx(): ## This is the line I need help with I want to check if the input is a string and if not print("Wrong input option please enter a letter")


    print("you got " + str(score) + "/" + str(len(questions)))

run_test (questions)

Thank you in advance for the help :)

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
sacerdos
  • 23
  • 3
  • 2
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – cSharp May 09 '22 at 06:41
  • 1
    What do you mean by "the user can only input a *string*"? The `input` function always returns a string. – blhsing May 09 '22 at 06:50
  • Without going into the details of the code itself, since you mentioned that your goal is to "restart" the loop, have you considered a while loop instead of a for one? This should make it very easy for you to set the requirements as you need with if statements. – Squary94 May 09 '22 at 06:57
  • @Squary94 "have you considered a while loop *instead of a for one*?" The `for` loop is there to iterate through the list of questions and has nothing to do with the OP's wish to retry a question. – blhsing May 09 '22 at 07:10

2 Answers2

1

I think I know what you're trying to do, but your requirement is rather poorly defined. The input() function always returns a string as you can see in the documentation. So you can't really define your requirement as requiring "some string" as the input. Even if the user inputs what you consider an int like 3, the input function gives you the str "3". So you'll need some kind of additional requirements and then check to see if what the user inputed is what you consider a "valid" string. I'll proceed with the rest of the answer assuming you come up with some function that'll validate your string for your requirements

def is_valid(string: str) -> bool:
    """checks if the string is valid"""
    # validation code goes here

Considering your question is a multiple choice question and assuming you want the user to input the option corresponding to the answer perhaps your validation code could possibly be

def is_valid(string: str) -> bool:
    valid_options = ['a', 'b', 'c', 'd']
    return string.lower() in valid_options

now to keep looping until the user inputs a valid input you can do something like

for question in questions:
    answer = input(question.prompt)
    while not is_valid(answer):
        print("Wrong input, please enter a valid input")
        answer = input(question.prompt)
phoney_badger
  • 462
  • 3
  • 10
  • 1
    Since this is multiple choice with answers of a, b, c, or d the validation code could be: `return string in ['a', 'b', 'c', 'd']` – DarrylG May 09 '22 at 07:07
  • Yeah you're right. I'll edit my answer to add that. – phoney_badger May 09 '22 at 07:11
  • Hi Phoney_badger, Thank you very much for your help! i sincerely appreciate it. I was able to get the code running with your help, but there is a part of the code I still do not understand. i tried googling to find the answer first but I couldnt find anything In the line "def is_valid(input: str) -> bool:" I dont understand the "(input: str)" or the "->" sections of the code. Can you describe how those are working? is "->" an operator and what does it do? what does the (input : str) do? My apologies for being so bad ive only been coding for a few days – sacerdos May 09 '22 at 23:04
  • Oh I'm sorry since you're a beginner I should've kept it simple and left those out. That is simply a type hint, showing you what the types of the arguments and the return value of the function are. You can leave them out without affecting anything. It is equivalent to `def is_valid(input):`. The `input: str` part simply hints that the input variable is of type string and `-> bool` shows the function returns a boolean. You can learn more about them if you're interested [here](https://docs.python.org/3/library/typing.html). But you can also ignore them for now, they are completely optional. – phoney_badger May 10 '22 at 00:33
  • oh that makes much more sense! Thank you very much for clarifying and providing me with the link to the type hints, youre a ton of help phoney_badger! I appreciate you beyond belief. – sacerdos May 10 '22 at 18:46
-2

You need to use isinstance(variable, str) returns boolean value.

if isinstance(question, str) != True:
        print("Wrong input option please enter a letter")