Python:从一点继续循环

时间:2015-03-02 08:07:43

标签: python

我刚刚开始了一个Python编程课程,并试图一步一步走。我刚刚为自己创建了一个小西班牙语 - 英语提醒,我现在正面临着一个不足之处。我用英语问一个问题,需要用西班牙语翻译。如果答案是正确的,我可以继续,但如果答案是假的,我想再问qeustion ..这是我的代码到目前为止,我的问题如果我给出了一个错误的答案,那么循环会回到第一个问题。你能帮助我,我只想重复一遍我没有正确回答的问题吗?

谢谢,

while True:
    ask = raw_input('How are you? ')
    answer = str(ask)
    if answer == 'Que tal?':
        ask = raw_input(' Sorry, I have other programs ')
        answer = str(ask)
    if answer == 'Lo siento, pero ya tengo otros planes':
        ask = raw_input(' I am reading a book ')
        answer = str(ask)
    else:
        continue 

1 个答案:

答案 0 :(得分:0)

  1. 创建questions及其answers的列表。将此详细信息保存在任何变量中列表。
  2. 使用for loop来迭代问题列表项,并使用raw_input()提问。
  3. 通过if condition
  4. 检查用户答案是否正确
  5. 如果用户回答错误,请再次向用户提出同样的问题。
  6. 如果用户答案正确,请询问下一个问题。
  7. 使用break语句从while循环中出来。
  8. <强>限制: 如果用户没有对任何问题给出正确的答案,那么程序会一次又一次地询问同一问题,直到用户给出正确答案。

    演示:

    #- Create List of tuples which contains question and respective answer.    
    questions = [("Question number one?", "one"), ("Question number two?", "two"),\
                 ("Question number three?", "three")]
    
    for i in questions:
        while 1:
            ans = raw_input(i[0])
            if ans==i[1]:
                print "Correct."
                break
            else:
                print "Wrong answer"
    

    输出:

    infogrid@infogrid-vivek:~/workspace/vtestproject$ 
    infogrid@infogrid-vivek:~/workspace/vtestproject$ python task4.py 
    Question number one?test1
    Wrong answer
    Question number one?test1
    Wrong answer
    Question number one?one
    Correct.
    Question number two?two
    Correct.
    Question number three?test3
    Wrong answer
    Question number three?three
    Correct.
    

    多项选择方程和答案。

    演示:

    #- Create List of tuples which contains question. optinons and  respective answer.
    questions = [("Question number one?", ["a. one", "b. two", "c. three"], "a"),\
                 ("Question number two?", ["a. one", "b. two", "c. three"], "b"),\
                 ("Question number three?", ["a. one", "b. two", "c. three"], "c")
                 ]
    
    #- Set number of chances to solve each question.
    chances = 3
    #- Score. Plus one for each correct answer.
    score = 0
    
    for i in questions:
        print "-"*10
        for j in xrange(0, chances):
            print "Question:", i[0], "\n Options:", i[1]
            ans = raw_input("Enter option:")
            if ans==i[2]:
                print "Correct."
                score += 1
                break
            else:
                print "Wrong answer"
    
    
    print "Your score: ", score
    

    输出:

    ----------
    Question: Question number one? 
     Options: ['a. one', 'b. two', 'c. three']
    Enter option:b
    Wrong answer
    Question: Question number one? 
     Options: ['a. one', 'b. two', 'c. three']
    Enter option:c
    Wrong answer
    Question: Question number one? 
     Options: ['a. one', 'b. two', 'c. three']
    Enter option:b
    Wrong answer
    ----------
    Question: Question number two? 
     Options: ['a. one', 'b. two', 'c. three']
    Enter option:b
    Correct.
    ----------
    Question: Question number three? 
     Options: ['a. one', 'b. two', 'c. three']
    Enter option:s
    Wrong answer
    Question: Question number three? 
     Options: ['a. one', 'b. two', 'c. three']
    Enter option:c
    Correct.
    Your score:  2