User inputting real answer using numbers?

This is the place for queries that don't fit in any of the other categories.

User inputting real answer using numbers?

Postby aa1511 » Wed Apr 02, 2014 7:21 pm

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.
Code: Select all
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! :D
(I am new to python)
aa1511
 
Posts: 4
Joined: Wed Mar 26, 2014 12:30 pm

Re: User inputting real answer using numbers?

Postby stranac » Wed Apr 02, 2014 7:46 pm

If that's your real code, I would expect it to give you a SyntaxError...

You should always post a piece of code we can test without having to modify it.
Also, when you get an error, you should post the entire error traceback(in code tags as well).
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1212
Joined: Thu Feb 07, 2013 3:42 pm

Re: User inputting real answer using numbers?

Postby aa1511 » Wed Apr 02, 2014 7:49 pm

Sorry, this is the error
Code: Select all
Traceback (most recent call last):
  File "C:\Users\Alex\Desktop\Documents\Python - It1.py", line 63, in <module>
    if Correctone == realAns[z]:
IndexError: list index out of range
aa1511
 
Posts: 4
Joined: Wed Mar 26, 2014 12:30 pm

Re: User inputting real answer using numbers?

Postby stranac » Wed Apr 02, 2014 8:12 pm

And how about a small piece of code that reproduces your problem(with its own error)?
What you have shown is not even valid python, so it's hard to guess what might be going wrong.

If you can't create a small example, you can just post the code that matches that traceback.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1212
Joined: Thu Feb 07, 2013 3:42 pm

Re: User inputting real answer using numbers?

Postby Kebap » Thu Apr 03, 2014 11:48 am

aa1511 wrote:
Code: Select all
z = realAns
for z in range (len(def)):
            if realAns == def [z]:
                position2 = z

Welcome to python! I find this part can be done way easier. However, I am not yet sure what exactly you are doing there. Because I can't find a definition of your variable "def" anywhere. Then again, please abstain from naming your variables like original python commands ("def"). It tends to become very confusing! :mrgreen:

Consider this code:

Code: Select all
if my_answer in list_of_answers:
    print list_of_answers.index(my_answer)
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 397
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: User inputting real answer using numbers?

Postby stranac » Thu Apr 03, 2014 1:19 pm

Kebap wrote:Then again, please abstain from naming your variables like original python commands ("def")

Not only should you not, but you cannot name a variable def.
That's how I knew that wasn't the actual code.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1212
Joined: Thu Feb 07, 2013 3:42 pm

Re: User inputting real answer using numbers?

Postby Marbelous » Thu Apr 03, 2014 3:38 pm

Another cross-post from DevShed where he has already been answered: http://forums.devshed.com/python-progra ... 60569.html
Marbelous
 
Posts: 155
Joined: Fri May 31, 2013 8:12 pm

Re: User inputting real answer using numbers?

Postby Kebap » Thu Apr 03, 2014 6:09 pm

Marbelous wrote:Another cross-post
Thanks for pointing that out. :|
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 397
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: RodionGork and 7 guests