#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    User inputting real answer using numbers?


    Hi, I'm having a problem implementing 2 things in my code. I am struggling to let the user input the number of the correct definition.

    print ("1) ", answers[0])
    print ("2) ", answers[1])
    print ("3) ", answers[2])

    z = realAns
    for z in range (len(def)):
    if realAns == def [z]:
    position2 = z


    CorrectOne = input("Pick correct definition and input number 1,2 or 3: ")
    if Correctone == realAns[z]:
    print ("Well Done you Solved it!")

    I believe the problem is with this part of the code. People who are confused, I am trying to let the user input the correct number which corresponds to the definition. However, I am struggling to do this as the error comes up as string out of range? What do I need to do to fix this!? Furthermore, I have no idea how to start to implement a part of a code where when the correct definition is picked twice correctly for the same answer, the definition will automatically remove itself.
    Thanks for any advice given!
    (I am new to python)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    If I show you this statement:

    Code:
    x = y
    Then I ask you to tell me what value x is, can you tell me? If not, then how do you expect python to know what the value of z is in your code here:

    Code:
    z = realAns
    Simlarly, if I ask you what the length of the string stored in a variable called v is, can you tell me? Then how do you expect python to know the length of the variable def here:

    Code:
    for z in range (len(def)):
    Whatever you are doing to learn python is the WRONG WAY. Buy a beginning python book, read it, and do all the exercises. In the meantime, stick to programs like:

    Code:
    print "hello world"
    Last edited by 7stud; April 2nd, 2014 at 10:03 PM.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    On the off chance that you defined answers and realAns before this part of the program executes,

    def is a reserved word. You cannot use def as a variable name.
    Last edited by b49P23TIvg; April 2nd, 2014 at 10:34 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    ...which is the reason that all beginners should put 'my_' in front of all their variable names.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Tewksbury, MA
    Posts
    45
    Rep Power
    3
    Cross-post @ python-forum: python-forum.org

IMN logo majestic logo threadwatch logo seochat tools logo