list - Python : verifying user's input? -


my variable useranswerlist[] takes user's input, , need verify if user has input other a, b, c, d.

here's code below, , wondering how should validate user's input between range of (a b c d) , if not print error message?

answerlist = ["a","c","a","a","d","b","c","a","c","b","a","d","c","a","d","c","b","b","d","a"] useranswerlist = [] correct = 0 incorrect = 0  def main():     in range(20):         = + 1         answer = input("please enter answer question %d:" %i)         useranswerlist.append(answer)         numcorrect = [i in useranswerlist if in answerlist]      if len(numcorrect) > 15:         print("congratulations have passed exam!")     elif len(numcorrect) < 15:         print("failed....please try again")  correct = len(numcorrect) incorrect = 20 - correct  print("correct answers:",correct,"/ incorrect answers:",incorrect)    main() 

you using in syntax well. if want check whether answer either a, b, c or d following code work.

if not answer in ['a', 'b', 'c', 'd']:     print("invalid answer.") 

this script can further benefit python's syntax such:

if answer not in 'abcd':     print("invalid answer.") 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -