代码拉取完成,页面将自动刷新
"""
import math
def calculate(a, b, o):
if o == "+":
return a + b
elif o == "-":
return a - b
elif o == "*":
return a * b
elif o == "/":
if b == 0:
return "Division by zero is not allowed"
return a / b
elif o == "~":
return ~a
elif o == "&":
return a & b
elif o == "|":
return a | b
elif o == "^":
return a ^ b
elif o == "%":
return a % b
elif o == "log":
if a <= 0:
return "Input must be > 0 for log"
if b <= 0 or b == 1:
return "Invalid base. Must be > 0 and != 1"
return math.log(a, b)
else:
return "Invalid operator"
print("Welcome to the calculator!")
print("This calculator supports normal, binary mode and complex mode.")
print("Pay attention: the number you enter must be integer in normal mode!")
while True:
print("Choose the mode of calculator (or enter 'q' to quit):")
print("1. normal")
print("2. binary")
print("3. complex")
mode_input = input()
if mode_input == "q":
print("Exiting the calculator. Goodbye!")
break
mode = int(mode_input)
operator = input("Enter operator:")
if mode == 2 and operator == "~":
a = int(input("Enter the number:"))
print("The result is: ", calculate(a, 0, operator))
else:
if operator == "log" and mode == 1:
a = float(input("Enter the number: "))
b = float(input("Enter the base: "))
print("The result is: ", calculate(a, b, operator))
continue
if mode == 3:
a = complex(input("Enter the first complex number: "))
b = complex(input("Enter the second complex number: "))
if operator in ["+", "-", "*", "/"]:
print("The result is: ", calculate(a, b, operator))
else:
print("Invalid operator for complex mode")
else:
a = int(input("Enter the first number: ")) if mode == 2 else float(input("Enter the first number: "))
b = int(input("Enter the second number: ")) if mode == 2 else float(input("Enter the second number: "))
if mode == 1:
if operator in ["+", "-", "*", "/", "%","^"]:
print("The result is: ", calculate(a, b, operator))
else:
print("Invalid operator")
elif mode == 2:
if operator in ["&", "|", "^"]:
print("The result is: ", calculate(a, b, operator))
else:
print("Invalid operator")
"""
import random
n = int(input("The number of the problem: "))
point = 0
for _ in range(n):
a = random.randint(0, 9)
b = random.randint(0, 9)
operate = random.randint(0, 3)
if operate == 0:
user_answer = int(input("{a}+{b}=? "))
result = a + b
elif operate == 1:
if a < b:
a, b = b, a
user_answer = int(input("{a}-{b}=? "))
result = a - b
elif operate == 2:
user_answer = int(input("{a}*{b}=? "))
result = a * b
else:
while b == 0:
b = random.randint(1, 9)
user_answer = float(input("{a}/{b}=? "))
result = a /b
if user_answer == result:
print("Correct!")
point += 10
else:
print("Wrong!")
print("Your total score is {point}")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。