python 我如何使用函数缩短我的代码,如果用户不输入'TRUE'或'FALSE',是否有任何方法使布尔值重复

8iwquhpp  于 2023-04-28  发布在  Python
关注(0)|答案(1)|浏览(76)

此问题已在此处有答案

Asking the user for input until they give a valid response(22答案)
3小时前关闭

import random

CorrectResponces = ['Congrats', 'Good Job', 'You got it', "You're on fire"]

IncorrectResponces = ['Better luck next itme', 'Think a little harder',
                      'Wow are you stupid', 'How did you get this one wrong']

FinalResponces = ["You wouldn't even be able to get participation points", 'You are really really bad at this',
                  "You didn't do absolutely terrible but not good either", "Good enough job ig"]

def True_Or_False():

    i = 1
    CorrectVal = 0
    WrongVal = 0

    Start = input('Enter "START" to begin =>')
    if Start == "START":

        while i <= 3:

            VGQuestions = random.randint(1, 3)

            if VGQuestions == 1:
                Answer = input(
                    '"The Legend of Zelda Breath of the Wild" Released in 2017 =>')
                i = i + 1
                if Answer == 'TRUE':
                    print(random.choice(CorrectResponces))
                    CorrectVal = CorrectVal + 1
                elif Answer == 'FALSE':
                    print(random.choice(IncorrectResponces))
                    WrongVal = WrongVal + 1
                else:
                    print('Please enter "TRUE" or "FALSE"')

            if VGQuestions == 2:
                Answer = input(
                    '"Super Smash Bros Ultimate" has over 100 playable characters =>')
                i = i + 1
                if Answer == 'FALSE':
                    print(random.choice(CorrectResponces))
                    CorrectVal = CorrectVal + 1
                elif Answer == 'TRUE':
                    print(random.choice(IncorrectResponces))
                    WrongVal = WrongVal + 1
                else:
                    print('Please enter "TRUE" or "FALSE"')

            if VGQuestions == 3:
                Answer = input(
                    'There are over 500 obtainable pokemon across all the games as of 2023 =>')
                i = i + 1
                if Answer == 'TRUE':
                    print(random.choice(CorrectResponces))
                    CorrectVal = CorrectVal + 1
                elif Answer == 'FALSE':
                    print(random.choice(IncorrectResponces))
                    WrongVal = WrongVal + 1

        if WrongVal == 3:
            print(FinalResponces[0])
        elif WrongVal == 2:
            print(FinalResponces[1])
        elif WrongVal == 1:
            print(FinalResponces[2])
        elif WrongVal == 0:
            print(FinalResponces[3])

        FinalScore = f'Correct {CorrectVal}; Wrong {WrongVal}'
        print(FinalScore)

    else:
        print('Please type "START" to begin')
        True_Or_False()

True_Or_False()

我想布尔循环时,'真'或'假'没有输入。我试着把这部分代码放在一个函数中,然后在else之后声明这个函数,但它不起作用。

91zkwejq

91zkwejq1#

import random
CorrectVal = 0
WrongVal = 0

CorrectResponces = ['Congrats', 'Good Job', 'You got it', "You're on fire"]

IncorrectResponces = ['Better luck next itme', 'Think a little harder', 'Wow are you stupid', 'How did you get this one wrong']

FinalResponces = ["You wouldn't even be able to get participation points", 'You are really really bad at this', "You didn't do absolutely terrible but not good either", "Good enough job ig"]

def logic(i):
    
    global CorrectVal
    global WrongVal
    global VGQuestions
    global CorrectResponces
    global IncorrectResponces
    global FinalResponces
    
    if VGQuestions == 1:
        Answer = input(
            '"The Legend of Zelda Breath of the Wild" Released in 2017 =>')
        i = i + 1
        if Answer == 'TRUE':
            print(random.choice(CorrectResponces))
            CorrectVal = CorrectVal + 1
        elif Answer == 'FALSE':
            print(random.choice(IncorrectResponces))
            WrongVal = WrongVal + 1
        else:
            print('Please enter "TRUE" or "FALSE"')
        
i = 1

Start = input('Enter "START" to begin =>')
if Start == "START":

    while i <= 3:

        VGQuestions = random.randint(1, 3)

        if VGQuestions == 1:
            logic(1)

        if VGQuestions == 2:
            logic(2)

        if VGQuestions == 3:
            logic(3)

    if WrongVal == 3:
        print(FinalResponces[0])
    elif WrongVal == 2:
        print(FinalResponces[1])
    elif WrongVal == 1:
        print(FinalResponces[2])
    elif WrongVal == 0:
        print(FinalResponces[3])

    FinalScore = f'Correct {CorrectVal}; Wrong {WrongVal}'
    print(FinalScore)

else:
    print('Please type "START" to begin')
    True_Or_False()

相关问题