需要帮助输出

时间:2021-07-20 02:56:33

标签: 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

2 个答案:

答案 0 :(得分:0)

您使用过多的 if 来限制该功能。

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

代码:

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")

结果:

# 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 =

答案 1 :(得分:0)

你需要它来打印问题吗?例如,您可以让输入提出问题吗?

代码:

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")

这只会在提供答案后发布到终端,因此它包含正确或不正确的标签。