【问题标题】:'none' prints for no apparent reason'none' 无明显原因打印
【发布时间】:2019-10-20 00:54:25
【问题描述】:
print("Entre nummber 1: ")
num1 = float(input('> '))
print("Entre opperation: ")
op = input('> ')
print("Entre nummber 2: ")
num2 = float(input('> '))
result = print("Your Result is:")

if op == "+":
    print(num1 + num2)
    print(result)
    print("Done")

elif op == '-':
    print(num1 - num2)
    print(result)
    print("Done")

elif op == '/':
    print(num1 / num2)
    print(result)
    print("Done")

elif op == '*':
    print(num1 * num2)
    print(result)
    print("Done")
elif op == '**':
    print(num1 ** num2)
    print(result)
    print("Done")
else:
    print("Entre a valid opperation")

我试着做一个计算器。它工作正常,但最后会无缘无故弹出“无”。

我不知道为什么。任何帮助表示赞赏。

这就是问题所在:

【问题讨论】:

  • result = print(...)print 的返回值分配给结果。由于print 没有任何相关的返回值,所以使用默认值None。只是 print(...) 没有 result = 部分。

标签: python python-3.7


【解决方案1】:
result = print("Your Result is:")

print("Your Result is:") 打印此字符串"Your Result is:" 并返回None,现在result 等于None。然后print(result) 打印None

【讨论】:

    【解决方案2】:
    result = print("Your Result is:")
    

    print什么都不返回None

    结果值为None

    print(result) #this is none
    

    你应该把它存储在一个变量中

    result = num1 + num2
    print(result) #with calculated value
    

    【讨论】:

      【解决方案3】:
      # remove result = print("Your result is :")
      # Add this result = num1 'operation +/-/*/** etc' num2 after your if condittions.
      # for example :
      if op == "+":
          result = num1 + num2
          print("Your result is :",result)
      if op == "-" :
          result = num1 - num2
          print("Your result is :",result)
      # It will work fine. 
      

      【讨论】:

        猜你喜欢
        • 2016-08-10
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        • 2014-04-09
        • 1970-01-01
        • 2015-09-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多