简体   繁体   English

当两件事正确时如何打印信息,以及在这两件事不正确时如何使用输入功能

[英]How to print info when 2 things are correct and to use the input fuction when the those two things are incorrect

I am doing a controlled assessment. 我正在进行对照评估。 I have this code : 我有这个代码:

# user Qualifications
print("\nQualification Level")

print("\n""\"AP\" = Apprentice",
      "\n\"FQ\" = Fully-Qualified")
user_qual = input("Enter you Qualification Level: ")

When the user_qual is equal to AP or FQ I want to print "correct". user_qual等于APFQ我要打印“正确”。 But when the user_qual is not equal to either AP or FQ , I want to print an error message and re-ask the user_qual input question. 但是,当user_qual不等于APFQ ,我想打印一条错误消息并重新询问user_qual输入问题。 However, I tried many different ways but couldn't do it. 但是,我尝试了许多不同的方法,但无法做到。

Please just give a simple solution and nothing complicated. 请只给出一个简单的解决方案,而没有什么复杂的。 I am just a beginner and learning the basics. 我只是一个初学者,正在学习基础知识。

You can try defining the qualification levels within a variable. 您可以尝试在变量中定义资格级别。 In this simple case a tuple suffices, but if there were to be more qualification levels, a dictionary would make more sense. 在这种简单的情况下,一个元组就足够了,但是如果要有更多的资格等级,那么字典会更有意义。

qualification_levels = ('AP', 'FQ')
print("\n""\"AP\" = Apprentice",
      "\n\"FQ\" = Fully-Qualified")
user_qual = input("Enter you Qualification Level: ")
if user_qual in qualification_levels:
    print('correct!')
else:
    print('ERROR!!!')

You need to turn your code into a function so it can be called again, if the criteria are not met: 如果不符合条件,则需要将代码转换为函数,以便可以再次调用它:

def prompt():
    # user Qualifications
    print('\nQualification Level')
    print('\n"AP" = Apprentice\n"FQ" = Fully-Qualified')

    user_qual = input('Enter you Qualification Level: ')

    if user_qual not in ('AP','FQ'):
        prompt() # Criteria not met, so ask again
    print('Correct!') # Criteria met, don't ask again


# Call the function the first time
prompt()

As @IanAuld mentions in the comments, if you want to allow users to provide the answer (ie. AP) in a case insensitive manner (so users can type 'ap' instead), you would want to convert the case before evaluating it, like so: 正如@IanAuld在评论中提到的那样,如果您希望允许用户以不区分大小写的方式提供答案(即AP)(以便用户可以键入“ ap”),则您需要先转换大小写,然后再进行评估,像这样:

if user_qual.upper() not in ('AP','FQ'):
    ...rest of code...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM