python随机口算题(python随机出算术题)
编程口算题输答案后判断对错,Python编程语句怎么写?
import random
flag = ["+","-","×","÷"]
a1 = random.randint(0,9)
a2 = random.randint(0,9)
i_flag = random.randint(0,3)
str = f"{a1} {flag[i_flag]} {a2} = "
result = None
if flag[i_flag] == "+":
?result = a1 + a2
elif flag[i_flag] == "-":
? ?result = a1 - a2
elif flag[i_flag] == "×":
? result = a1 * a2
elif flag[i_flag] == "÷":
? result = a1 / a2
else:
? pass
ins = input(str)
if f"{result}" == ins:
?print(f"{ins} is correct! ")
else:
?print(f"{ins} is wrong, right answer is : {str}{result}")
Python 随机产生两个1位整数,提示用户输入两个数的和,判断用户输入是否正确并?
import random
oneNum= random.randint(0,9)
twoNum = random.randint(0,9)
tempNum= oneNum+twoNum
cusNum= input("用户输入")
if cusNum ==tempNum:
print(“正确”)
else:
print(“错误”)
如何用Python生成计算题?
这道题我回答过的,不知道怎么被删除了。下面是上次回答的代码。
import?random
cnt_n?=?int(input("题目数量:"))
max_n?=?int(input("最大数字:"))
#?产生乘法查找表
times_list?=?[]
for?i?in?range(max_n):
????for?j?in?range(max_n):
????????if?i?*?j?=?max_n:
????????????qst?=?str(i)?+?"*"?+?str(j)
????????????rst?=?str(i*j)
????????????if?qst?not?in?times_list:
????????????????times_list.append([qst,?rst])
#?题目表
question_list?=?[]
for?_?in?range(cnt_n):
????#?随机运算,0+,1-,2*,3/
????op?=?random.randint(0,?3)
????#?+
????if?op?==?0:
????????x1?=?random.randint(0,?max_n)
????????x2?=?random.randint(0,?max_n?-?x1)
????????result?=?x1?+?x2
????????qst?=?str(x1)?+?"+"?+?str(x2)?+?"="
????????question_list.append([qst,?result])
????#?-
????elif?op?==?1:
????????x1?=?random.randint(0,?max_n)
????????x2?=?random.randint(0,?x1)
????????result?=?x1?-?x2
????????qst?=?str(x1)?+?"-"?+?str(x2)?+?"="
????????question_list.append([qst,?result])
????#?*
????elif?op?==?2:
????????tmp?=?random.sample(times_list,?1)
????????qst?=?tmp[0][0]?+?"="
????????question_list.append([qst,?tmp[0][1]])
????#?/
????elif?op?==?3:
????????tmp?=?random.sample(times_list,?1)
????????x1,?x2?=?tmp[0][0].split("*")
????????result?=?tmp[0][1]
????????while?int(x1)?==?0:
????????????tmp?=?random.sample(times_list,?1)
????????????x1,?x2?=?tmp[0][0].split("*")
????????????result?=?tmp[0][1]
????????qst?=?result?+?"/"?+?x1?+?"="
????????question_list.append([qst,?x2])
print("开始回答:")
wrong?=?""
for?i,?qst?in?enumerate(question_list):
????x?=?input("第{:2d}题:{}".format(i+1,?qst[0]))
????if?int(x)?!=?int(qst[1]):
????????wrong?+=?str(i+1)?+?"??"
w_item?=?wrong.split()
print("回答正确{}道题目。".format(len(question_list)?-?len(w_item)))
print("回答错误的题号是:{}".format(wrong))
求大神帮助两道Python题?
3.1题
df1?=?pd.read_csv('num.text',sep=',header=None)
df1.columns?=?['N']
print(df1[df1.N??0?].shape[0],df1[df1.N?==?0?].shape[0],df1[df1.N??0?].shape[0])
3.2题目
from?random?import?randint,?seed
seed(8)
output?=?[]
for?i?in?range(100):
output.append(random.randint(10,99))
df2?=?pd.Dataframe(output,?columns?=?['N'])
df2.to_csv('d:\num1.txt')
df2?=?pd.read_csv('d:\num1.txt')
df2?=?df2.sort_values(by='N')
df2.to_csv('d:\num2.txt')
Python使用random函数计算100以内加减法,没有题数限制的那种
import?random
max_n?=?100
i?=?0
while?True:
????i?+=?1
????#?随机运算,0+,1-
????op?=?random.randint(0,?1)
????#?+
????if?op?==?0:
????????x1?=?random.randint(0,?max_n)
????????x2?=?random.randint(0,?max_n?-?x1)
????????result?=?x1?+?x2
????????qst?=?str(x1)?+?"+"?+?str(x2)?+?"="
????????question?=?[qst,?result]
????#?-
????elif?op?==?1:
????????x1?=?random.randint(0,?max_n)
????????x2?=?random.randint(0,?x1)
????????result?=?x1?-?x2
????????qst?=?str(x1)?+?"-"?+?str(x2)?+?"="
????????question?=?[qst,?result]
????x?=?input("第{:2d}题:{}".format(i?+?1,?question[0]))
????if?int(x)?==?int(question[1]):
????????print("回答正确!")
????else:
????????print("回答错误!{}{}".format(question[0],?question[1]))
python在一定范围内随机抽取两个整数,如何保证第一个是第二个的整数倍
在2至n2范围内,查找可以整除n1的整数,仅供参考。
import?random
n1?=?random.randint(10,?20)
n2?=?random.randint(1,?10)
print(n1,n2)
for?i?in?range(2,?n2+1):
????if?(n1?%?i)?==?0:
????????print(n1,?i)