Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help

Status
Not open for further replies.

paul1977

Programmer
Jun 10, 2011
2
GB
hi guys am very new to python and going through a couple of tutorials but not getting very far with them think its my age. So instaed i have decided to try and write a small programme and try and pick it up like that and it seems to be working. however i have become stuck now. I have typed the following code


#!/usr/bin/env python
#a simple question
keep_asking = True
while keep_asking:
a = raw_input("What Is My Name? ") # the question
print "paul"
if a == "Paul": # the answer
print ("Correct")
keep_asking = False
else:
print ("Wrong Try Again")


what i want to do is list 3 posible answers underneath the question can anyone hel. i know this is a simple and silly question but i cant figure it out thank you in advance
 
Hi

[tt]raw_input()[/tt] will wait for input and following lines will be executed only after it finished.

You will have to use a different approach, either
[ul]
[li]include the three hints in the [tt]raw_input()[/tt] prompt text[/li]
[li][tt]print[/tt] the question, [tt]print[/tt] the three hints, then [tt]raw_input()[/tt] the answer[/li]
[li]use a curses module to display the three hint at the desired position, then ask for the answer at another position[/li]
[/ul]


Feherke.
 
I just cant get my head around why you cant ask a simple question in the following order

question? answer
1. answer
2. answer
3.answer


seems very simple to me .
 
Read again what feherke said.
The simplest is to write the hints in the same line - something like this:
Code:
a = raw_input("What Is My Name? (John, Peter, Paul): ")
if a == "John": # the answer 
  print ("John is correct")
elif a == "Peter": # the answer 
  print ("Peter is correct")
elif a == "Paul": # the answer 
  print ("Paul is correct")
else:
  print ("Wrong Try Again")
 
looking at the request you could should probably a function for multiple choice questions which you can reuse as req

Code:
def multichoice(question,answeres,correct):
    print question
    for answer in answers:
        print answer
    guess=raw_input()
    return guess==correct
which you can hen use as follows
Code:
q="what is my name"
a=['paul','fred','john']
c='paul'
multichoice(q,a,c)

although you should probably take further steps to sort out any case sensitivity issues .



I do not Have A.D.D. im just easily, Hey look a Squirrel!
 
Hi

If you just want to have the answers below the question, you could use an ANSI escape sequence :
Python:
[COLOR=darkgoldenrod]raw_input[/color][teal]([/teal][green][i]"\n1. answer\n2. answer\n3. answer\n\033[4AQuestion ? "[/i][/green][teal])[/teal]
Note that depending on the environment and the terminal, the ANSI escapes may or may not work.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top