我的输入尽管我想要将其转换为整数,但并未变为整数。

huangapple go评论100阅读模式
英文:

My input is not turning into an integer even though I want it to

问题

def add(num1, num2):
    ans = num1 + num2
    print(ans)

def subtract(num1, num2):
    ans = num1 - num2 
    print(ans)

def multiply(num1, num2):
    ans = num1 * num2
    print(ans)

def divide (num1, num2):
    if num2 == 0:
        print("嘿,你不能这样做")
    else:
        ans = num1 / num2
        print(ans)

while True:
    option = input("你想做什么?相加(1),相减(2),相乘(3),相除(4)。选择相应的数字进行操作。")
    if option == "1":
        num1 = int(input("第一个数字是多少?"))
        num2 = int(input("第二个数字是多少?"))
        add(num1, num2)
      
    elif option == "2": 
        num1 = int(input("第一个数字是多少?"))
        num2 = int(input("第二个数字是多少?"))
        subtract(num1, num2)
        
    elif option == "3":
        num1 = int(input("第一个数字是多少?"))
        num2 = int(input("第二个数字是多少?"))
        multiply(num1, num2)

    elif option == "4":
        num1 = int(input("第一个数字是多少?"))
        num2 = int(input("第二个数字是多少?"))
        divide(num1, num2)

    print("错误,选项无效。")
    print()

The code has been translated to Chinese as requested. If you encounter issues with running it in VS Code, please provide more details about the specific problem or error message so that I can assist you further.

英文:
def add(num1, num2):
    ans = num1 + num2
    print(ans)

def subtract(num1, num2):
    ans = num1 - num2 
    print(ans)

def multiply(num1, num2):
    ans = num1 * num2
    print(ans)

def divide (num1, num2):
    if num2 == 0:
        print("Hey, you can't do that man")
    else:
        ans = num1 / num2
    print(ans)




while True:
    option = input("What would you like to do? Add (1), Subtract (2), Multiply (3), Divide (4). Choose the corresponding number for your option. ")
    if option == "1":
        num1 = int(input("What is the first number? "))
        num2 = int(input("What is the second number? "))
        add(num1, num2)
      
    elif option == "2": 
        num1 = int(input("What is the first number? "))
        num2 = int(input("What is the second number? "))
        subtract(num1, num2)
        
    elif option == "3":
        num1 = input("What is the first number? ")
        num2 = input("What is the second number? ")
        multiply(num1, num2)

    elif option == "4":
        num1 = int(input("What is the first number? "))
        num2 = int(input("What is the second number? "))
        divide(num1, num2)

    print("Error, option not valid.")
    print()

I ran this in Replit and it worked fine but for VS Code it is not working. What is the issue here?

答案1

得分: 1

在你的第三个选项中,即乘法选项,你忘记在输入之前写上 "int"。

以下是修正后的代码:

elif option == "3":
    num1 = int(input("第一个数字是多少?"))
    num2 = int(input("第二个数字是多少?"))
    multiply(num1, num2)

我还建议只在用户输入的数字不在列表中时显示 "选项无效" 文本。例如:

def add(num1, num2): 
    ans = num1 + num2 
    print(ans)

def subtract(num1, num2): 
    ans = num1 - num2 
    print(ans)

def multiply(num1, num2): 
    ans = num1 * num2 
    print(ans)

def divide(num1, num2): 
    if num2 == 0: 
        print("嘿,你不能这样做") 
    else: 
        ans = num1 / num2 
        print(ans)

while True: 

    option = input("您想要做什么?添加(1),减去(2),乘以(3),除以(4)。选择相应的数字以进行操作。") 
    if option == "1": 
        num1 = int(input("第一个数字是多少?")) 
        num2 = int(input("第二个数字是多少?")) 
        add(num1, num2)

    elif option == "2": 
        num1 = int(input("第一个数字是多少?"))
        num2 = int(input("第二个数字是多少?"))
        subtract(num1, num2)

    elif option == "3":
        num1 = int(input("第一个数字是多少?"))
        num2 = int(input("第二个数字是多少?"))
        multiply(num1, num2)

    elif option == "4":
        num1 = int(input("第一个数字是多少?"))
        num2 = int(input("第二个数字是多少?"))
        divide(num1, num2)

    else:
        print("错误,选项无效。\n")
英文:

It seems like in your third option, the one for multiplication, you forgot to write "int" before your input.

Here's the fix:

elif option == "3":
    num1 = int(input("What is the first number? "))
    num2 = int(input("What is the second number? "))
    multiply(num1, num2)

I would also recommend having the "option not valid" text displayed only when the user inputs a number not listed. For example:

def add(num1, num2): 
    ans = num1 + num2 
    print(ans)

def subtract(num1, num2): 
    ans = num1 - num2 
    print(ans)

def multiply(num1, num2): 
    ans = num1 * num2 
    print(ans)

def divide (num1, num2): 
    if num2 == 0: 
        print("Hey, you can't do that man") 
    else: 
        ans = num1 / num2 
        print(ans)

while True: 

    option = input("What would you like to do? Add (1), Subtract (2), Multiply (3), Divide (4). Choose the corresponding number for your option. ") 
    if option == "1": 
        num1 = int(input("What is the first number? ")) 
        num2 = int(input("What is the second number? ")) 
        add(num1, num2)

    elif option == "2": 
        num1 = int(input("What is the first number? "))
        num2 = int(input("What is the second number? "))
        subtract(num1, num2)

    elif option == "3":
        num1 = int(input("What is the first number? "))
        num2 = int(input("What is the second number? "))
        multiply(num1, num2)

    elif option == "4":
        num1 = int(input("What is the first number? "))
        num2 = int(input("What is the second number? "))
        divide(num1, num2)

    else:
        print("Error, option not valid.\n")

答案2

得分: 0

此外,除了在选项3周围加上int()包装器之外,我建议将错误消息移至循环内部,以避免重复打印。否则,在VS Code中运行正常。

elif option == "4":
num1 = int(input("第一个数字是什么? "))
num2 = int(input("第二个数字是什么? "))
divide(num1, num2)

else:
print("错误,选项无效。")
print()

英文:

In addition to putting int() wrappers around option 3 I would suggest to move the error message inside the loop to avoid repeated printing.Otherwise, it works fine in VS Code.

 elif option == "4":
     num1 = int(input("What is the first number? "))
     num2 = int(input("What is the second number? "))
     divide(num1, num2)

 else:
     print("Error, option not valid.")
 print()

huangapple
  • 本文由 发表于 2023年8月9日 12:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864607-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定