需要帮助输出吗

2024-09-27 09:28:58 发布

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

我正在做一个数学测验,需要帮助输出

Q1: 31 + 11 = **42**            Correct

其中42由用户输入,然后在同一行中输出正确和不正确

import random

# setting up variables that i will need

NUMEBR_OF_QUES = 0
count = 0
correctCount = 0
incorrectCount = 0
levelOne = 1
levelTwo = 2
levelThree = 3
addition = 1
subtraction = 2

# Start asking user questions to set up code

quizType = int(input("Available quiz type: Enter 1 for addition and 2 for subtraction: "))

diffSelection = int(input("Difficulty Level: Enter 1 for 1-digit, 2 for 2-digit, and 3 for 3-digit: "))


# This is if the user chooses addition
while NUMEBR_OF_QUES < 10 and diffSelection == 1 and quizType == 1:
    levOneQues = random.randint(0, 9)
    levOneQuesTwo = random.randint(0, 9)
    NUMEBR_OF_QUES = NUMEBR_OF_QUES + 1
    if quizType == addition and diffSelection == levelOne:
        count = count + 1
        print("Q", count, ": ", levOneQues, " + ", levOneQuesTwo, "=")
        total = int(levOneQues + levOneQuesTwo)
        anwser = int(input())
        
        if anwser == total:
            correctCount = correctCount + 1
            print("Q", count, ": ", levOneQues, " + ", levOneQuesTwo, "= ", anwser, "Correct")
        else:
            incorrectCount = incorrectCount + 1
            print("Q", count, ": ", levOneQues, " + ", levOneQuesTwo, "= ", anwser, "Incorrect")

出于测试的目的,我把1作为第一个问题,第二个问题,输出如下

Available quiz type: Enter 1 for addition and 2 for subtraction: 1
Difficulty Level: Enter 1 for 1-digit, 2 for 2-digit, and 3 for 3-digit: 3
Q 1 :  278  +  761 =
3
Q 1 :  278  +  761 =  3 Incorrect

Tags: andofforcountrandomintenterdigit
2条回答

你需要它来打印问题吗?例如,您是否可以将输入改为提问

代码:

import random

# setting up variables that i will need

NUMEBR_OF_QUES = 0
count = 0
correctCount = 0
incorrectCount = 0
levelOne = 1
levelTwo = 2
levelThree = 3
addition = 1
subtraction = 2

# Start asking user questions to set up code

quizType = int(input("Available quiz type: Enter 1 for addition and 2 for subtraction: "))

diffSelection = int(input("Difficulty Level: Enter 1 for 1-digit, 2 for 2-digit, and 3 for 3-digit: "))


# This is if the user chooses addition
while NUMEBR_OF_QUES < 10 and diffSelection == 1 and quizType == 1:
    levOneQues = random.randint(0, 9)
    levOneQuesTwo = random.randint(0, 9)
    NUMEBR_OF_QUES = NUMEBR_OF_QUES + 1
    if quizType == addition and diffSelection == levelOne:
        count = count + 1
        total = int(levOneQues + levOneQuesTwo)
        answer = int (input ("Q " + str(count) + " : " + str(levOneQues) + " + " + str(levOneQuesTwo) + " = "))
        
        if anwser == total:
            correctCount = correctCount + 1
            print("Q", count, ": ", levOneQues, " + ", levOneQuesTwo, "= ", anwser, "Correct")
        else:
            incorrectCount = incorrectCount + 1
            print("Q", count, ": ", levOneQues, " + ", levOneQuesTwo, "= ", anwser, "Incorrect")

这只会在提供应答后发送到终端,因此它包括正确或不正确的标签

您使用了太多的if来限制功能。

  • while NUMEBR_OF_QUES < 10 and diffSelection == 1 and quizType == 1while循环只在执行add 1-digit时才进入,因此3-digit不起作用
  • if quizType == addition and diffSelection == levelOne:也只有在执行{}操作时才可以输入
  • 尝试f-string而不是用逗号打印
  • 如果您想将所有内容都放在一行中,那么print(chr(27) + "[2J")newline = " "可能会有所帮助
  • 如果只想清除一行,可以使用print ("\033[A \033[A")

code:

import random
NUMEBR_OF_QUES = 0
count = 0
correctCount = 0
incorrectCount = 0
levelOne = 1
levelTwo = 2
levelThree = 3
addition = 1
subtraction = 2
quizType = int(input("Available quiz type: Enter 1 for addition and 2 for subtraction: "))
diffSelection = int(input("Difficulty Level: Enter 1 for 1-digit, 2 for 2-digit, and 3 for 3-digit: "))
while NUMEBR_OF_QUES < 10:
    levOneQues = random.randint(0, 10**diffSelection-1)
    levOneQuesTwo = random.randint(0, 10**diffSelection-1)
    NUMEBR_OF_QUES = NUMEBR_OF_QUES + 1
    count = count + 1
    print(f"Q {count} :  {levOneQues}  {'+' if quizType == addition else '-'}  {levOneQuesTwo} =",end =" ")
    total = int(levOneQues + levOneQuesTwo) if quizType == addition else int(levOneQues - levOneQuesTwo)
    anwser = int(input())
    print ("\033[A                             \033[A")
    if anwser == total:
        correctCount = correctCount + 1
        print(f"Q {count} :  {levOneQues}  {'+' if quizType == addition else '-'}  {levOneQuesTwo} = {anwser} Correct")
    else:
        incorrectCount = incorrectCount + 1
        print(f"Q {count} :  {levOneQues}  {'+' if quizType == addition else '-'}  {levOneQuesTwo} = {anwser} Incorrect")

result:

# before press enter
Available quiz type: Enter 1 for addition and 2 for subtraction: 1
Difficulty Level: Enter 1 for 1-digit, 2 for 2-digit, and 3 for 3-digit: 2
Q 1 :  97  +  22 = 119

# after press enter
Available quiz type: Enter 1 for addition and 2 for subtraction: 1
Difficulty Level: Enter 1 for 1-digit, 2 for 2-digit, and 3 for 3-digit: 2
Q 1 :  97  +  22 = 119 Correct     
Q 2 :  7  +  99 = 

# before press enter
Available quiz type: Enter 1 for addition and 2 for subtraction: 2
Difficulty Level: Enter 1 for 1-digit, 2 for 2-digit, and 3 for 3-digit: 2
Q 1 :  50  -  3 = 0
# after press enter
Available quiz type: Enter 1 for addition and 2 for subtraction: 2
Difficulty Level: Enter 1 for 1-digit, 2 for 2-digit, and 3 for 3-digit: 2
Q 1 :  50  -  3 = 0 Incorrect      
Q 2 :  19  -  8 =

相关问题 更多 >

    热门问题