【问题标题】:Error message "int() argument" and "NoneType"错误消息“int() 参数”和“NoneType”
【发布时间】:2021-08-26 00:36:15
【问题描述】:

我想知道您是否可以再次帮助我。 仍然在入门级挣扎..

这一次,我冒险看看我是否可以编写一个简单的贷款模拟器,就像你看到的那样,找出你可以获得的最大抵押贷款金额。

我写了这个(不知道它是否运行到最后,并且可能写得不好),但它告诉我我在问题 5 中写错了一些东西,并告诉我一些关于“NoneType”的信息。 我有点理解“NoneType”是什么,但我不明白哪里错了。

我也不明白为什么它没有在之前的第三季度和第四季度向我抛出这个问题。

如果有人可以提供帮助,非常感谢!

Z


#lc is total lending capacity v is income w is years left x is asset y is dependents z is healthrisk

def lc(v, w, x=0, y=1, z=1):
    """returns the maximum amount the bank can extend to you as a loan."""
    return (v*w+x)*y*z

q1 = input("Enter your monthly income in 1 man-yen units")
v = 12*0.8*int(q1)

q2 = input("Enter the years you have left at your workplace")
w = int(q2)

q3 = input("Do you have any assets? If you do, write that amount in man-yen units. If none write 'n'")
try:
    x = int(q3)
except(ValueError):
    print("Okay, it is understood that you have no assets.")

q4 = input("If you dependants, write that number as an interger. If none write 'n'")
try:
    q4 = int(q4)
    a4 = 1-0.1*q4
except(ValueError):
    print("Okay, it is understood that you have no dependants.")

q5 = print("Write the number of times you went to hospital last year. If you havent been at all write 'n'")
try:
    q5 = int(q5)
    a5 = 1-0.1*q5
except(ValueError):
    print("Okay, it is understood that you did not go to the hospital at all last year.")

lc
print(lc)

-- 但我明白了……

Traceback (most recent call last):
 q5 = int(q5)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

---

【问题讨论】:

  • 你写的是q5 = print而不是q5 = input()
  • 所以你得到 Nonetype 的原因让我解释一下 print() 是一个函数,但 print() 函数不返回任何输出只是认为它做了一些操作,当你分配一个变量@ 987654327@ python 为您提供函数的变量返回值,因此print() 函数输出为无,因此变量q5 输出为无None
  • 所以如果print()函数返回值1,变量q5 would be 1的值但是print()不返回任何值print()默认是None。跨度>
  • 所以你的代码的主要问题是你写了q5 = print()而不是q5 = input()
  • 天啊!我真傻!对不起,我刚下班。非常感谢!也感谢您对 print() 函数的友好提示!你太客气了!

标签: python integer nonetype


【解决方案1】:

你所做的只是犯了一个愚蠢的错误。在定义q5 时,您必须使用input 而不是print。这是解决方法

q5 = input("Write the number of times you went to hospital last year. If you havent been at all write 'n'")
if q5.is_digit():
  q5 = int(Q5)
  a5 = 1 - (0.1 * Q5)
else:
  print("Okay, it is understood that you did not go to the hospital at all last year")

我对您的代码进行了一些更改,以便更快!欢迎您

【讨论】:

  • 阿尔法狼玩家,很抱歉这么晚才回来。刚下班。非常感谢您不遗余力地指出这一点!
【解决方案2】:

这是你的代码:

def lc(v, w, x=0, y=1, z=1):
    """returns the maximum amount the bank can extend to you as a loan."""
    return (v*w+x)*y*z

q1 = input("Enter your monthly income in 1 man-yen units")
v = 12*0.8*int(q1)

q2 = input("Enter the years you have left at your workplace")
w = int(q2)

q3 = input("Do you have any assets? If you do, write that amount in man-yen units. If none write 'n'")
try:
    x = int(q3)
except(ValueError):
    print("Okay, it is understood that you have no assets.")

q4 = input("If you dependants, write that number as an interger. If none write 'n'")
try:
    q4 = int(q4)
    a4 = 1-0.1*q4
except(ValueError):
    print("Okay, it is understood that you have no dependants.")

q5 = input("Write the number of times you went to hospital last year. If you havent been at all write 'n'")
try:
    q5 = int(q5)
    a5 = 1-0.1*q5
except(ValueError):
    print("Okay, it is understood that you did not go to the hospital at all last year.")

lc
print(lc)

I did not get what you want to do in the last two lines do you want to call the function lc?

【讨论】:

  • 没问题,这是我的工作
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
相关资源
最近更新 更多