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

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

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

问题

以下是翻译好的代码部分:

  1. def add(num1, num2):
  2. ans = num1 + num2
  3. print(ans)
  4. def subtract(num1, num2):
  5. ans = num1 - num2
  6. print(ans)
  7. def multiply(num1, num2):
  8. ans = num1 * num2
  9. print(ans)
  10. def divide (num1, num2):
  11. if num2 == 0:
  12. print("嘿,你不能这样做")
  13. else:
  14. ans = num1 / num2
  15. print(ans)
  16. while True:
  17. option = input("你想做什么?加法(1),减法(2),乘法(3),除法(4)。选择相应的数字作为你的选项。")
  18. if option == "1":
  19. num1 = int(input("第一个数字是多少?"))
  20. num2 = int(input("第二个数字是多少?"))
  21. add(num1, num2)
  22. elif option == "2":
  23. num1 = int(input("第一个数字是多少?"))
  24. num2 = int(input("第二个数字是多少?"))
  25. subtract(num1, num2)
  26. elif option == "3":
  27. num1 = int(input("第一个数字是多少?"))
  28. num2 = int(input("第二个数字是多少?"))
  29. multiply(num1, num2)
  30. elif option == "4":
  31. num1 = int(input("第一个数字是多少?"))
  32. num2 = int(input("第二个数字是多少?"))
  33. divide(num1, num2)
  34. print("错误,选项无效。")
  35. print()

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

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

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

英文:
  1. def add(num1, num2):
  2. ans = num1 + num2
  3. print(ans)
  4. def subtract(num1, num2):
  5. ans = num1 - num2
  6. print(ans)
  7. def multiply(num1, num2):
  8. ans = num1 * num2
  9. print(ans)
  10. def divide (num1, num2):
  11. if num2 == 0:
  12. print("Hey, you can't do that man")
  13. else:
  14. ans = num1 / num2
  15. print(ans)
  16. while True:
  17. option = input("What would you like to do? Add (1), Subtract (2), Multiply (3), Divide (4). Choose the corresponding number for your option. ")
  18. if option == "1":
  19. num1 = int(input("What is the first number? "))
  20. num2 = int(input("What is the second number? "))
  21. add(num1, num2)
  22. elif option == "2":
  23. num1 = int(input("What is the first number? "))
  24. num2 = int(input("What is the second number? "))
  25. subtract(num1, num2)
  26. elif option == "3":
  27. num1 = input("What is the first number? ")
  28. num2 = input("What is the second number? ")
  29. multiply(num1, num2)
  30. elif option == "4":
  31. num1 = int(input("What is the first number? "))
  32. num2 = int(input("What is the second number? "))
  33. divide(num1, num2)
  34. print("Error, option not valid.")
  35. 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"。

这是修正方法:

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

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

  1. def add(num1, num2):
  2. ans = num1 + num2
  3. print(ans)
  4. def subtract(num1, num2):
  5. ans = num1 - num2
  6. print(ans)
  7. def multiply(num1, num2):
  8. ans = num1 * num2
  9. print(ans)
  10. def divide (num1, num2):
  11. if num2 == 0:
  12. print("嘿,你不能这样做")
  13. else:
  14. ans = num1 / num2
  15. print(ans)
  16. while True:
  17. option = input("你想做什么?加法(1),减法(2),乘法(3),除法(4)。选择相应的数字作为你的选项。")
  18. if option == "1":
  19. num1 = int(input("第一个数字是多少?"))
  20. num2 = int(input("第二个数字是多少?"))
  21. add(num1, num2)
  22. elif option == "2":
  23. num1 = int(input("第一个数字是多少?"))
  24. num2 = int(input("第二个数字是多少?"))
  25. subtract(num1, num2)
  26. elif option == "3":
  27. num1 = int(input("第一个数字是多少?"))
  28. num2 = int(input("第二个数字是多少?"))
  29. multiply(num1, num2)
  30. elif option == "4":
  31. num1 = int(input("第一个数字是多少?"))
  32. num2 = int(input("第二个数字是多少?"))
  33. divide(num1, num2)
  34. else:
  35. 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:

  1. elif option == "3":
  2. num1 = int(input("What is the first number? "))
  3. num2 = int(input("What is the second number? "))
  4. 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:

  1. def add(num1, num2):
  2. ans = num1 + num2
  3. print(ans)
  4. def subtract(num1, num2):
  5. ans = num1 - num2
  6. print(ans)
  7. def multiply(num1, num2):
  8. ans = num1 * num2
  9. print(ans)
  10. def divide (num1, num2):
  11. if num2 == 0:
  12. print("Hey, you can't do that man")
  13. else:
  14. ans = num1 / num2
  15. print(ans)
  16. while True:
  17. option = input("What would you like to do? Add (1), Subtract (2), Multiply (3), Divide (4). Choose the corresponding number for your option. ")
  18. if option == "1":
  19. num1 = int(input("What is the first number? "))
  20. num2 = int(input("What is the second number? "))
  21. add(num1, num2)
  22. elif option == "2":
  23. num1 = int(input("What is the first number? "))
  24. num2 = int(input("What is the second number? "))
  25. subtract(num1, num2)
  26. elif option == "3":
  27. num1 = int(input("What is the first number? "))
  28. num2 = int(input("What is the second number? "))
  29. multiply(num1, num2)
  30. elif option == "4":
  31. num1 = int(input("What is the first number? "))
  32. num2 = int(input("What is the second number? "))
  33. divide(num1, num2)
  34. else:
  35. print("Error, option not valid.\n")

答案2

得分: 0

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

  1. elif option == "4":
  2. num1 = int(input("第一个数字是多少?"))
  3. num2 = int(input("第二个数字是多少?"))
  4. divide(num1, num2)
  5. else:
  6. print("错误,选项无效。")
  7. 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.

  1. elif option == "4":
  2. num1 = int(input("What is the first number? "))
  3. num2 = int(input("What is the second number? "))
  4. divide(num1, num2)
  5. else:
  6. print("Error, option not valid.")
  7. 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:

确定