【发布时间】:2022-01-17 03:04:32
【问题描述】:
我想创建一个循环,为每个循环生成不同的方程。但是当我尝试创建代码时,它只创建了几次相同的方程式。
import random
count=range(random.randint(1,10))
terms_range=range(1,11)
num_terms=random.radint(1,10)
#This is where the calculation will be written
calculation=''
result_from_user=''
signs=''
def sign_generator(x):
#This function will randomly generate the sign
sign=random.randint(1,4)
if sign == 1:
x = '+'
return x
elif sign == 2:
x = '-'
return x
elif sign == 3:
x= '*'
return x
else:
x='/'
return x
for x in count:
while num_terms>0:
terms=random.randint(1,9)
terms=str(terms)
signs=sign_generator(signs)
if num_terms !=1:
calculation=calculation+terms+signs
else:
calculation=calculation+terms
num_terms-=1
print(calculation)
result_from_user=input('= ')
我应该如何修复我的代码?
【问题讨论】:
-
这段代码有错别字。您是否尝试过实际运行它?
-
代码对我有用,生成新的随机方程。 (同时在第 4 行修复该类型)
-
我已多次运行此代码,但每个循环的方程相同。并感谢您指出错字