我的Python程序中的错误在哪里?

时间:2017-05-23 07:50:01

标签: python-3.6

我的代码中的错误在哪里?我无法找到它们。我不得不制作一个乘法学习程序,但我现在陷入困境。

    import random

n = int(input("How many exercises?"))
wrong = 0
abort = False
for k in range(n):
    a = random.randint(2, 13)
    b = random.randint(2, 13)
    help = str(k+1) + ". " + str(a) + " * " + str(b)
    answer = input(help + "=> ")
    if answer == "":
        abort = True
        break
    if answer !=a * b:
        print("Wrong!")
        wrong += 1
    if abort == True:
        print("Noob")
            return
if wrong == 0:
    print("Good")

else:
    print("Hale" ,wrong)

2 个答案:

答案 0 :(得分:0)

有两个错误,返回不在函数中,vigu变量在该点没有值,你永远不会到达

如果abort == True:     打印("菜鸟&#34)         返回

因为在此之前你设置abort = True然后你休息一下,

答案 1 :(得分:0)

稍微纠正了您的代码:

  1. 缩进代码;这非常重要
  2. 您不能在函数外使用“return”语句
  3. 如果要将其与“a * b”进行比较或将a * b转换为字符串
  4. ,请将“回答”转换为int

    以下是我所做的修正:

    import random
    
    n = int(input("How many exercises?"))
    wrong = 0
    abort = False
    for k in range(n):
        a = random.randint(2, 13)
        b = random.randint(2, 13)
        help = str(k+1) + ". " + str(a) + " * " + str(b)
        answer = input(help + "=> ")
        if answer == "":
          abort = True
          break
        if answer != str(a * b):
          print("Wrong!")
          wrong += 1
        if abort == True:
          print("Noob")
    if wrong == 0:
        print("Good")
    
    '''Don't know what you wanted to do here
    else:
      print("Hale" ,vigu)'''