Python 如何将数字转换为符号(数学)
How to Convert Number into Signs (Math) in Python
我正在尝试使用两个随机数 (1, 10) 随机选择和、差或积来进行数学测验。我使用 z = random.randint(1, 3)
生成和、差或乘积,但我想使用此数字转换为 "x"、“/”或“+”等符号来显示输出以提问,因为我是新手Python 语言,我正在尝试学习如何将数字转换为符号。
我的代码在这里:
import random
def askNum():
while(1):
try:
userInput = int(input("Enter a number: "))
break
except ValueError:
print("Incorrect Input!")
return userInput
def askQuestion():
x = random.randint(1, 10)
y = random.randint(1, 10)
z = random.randint(1, 3)
print(" 1 = product \n 2 = sum \n 3 = difference")
print("What is " + str(x)+" " + str(z)+" " + str(y)+"?")
u = askNum()
if z == 1 and u==x*y:
return 1 #product
elif z == 2 and u==x+y:
return 1 #sum
elif z == 3 and u==x/y:
return 1 #difference
else:
return 0
amount = 10
correct = 0
for i in range(amount):
correct += askQuestion()
print("You got %d correct out of %d" % (correct, amount))
现实输出:
dm15125@isu:/u1/work/Python/math> python3 mathquiz.py
1 = product
2 = sum
3 = difference
What is 4 2 6?
Enter a number: 10
1 = product
2 = sum
3 = difference
What is 7 2 6?
Enter a number: 13
1 = product
2 = sum
3 = difference
What is 3 2 3?
Enter a number: 6
1 = product
2 = sum
3 = difference
What is 8 3 4?
Enter a number: 2
1 = product
2 = sum
3 = difference
What is 8 3 10?
Enter a number: 0.8
Incorrect Input!
Enter a number: .8
Incorrect Input!
Enter a number: 0
1 = product
2 = sum
3 = difference
What is 2 2 6?
Enter a number: 8
1 = product
2 = sum
3 = difference
What is 6 3 4?
Enter a number: 1.5
Incorrect Input!
Enter a number: 2
1 = product
2 = sum
3 = difference
What is 7 1 10?
Enter a number: 70
1 = product
2 = sum
3 = difference
What is 9 2 5?
Enter a number: 14
1 = product
2 = sum
3 = difference
What is 5 1 10?
Enter a number: 50
You got 8 correct out of 10
预期输出:
dm15125@isu:/u1/work/Python/math> python3 mathquiz.py
What is 4 + 6?
Enter a number: 10
What is 7 + 6?
Enter a number: 13
What is 3 + 3?
Enter a number: 6
What is 8 / 4?
Enter a number: 2
What is 8 / 10?
Enter a number: 0.8
Incorrect Input!
Enter a number: .8
Incorrect Input!
Enter a number: 0
What is 2 + 6?
Enter a number: 8
What is 6 / 4?
Enter a number: 1.5
Incorrect Input!
Enter a number: 2
What is 7 * 10?
Enter a number: 70
What is 9 + 5?
Enter a number: 14
What is 5 * 10?
Enter a number: 50
You got 8 correct out of 10
你可以使用字典来做到这一点:
operators = {
1: "+",
2: "-",
3: "/",
4: "*"
}
operators = operator[z] // where z is the random integer for gettig the operator.
在你的打印语句中
print("What is " + str(x)+" " + str(operator)+" " + str(y)+"?")
在这部分代码中:
print(" 1 = product \n 2 = sum \n 3 = difference")
print("What is " + str(x)+" " + str(z)+" " + str(y)+"?")
而不是 str(z)
,定义一个类似 ops = ['*', '+', '-']
的列表并使用 ops[z - 1]
。 - 1
是因为你的 z
从 1 开始,但数组索引从零开始。
所以你的函数会变成:
def askQuestion():
ops = ['*', '+', '-']
x = random.randint(1, 10)
y = random.randint(1, 10)
z = random.randint(1, 3)
print(" 1 = product \n 2 = sum \n 3 = difference")
print("What is " + str(x) + " " + ops[z - 1] + " " + str(y) + "?")
u = askNum()
if z == 1 and u==x*y:
return 1 #product
elif z == 2 and u==x+y:
return 1 #sum
elif z == 3 and u==x/y:
return 1 #difference
else:
return 0
您可以在 askQuestion 函数中使用“if”语句来选择要打印的符号。
或者使用像这样的列表:
symbols_list = ['*','+','-']
symbols_list[z-1]
然后使用“z”值对其进行索引(请记住,第一个位置的索引是 0 而不是 1)。
或者使用以“z”为键的字典来检索适当的符号:
symbols_dict = {1:'*', 2:'+',3:'-'}
symbols_dict[z]
如你所见,有很多选项,选择你更喜欢的一个即可。
我正在尝试使用两个随机数 (1, 10) 随机选择和、差或积来进行数学测验。我使用 z = random.randint(1, 3)
生成和、差或乘积,但我想使用此数字转换为 "x"、“/”或“+”等符号来显示输出以提问,因为我是新手Python 语言,我正在尝试学习如何将数字转换为符号。
我的代码在这里:
import random
def askNum():
while(1):
try:
userInput = int(input("Enter a number: "))
break
except ValueError:
print("Incorrect Input!")
return userInput
def askQuestion():
x = random.randint(1, 10)
y = random.randint(1, 10)
z = random.randint(1, 3)
print(" 1 = product \n 2 = sum \n 3 = difference")
print("What is " + str(x)+" " + str(z)+" " + str(y)+"?")
u = askNum()
if z == 1 and u==x*y:
return 1 #product
elif z == 2 and u==x+y:
return 1 #sum
elif z == 3 and u==x/y:
return 1 #difference
else:
return 0
amount = 10
correct = 0
for i in range(amount):
correct += askQuestion()
print("You got %d correct out of %d" % (correct, amount))
现实输出:
dm15125@isu:/u1/work/Python/math> python3 mathquiz.py
1 = product
2 = sum
3 = difference
What is 4 2 6?
Enter a number: 10
1 = product
2 = sum
3 = difference
What is 7 2 6?
Enter a number: 13
1 = product
2 = sum
3 = difference
What is 3 2 3?
Enter a number: 6
1 = product
2 = sum
3 = difference
What is 8 3 4?
Enter a number: 2
1 = product
2 = sum
3 = difference
What is 8 3 10?
Enter a number: 0.8
Incorrect Input!
Enter a number: .8
Incorrect Input!
Enter a number: 0
1 = product
2 = sum
3 = difference
What is 2 2 6?
Enter a number: 8
1 = product
2 = sum
3 = difference
What is 6 3 4?
Enter a number: 1.5
Incorrect Input!
Enter a number: 2
1 = product
2 = sum
3 = difference
What is 7 1 10?
Enter a number: 70
1 = product
2 = sum
3 = difference
What is 9 2 5?
Enter a number: 14
1 = product
2 = sum
3 = difference
What is 5 1 10?
Enter a number: 50
You got 8 correct out of 10
预期输出:
dm15125@isu:/u1/work/Python/math> python3 mathquiz.py
What is 4 + 6?
Enter a number: 10
What is 7 + 6?
Enter a number: 13
What is 3 + 3?
Enter a number: 6
What is 8 / 4?
Enter a number: 2
What is 8 / 10?
Enter a number: 0.8
Incorrect Input!
Enter a number: .8
Incorrect Input!
Enter a number: 0
What is 2 + 6?
Enter a number: 8
What is 6 / 4?
Enter a number: 1.5
Incorrect Input!
Enter a number: 2
What is 7 * 10?
Enter a number: 70
What is 9 + 5?
Enter a number: 14
What is 5 * 10?
Enter a number: 50
You got 8 correct out of 10
你可以使用字典来做到这一点:
operators = {
1: "+",
2: "-",
3: "/",
4: "*"
}
operators = operator[z] // where z is the random integer for gettig the operator.
在你的打印语句中
print("What is " + str(x)+" " + str(operator)+" " + str(y)+"?")
在这部分代码中:
print(" 1 = product \n 2 = sum \n 3 = difference")
print("What is " + str(x)+" " + str(z)+" " + str(y)+"?")
而不是 str(z)
,定义一个类似 ops = ['*', '+', '-']
的列表并使用 ops[z - 1]
。 - 1
是因为你的 z
从 1 开始,但数组索引从零开始。
所以你的函数会变成:
def askQuestion():
ops = ['*', '+', '-']
x = random.randint(1, 10)
y = random.randint(1, 10)
z = random.randint(1, 3)
print(" 1 = product \n 2 = sum \n 3 = difference")
print("What is " + str(x) + " " + ops[z - 1] + " " + str(y) + "?")
u = askNum()
if z == 1 and u==x*y:
return 1 #product
elif z == 2 and u==x+y:
return 1 #sum
elif z == 3 and u==x/y:
return 1 #difference
else:
return 0
您可以在 askQuestion 函数中使用“if”语句来选择要打印的符号。
或者使用像这样的列表:
symbols_list = ['*','+','-']
symbols_list[z-1]
然后使用“z”值对其进行索引(请记住,第一个位置的索引是 0 而不是 1)。
或者使用以“z”为键的字典来检索适当的符号:
symbols_dict = {1:'*', 2:'+',3:'-'}
symbols_dict[z]
如你所见,有很多选项,选择你更喜欢的一个即可。