【问题标题】:What is the best way to deal with negative integers inputs in a python calculator?python处理-0和负号(输入)
【发布时间】:2020-08-09 08:11:42
【问题描述】:

开发者和工程师们您好:

我该如何处理这两种类型的输入1)当用户输入“-0”2)当用户输入减号“-”时,在我的while 循环用于添加非负整数。

程序在整体设计中开发了一些功能,就是添加非负整数,但是当用户输入-0或-(减号)符号时它有一个缺陷。当用户输入这些条目时,我想打印('你输入了一个负整数或负/减号')。

目前,当用户在输入中提交-0时程序继续运行而不添加,当用户仅提交-符号时出现ValueError。

请提供反馈。

entry = 0 
sum = 0
# Request input from the user
print('This is nonnegative integer addition program. \n',)

while entry >= 0:
    entry=int(input('Please enter numbers to add: '))
    if entry >=0:
        sum+=entry
    elif entry<0 or str(entry[0])=='-' or str(entry)=='-0':  # -0 nor (first index -(minus) does not work
        print('You have entered a negative integer or symbol:', \
            entry, ', therefore you have been exited out of the program')
print('total integer input sum =',sum)

【问题讨论】:

  • 你可以通过省略一堆前锋来解决这个问题,关于你读到的,这个问题很有趣。等等等等。并说出为什么它不起作用。 stackoverflow.com/help/how-to-ask
  • 你需要检查输入的字符串是否有"-0",然后才能将其转换为整数并正常处理。
  • 感谢马特,更新后的描述。
  • 为什么需要将'-''-0' 分开?两者都属于entry[0] == '-'...如果您发现自己在尝试解析字符串时遇到了太多条件,您可以考虑使用正则表达式...

标签: python-3.x


【解决方案1】:

当您将输入存储为 int entry=int(input('Please enter numbers to add: ')) 时,条目值为 0,因为在 int 上没有 -0,因此如果要检查用户输入是否为 -0,则需要将其保留为 str。 你可以试试

entry = 0 
sum = 0
print('This is nonnegative integer addition program. \n',)

while int(entry) >= 0:
    entry=input('Please enter numbers to add: ')
    if entry[0] !='-':
        if int(entry) >= 0:
            sum+=int(entry)
    elif entry[0]=='-' or entry=='-0':
        print('You have entered a negative integer or symbol:', \
            entry, ', therefore you have been exited out of the program')
        entry = -1
    print('total integer input sum =',sum)

【讨论】:

  • 您好 Arad 先生,感谢您的反馈,但是,如果您尝试您的代码和我的代码,程序仍然接受 -0 并在 - 减号输入时输出值错误。一旦找到解决方案,我将回答这篇文章,但我将继续学习循环。虽然这是一个很酷的坑洼,但要注意并且能够在遇到时处理/修复它真是太棒了。
  • 你现在可以试试
  • ValueError: int() 基数为 10 的无效文字:'-'
  • 你有什么意见?
  • 输入是 - 减号。我在 .py 文件中编写了程序,并在终端上运行它以进行测试/调试。
【解决方案2】:

如另一个答案中所述。您将值转换为 int 并且您不能再访问这些字符。我已经让程序存储了 user_input 并检查了它。

entry = 0 
sum = 0
# Request input from the user
print('This is nonnegative integer addition program. \n',)

while entry >= 0:
    user_input = input('Please enter numbers to add: ')

    if '-' in user_input:
        print( 
           'You have entered a negative integer or symbol:', 
           user_input, 
           ', therefore you have been exited out of the program'
        )
        break

    entry = int(user_input)
    sum+=entry

print('total integer input sum =',sum)

在这个版本中,我将输入字符串存储为user_input,并检查它是否有“-”符号。我使用'-' in 而不是user_input[0]=='-',因为以空格开头的输入仍然可以使用int

另外需要注意的是,你的 if/elif 被过度指定了。

if entry >=0:
    sum+=entry
elif entry<0 or str(entry[0])=='-' or str(entry)=='-0':
    break

在您的第二个子句中,条目保证小于零。所以str(entry[0]) 永远不会被调用。谢天谢地,因为这是一个错误。你的 if 语句相当于。

if entry>=0:
    ...
else:
    ...

最后一点。当您在打印语句中有一组括号时。您不需要 \ 来指示续行。

print(
    "one",
    "two"
)

Python 识别打开的(。如果你想继续一个字符串,你需要它。

print("one \
      two")

【讨论】:

  • 你好,马特,非常感谢你的反馈和参与这篇文章。我在 ri: 中的 if '-' 之后添加了第三个 elif:但打印了一条消息,但在处理错误/输入时无济于事。我正在考虑进一步嵌套条件语句并创建第三个变量来保存 -0 或减号来以这种方式管理它。
  • @ASEDev 我已经更新了答案,我使用了你的原始代码,并添加了一些关于你在程序中所做的事情的注释。
  • 谢谢,马特。我肯定对我的程序及其行为有了更好的了解。
猜你喜欢
  • 2017-09-08
  • 2015-12-27
  • 2017-09-08
  • 1970-01-01
  • 2017-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
相关资源
最近更新 更多