这是我的代码,我发现它有点笨重和重复。 """ 钥匙 UCIO=用户选择的输入操作 x=全局变量,用户将使用的第一个数字 y=全局变量,用户将使用的第二个数字 z=局部变量,所选操作的结果数 “”“
# Declaring the types of operates the user cold use
print("Select an operation")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Power")
# Making the code look neater
print("")
# Gathering information from the user to calculate equation
UCIO = input("Enter an operation 1/2/3/4/5: ")
x = input("Enter your first number: ")
y = input("Enter your second number: ")
# Making the code look neater
print("")
# Calculating an equation with the operation "+"
if UCIO == "1":
z = float(x) + float(y)
print(x + " " + "+" + " " + y + " " + "=" + " " + str(z))
# Calculating an equation with the operation "-"
elif UCIO == "2":
z = float(x) - float(y)
print(x + " " + "-" + " " + y + " " + "=" + " " + str(z))
# Calculating an equation with the operation "*"
elif UCIO == "3":
z = float(x) * float(y)
print(x + " " + "*" + " " + y + " " + "=" + " " + str(z))
# Calculating an equation with the operation "/"
elif UCIO == "4":
z = float(x) / float(y)
print(x + " " + "/" + " " + y + " " + "=" + " " + str(z))
# Calculating an equation with the operation "^"
elif UCIO == "5":
z = float(x) ** float(y)
print(x + " " + "^" + " " + y + " " + "=" + " " + str(z))
正如在注释中所说的,您可以使用
UCIO
的值并将其链接到要使用的目标函数UCIO
的字典。每个UCIO
都有一个alias
、一个message
和一个操作函数fct
message
键来打印所有可能的选项UCIO
,x
和y
UCIO
在可能的选项中,请使用字典中相应的操作函数fct
相关问题 更多 >
编程相关推荐