如何使用ifelse语句循环再次提问

2024-04-26 23:30:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让我的程序在有人答错问题时运行(即,用户输入的数字太大或太小,它将要求用户再次输入答案)。我尝试使用While True:loops,但这使我的程序有一个无限循环,永远不会结束。任何帮助都将不胜感激

import random

def main() :
    # Generate two random numbers between 1 and 100
    n1 = random.randint(1,100)
    n2 = random.randint(1,100)

    # Ask the answer
    print("What is the sum:",n1,"+",n2)
    attempt = 1
    answer = int(input("Answer: "))


    if answer != n1 + n2 and answer < n1 + n2:
        print("Wrong! The correct answer is greater than " + str(answer) + ".")
    else:
        if answer != n1 + n2 and answer > n1 + n2:
            print("Wrong! The correct answer is less than " + str(answer) + ".")
        else:
            if answer == n1 + n2:
                print("Yes! The correct answer is " + str(answer) + "." + "\n You got it in " + str(attempt) + " attempts.") 
                exit()

main()

Tags: andthe用户answer程序ifismain
2条回答

我们可以把代码缩短很多。。。我还添加了注释作为解释

import random

def main() :
    # Generate two random numbers between 1 and 100
    n1 = random.randint(1,100)
    n2 = random.randint(1,100)

    # Ask the answer
    print("What is the sum:",n1,"+",n2)
    attempt = 1
    answer = int(input("Answer: "))

    # keep looping until the answer is correct
    while answer != n1 + n2:
        # we already checked in the while loop that answer isn't == to n1+n2, so we don't need to check again
        if answer < n1 + n2:
            print("Wrong! The correct answer is greater than " + str(answer) + ".")
        # if answer != n1+n2 and answer is not < n1 + n2, it can only be answer > n1 + n2 so we don't need to explicitly state it 
        else:
            print("Wrong! The correct answer is less than " + str(answer) + ".")
        # increase the attempt number
        attempt += 1
        # ask the user again for an input
        answer = int(input("Answer: "))
    # once the while loop is done (when answer is == n1 + n2) then run this code below
    print("Yes! The correct answer is " + str(answer) + "." + "\n You got it in " + str(attempt) + " attempts.") 
    exit()
main()

没有我的评论:

import random

def main() :
    # Generate two random numbers between 1 and 100
    n1 = random.randint(1,100)
    n2 = random.randint(1,100)

    # Ask the answer
    print("What is the sum:",n1,"+",n2)
    attempt = 1
    answer = int(input("Answer: "))

    while answer != n1 + n2:
        if answer < n1 + n2:
            print("Wrong! The correct answer is greater than " + str(answer) + ".")
        else:
            print("Wrong! The correct answer is less than " + str(answer) + ".")
        attempt += 1
        answer = int(input("Answer: "))
    print("Yes! The correct answer is " + str(answer) + "." + "\n You got it in " + str(attempt) + " attempts.") 
    exit()
main()

此解决方案将适用于多个错误答案,而不会陷入无休止的循环。无论如何,给你:

def main():
    # Generate two random numbers between 1 and 100
    n1 = random.randint(1,100)
    n2 = random.randint(1,100)
    attempt = 1
    # Ask the answer
    print("What is the sum:",n1,"+",n2)

    while True:
        answer = int(input("Answer: "))
        if answer < n1 + n2:
            print("Wrong! The correct answer is greater than " + str(answer) + ".")
            attempt += 1
            continue
        elif answer > n1 + n2:
            print("Wrong! The correct answer is less than " + str(answer) + ".")
            attempt += 1
            continue
        elif answer == n1 + n2:
            print("Yes! The correct answer is " + str(answer) + "." + "\n You got it in " + str(attempt) + " attempts.")
            break

main()

干杯

相关问题 更多 >