2016-06-10 47 views
-2

在这段代码中,我想创建10个随机问题,但是如果操作符号(+, - 或*)是 - 那么我想要第一个数字要大于第二个(所以答案会是积极的。但是,这似乎并没有工作,因为我得到的是“ - ”问题,其中y小于x。这个代码的目的是针对y,r和Z都可以随机生成的。使用while循环和random.randint(python 3.4.3)制作y> x

import random 
    name=input("What is you name: ") 
    x=0 
    d=0 
    op=['+','-','*'] 

    def easy(): 
     global x 
     global d 
     y=random.randint(1,12) 
     z=random.randint(1,12) 
     r=op[random.randint(0,2)] 
     if (r==1): 
      while (y<z): 
       y=random.randint(1,12) 

     answer=eval('{}{}{}'.format(y,r,z)) 
     question=int(input("What is {}{}{}?".format(y,r,z))) 
     if question==answer: 
      print("Correct") 
      x+=1 
     else: 
      print("Incorrect") 
     d+=1 
     while d<10: 
      easy() 
    easy() 

回答

0

你跟1比较r,但你的运营商已经分配了。与'-'而不是进行比较。

+0

非常感谢你 – Woli123

-1

试试这个:

import random 
name=input("What is you name: ") 
x=0 
d=0 
op=['+','-','*'] 

def easy(): 
    global x 
    global d 
    y=random.randint(1,12) 
    z=random.randint(1,12) 
    r=op[random.randint(0,2)] 
    if (r=='-'): # << change from if (r==1) 
     while (y<=z): # << what if z is 12? 
      print(y, z) 
      y=random.randint(1,12) 

    answer=eval('{}{}{}'.format(y,r,z)) 
    question=int(input("What is {}{}{}?".format(y,r,z))) 
    if question==answer: 
     print("Correct") 
     x+=1 
    else: 
     print("Incorrect") 
    d+=1 
    while d<10: 
     easy() 
easy() 
+0

谢谢,我会把你的建议纳入考虑 – Woli123

+0

无论如何,好的工作和你的项目的好运。 –

0

而不是循环找到一对值,为什么不生成它们与所需的属性?

import random as rnd 

smaller, larger = sorted([rnd.randint(1,11), rnd.randint(1,12)]) 
if larger == smaller: 
    larger += 1 

由于在范围(1,11)中产生的第一个值,如果两个值相等总有空间来增加其中的一个。