这有什么不对?

时间:2014-12-17 10:22:58

标签: python

import random
import time

operators = ["+", "-","*"]

score = 0
name = input ("What is your name?")
print ("Welcome to the quiz",name,)
print ("-------------------------------------------------")

question =0

while question<10:
     question = question+1
     number1 = random.randint(1,10)
     operator = random.choice(operators)                               
     number2 = random.randint(1,10)
     question = ("%d %s %d") % (number1, operator, number2)
     print("What is",question,"?")
     answer = int(input())

     if answer == eval (question):
          score =score+1
          print("Correct!")
     else:
          print("That's incorrect!")

print ("-------------------------------------------------")

print("Welldone",name, "you scored",score,"out of 10")

然后它说

Traceback (most recent call last):   
  File "\\shbvfs01.derbyshire.local\studentdocs\2011\DBooth\Documents\Computing\Controlled Assessment\A453\Python - Maths Quiz Version2 (Different altogether).py", line 14, in <module>
    while question<10: 
TypeError: unorderable types: str() < int()

2 个答案:

答案 0 :(得分:3)

您正在用行上的字符串覆盖while循环控制变量question

question = ("%d %s %d") % (number1, operator, number2)

您收到的错误消息告诉您第14行:

while question < 10:

无法使用数字文字对字符串进行排序。

请学会阅读您收到的错误消息,并记住明智而谨慎地命名变量。

答案 1 :(得分:0)

首先看到循环问题=(“%d%s%d”),所以问题现在是一个str。 然后尝试继续比较条件,(问题&lt; 10),有str&lt; INT。