退出个人if elif陈述

时间:2017-08-22 07:34:52

标签: python

我有一个程序可以生成一个随机问题,以获取一个数字然后显示给出的答案是对还是错。

例如: 如果它要求输入正数,您可以输入一个数字,然后显示该数字是否为正数。

所以我知道它能正确获取输入并能给出正确的答案,但最终会给出多个答案。

继承人看到的代码:

import random

def RandomQuestions()
    questionsList=["Enter a number thats not prime:",
                   "Enter a number thats prime:",
                   "Enter a number thats odd:",
                   "Enter a number thats even:",
                   "Enter a number thats positive:",
                   "Enter a number thats negative:",]

    question = (random.choice(questionsList))

    print(question)

    getnum= int(input(""))

    if question [0]:
        if getnum <= 1:
            print("Correct", getnum, "is not prime.")
        elif getnum <=3:
            print("Incorrect",getnum,"is prime.")
        elif getnum%2==0 or getnum%3==0:
            print("Correct", getnum, "is not prime.")
        else:
            print("Incorrect",getnum,"is prime.")

    if question [1]:
        if getnum <= 1:
            print("Incorrect", getnum, "is not prime.")
        elif getnum <=3:
            print("Correct",getnum,"is prime.")
        elif getnum%2==0 or getnum%3==0:
            print("Incorrect", getnum, "is not prime.")
        else:
            print("Correct",getnum,"is prime.")

    if question [2]:
        if (getnum%2)==0:
            print("Incorrect",getnum,"is even.")
        else:
            print("Correct",getnum,"is odd.")

    if question [3]:
        if (getnum%2)==0:
            print("Correct",getnum,"is even.")
        else:
            print("Incorrect",getnum,"is odd.")

    if question [4]:
        if (getnum>0):
            print("Correct",getnum,"is positive.")
        else:
            print("Incorrect",getnum,"is negative.")

    if question [5]:
        if (getnum>0):
            print("Incorrect",getnum,"is positive.")
        else:
            print("Correct",getnum,"is negative.")import random

RandomQuestions()

因此,如果您在计划中最终为所有问题提供答案。如果它与问题列表无关,我如何让它退出if语句?

4 个答案:

答案 0 :(得分:0)

变量question包含一个字符串。这意味着question[0]包含字符串的第一个字符,question[1]第二个字符串,依此类推。并且一个char将始终评估为True

解决方案是使用if question == questionsList[0]:代替if question[0]:等等......

答案 1 :(得分:0)

我认为你的if语句总是返回true并且其中的代码块总是被执行,请尝试:

if question == questionsList[0]:
    ...
elif question == questionsList[1]:
    ...

答案 2 :(得分:0)

在执行question[0]question[1]时,您正在访问问题文本中该位置的字符(例如:E然后n等等() - 非空字符串总是等于True,这就是为什么所有if都在运行的原因。

更优雅的方法是在做出选择时配对问题及其验证器,然后在转换为整数后调用输入上的验证器,例如:

from random import choice

def is_prime(number):
    # Write your prime checker here
    # Return True if prime or False if not
    pass

QUESTIONS = (
    # (text for question, callable to check if correct)
    ('not prime: ', lambda L: not is_prime(L)),
    ('prime: ', is_prime),
    ('odd: ', lambda L: L % 2 != 0),
    ('even: ', lambda L: L % 2 == 0),
    ('positive: ', lambda L: L > 0),
    ('negative: ', lambda L: L < 0)
)


question, answer_is_correct = choice(QUESTIONS)
number = int(input(question))
print('correct' if answer_is_correct(number) else 'wrong')

答案 3 :(得分:0)

你的coode有一些纠正。这将帮助您关联python中if和elif的流程。

import random

def RandomQuestions():
    questionsList=["Enter a number thats not prime:",
               "Enter a number thats prime:",
               "Enter a number thats odd:",
               "Enter a number thats even:",
               "Enter a number thats positive:",
               "Enter a number thats negative:",]

    question = (random.choice(questionsList))

    print(question)

    getnum= int(input(""))

    if question  == questionsList[0]:
        if getnum <= 1:
            print("Correct", getnum, "is not prime.")
        elif getnum <=3:
            print("Incorrect",getnum,"is prime.")
        elif getnum%2==0 or getnum%3==0:
            print("Correct", getnum, "is not prime.")
        else:
            print("Incorrect",getnum,"is prime.")



    elif question == questionsList[1]:
        if getnum <= 1:
            print("Incorrect", getnum, "is not prime.")
        elif getnum <=3:
            print("Correct",getnum,"is prime.")
        elif getnum%2==0 or getnum%3==0:
            print("Incorrect", getnum, "is not prime.")
        else:
            print("Correct",getnum,"is prime.")


    elif question == questionsList[2]:
        if (getnum%2)==0:
            print("Incorrect",getnum,"is even.")
        else:
            print("Correct",getnum,"is odd.")


    elif question == questionsList[3]:
        if (getnum%2)==0:
            print("Correct",getnum,"is even.")
        else:
            print("Incorrect",getnum,"is odd.")


    elif question == questionsList[4]:
        if (getnum>0):
            print("Correct",getnum,"is positive.")
        else:
            print("Incorrect",getnum,"is negative.")


    elif question == questionsList[5]:
        if (getnum>0):
            print("Incorrect",getnum,"is positive.")
        else:
            print("Correct",getnum,"is negative.")

RandomQuestions()