Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Python issues

  1. #1
    New to the CF scene
    Join Date
    Jul 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Python issues

    Hi. I am stuck on a piece of code I need to write for a multi choice quiz and it needs to catch out the user if they put anything other than A, B or C.

    All I know about it, is that it is an if statement.
    Please reply with some solutions.

  2. #2
    New to the CF scene
    Join Date
    Jul 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Python issues update

    If you hit a wrong key, it says please try again and goes back to the question you got wrong.
    lets say you press S instead of A, it won't say please try again that isn't an option.
    That is what I need it to say. if you know the python code to make it say that please reply with how to say that.

  3. #3
    New to the CF scene
    Join Date
    Jul 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Seeing as this is for a multiple choice quiz, I assume you will be making lots of calls to this code. So it will make sense to put it into a function that you can call from anywhere in your quiz.

    The comments in the code should explain most of what is going on.
    Code:
    #!/usr/bin/env python3
    
    def GetUserResponse()
        # dictionary of valid responses
        validResponses={"a","b","c"}
    
        # variable to store the users response
        # NOTE: initialised to a value that is NOT in our
        # list of valid responses
        userResponse=""
    
        # Loop until we get a valid response from the user:
        while userResponse not in validResponses:
            userResponse=input("Please answer a, b or c: ").lower()
            if userResponse not in validResponses:
                print("Invalid input - please try again!")
    
        return userResponse
    In the call to input (where we actually get input from the user), we are calling lower() - which will convert the users response to lowercase - that way we don't need to bother adding the CAPS versions of A,B,C to the dictionary of valid responses.

    To use the function in your quiz you could do something like this:
    Code:
    # ask a question
    print("Q: What is the air-speed velocity of an unladen swallow?")
    print("A. 16 M/s")
    print("B. 20 M/s")
    print("C. What do you mean? An African or European swallow?")
    
    # call our function to get the users response
    answer = GetUserResponse()
    
    # Check the users answer
    if answer == 'c':
        print("Aha, a monty python fan - have a point!")
    else
        print("You obviously don't know monty python! :( No points for you!")
    Above would be a simple usage, using hard-coded questions and answers.

    But if you are doing something more object oriented - e.g. You have read all questions/answers from a file and are storing them in a list of Question objects and then asking the questions in a sequence - you'd need to plumb-in the GetUserResponse function as necessary to fit your design.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •