当给出某些输入时,我可以忽略某些代码吗



我正在python上构建一个基本的计算器,以了解编程的基本知识以及其他知识。基本上,我的问题是,当我用计算器对一个数字求平方时,它仍然会问我的第二个数字,而它会给出正确的答案。我仍然希望它在使用平方函数时不要问我的第一个数字。

name = input("Enter your name: ")
lastname = input("Enter your last name: ")
print("Welome " + name + " " + lastname + "!")
num1 = float(input("Enter a number to calculate: "))
Component = input("*, /, +, -...?????: ")
num2 = float(input("Enter your last number...: "))
if Component == "*":
print(num1 * num2)
if Component == "+":
print(num1 + num2)
if Component == "/":
print(num1 / num2)
if Component == "-":
print(num1 - num2)
if Component == "sqr":
print(num1 ** 2)

请改用此num2 = float(input("Enter your last number...: ")) if Component.strip() != "sqr" else 0

此处的完整代码:

name = input("Enter your name: ")
lastname = input("Enter your last name: ")
print("Welome " + name + " " + lastname + "!")
num1 = float(input("Enter a number to calculate: "))
Component = input("*, /, +, -...?????: ")
num2 = float(input("Enter your last number...: ")) if Component.strip() != "sqr" else 0
if Component == "*":
print(num1 * num2)
if Component == "+":
print(num1 + num2)
if Component == "/":
print(num1 / num2)
if Component == "-":
print(num1 - num2)
if Component == "sqr":
print(num1 ** 2)

相关内容

  • 没有找到相关文章

最新更新