1 Star 0 Fork 0

巧克力ovo/PythonLearn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
experiment2.py 3.37 KB
一键复制 编辑 原始数据 按行查看 历史
wlt 提交于 3个月前 . update
"""
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}")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chocolate-ovo/python-learn.git
git@gitee.com:chocolate-ovo/python-learn.git
chocolate-ovo
python-learn
PythonLearn
master

搜索帮助