我的输入没有转换为整数,尽管我想要这样做。

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

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()

关于在VS Code中无法正常工作的问题,我无法提供具体的解决方案,因为我无法直接查看你的代码和环境设置。但是,你可以尝试以下几个步骤来解决问题:

  1. 确保你的VS Code已正确配置Python环境,并且已安装所需的依赖项。
  2. 检查代码中是否存在任何语法错误或拼写错误。
  3. 确保你的VS Code中的Python解释器与Replit中使用的解释器版本相同。
  4. 检查VS Code的输出窗口或终端窗口,查看是否有任何错误消息或警告。
  5. 尝试在VS Code中创建一个新的Python项目,并将代码复制到新项目中,然后再次运行。

如果问题仍然存在,你可能需要进一步调查和调试,或者寻求其他开发者的帮助。

英文:
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.html
匿名

发表评论

匿名网友

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

确定