发出串联“ str”和“ int”对象的问题

时间:2019-04-18 15:20:45

标签: python python-2.7

我正在创建一个程序,该程序将决定您是否应该升级计算机

#User inputs
CPU = int(raw_input("User, input the production year of your CPU: "))
GPU = int(raw_input("User, input the production year of your GPU, if  you have a dedicated GPU: "))
buyyear = int(raw_input("User, input the year that you bought or built your desktop/laptop: "))
date = int(raw_input("User, input current year: "))

#Calculations
CPUcalc = str(date - CPU)
GPUcalc = str(date - GPU)
avgcalc = str((GPUcalc + CPUcalc + buyyear)/3)

#Outputs
if date > avg_calc:
print ("Your computer is " + avgcalc + " years old. You might not need to upgrade your laptop/desktop, but if you think your laptop/desktop is slow, you may need to upgrade.")

elif date < avg_calc:
print ("Your computer is " + avgcalc + " years old. You may need to upgrade your laptop/desktop, but if you think your laptop/desktop is not slow, you my not need to upgrade.")

else:
print "ERROR: The program didn't meet the requirements for the program to calculate if you need to upgrade or not."
print "The equation is: ((current year - CPU year) + (current year - GPU year) + bought year) divide by 3) = x"
print "If 'x' is less than 4, you may not need to upgrade"
print "If 'x' is more than 4, you may need to upgrade"

期望:一个很长的描述,说您是否应该升级 还是不可以。

结果:

Traceback (most recent call last):
File "F:\Free-Lance Coding\Computer Upgrading.py", line 10, in <module>
    avgcalc = str((GPUcalc + CPUcalc + buyyear)/3)
TypeError: cannot concatenate 'str' and 'int' objects

3 个答案:

答案 0 :(得分:4)

假设您以后想要打印CPUcalcGPUcalcavgcalc,则将值转换为字符串还为时过早。您正在尝试使用GPUcalcCPUcalc进行计算,但是这些值实际上是不需要的时候是字符串。

仅将必须为字符串的值转换为字符串:

CPUcalc = date - CPU
GPUcalc = date - GPU
avgcalc = (GPUcalc + CPUcalc + buyyear) / 3

您还有一个错字,您同时引用了avgcalcavg_calc。大概只有avgcalc存在:

if date > avgcalc:
    print ("Your computer is " + str(avgcalc) + " years old. You might not need to upgrade your laptop/desktop, but if you think your laptop/desktop is slow, you may need to upgrade.")

elif date < avgcalc:
    print ("Your computer is " + str(avgcalc) + " years old. You may need to upgrade your laptop/desktop, but if you think your laptop/desktop is not slow, you my not need to upgrade.")

else:
    print "ERROR: The program didn't meet the requirements for the program to calculate if you need to upgrade or not."
    print "The equation is: ((current year - CPU year) + (current year - GPU year) + bought year) divide by 3) = x"
    print "If 'x' is less than 4, you may not need to upgrade"
    print "If 'x' is more than 4, you may need to upgrade"

请注意,您不需要使用str()将值转换为字符串,仅在将非字符串对象与字符串连接时才需要。如果将值分别传递给语句,则可以print为您将值转换为字符串(此时,print在每个元素之间插入空格):

if date > avgcalc:
    print "Your computer is", avgcalc, "years old. You might not need to upgrade your laptop/desktop, but if you think your laptop/desktop is slow, you may need to upgrade."

elif date < avgcalc:
    print "Your computer is", avgcalc, "years old. You may need to upgrade your laptop/desktop, but if you think your laptop/desktop is not slow, you my not need to upgrade."

else:
    # ...

您还可以在str.format() method中使用字符串格式:

if date > avgcalc:
    print "Your computer is {} years old. You might not need to upgrade your laptop/desktop, but if you think your laptop/desktop is slow, you may need to upgrade.".format(avgcalc)

elif date < avgcalc:
    print "Your computer is {} years old. You may need to upgrade your laptop/desktop, but if you think your laptop/desktop is not slow, you my not need to upgrade.".format(avgcalc)

else:
    # ...

{}占位符会将值转换为合适的字符串,或者您可以进一步配置应如何对待该值。

请注意,您的实际测试(date > avgcalcdate < avgcalc)和您在else分支中打印的文本彼此不一致,即您说要根据数字4

您的计算也没有多大意义,为什么还要将购买计算机的年份与购买CPU和GPU以来的使用年数相加呢?您可能想在(date - buyyear)值中使用avgcalc。我还将在每个变量中使用术语age,以使值 mean

的含义更清楚。
CPU_age = date - CPU
GPU_age = date - GPU
computer_age = date - buyyear
avg_age = (CPU_age + GPU_age + computer_age) / 3

if avg_age > 4:
    # computer is old
else:
    # computer is still new enough

您的else:分支意义不大,因为只有dateavgcalc完全相等时才会发生。您可能想了解validating user input并在此处使用错误消息。

您也可以考虑使用Python获取当前年份,计算机通常已经知道日期。 Python具有datetime模块,其中datetime.date.today()为您获取今天的日期,并且生成的对象具有year属性:

import datetime

# ...

date = datetime.date.today().year

最后但并非最不重要的一点:如果您正在学习Python,则强烈建议您切换到Python3。Python2(现在使用的语言)将在1月终止使用2020年,little over 8 months from today

答案 1 :(得分:0)

您遇到以下问题:
 -python 2中的打印语句print "Test"与python 3中的print("test")之间的区别
 -if else语句后的缩进问题
 -变量名不匹配avgcalcavg_calc
 -将avgcalc与字符串连接以打印

这是有效的代码,但实际上您需要了解python以及raw_input()input()方法,printprint()在python版本方面的区别。 / p>

如果您使用的是Python 3:

#User inputs
CPU = int(input("User, input the production year of your CPU: "))
GPU = int(input("User, input the production year of your GPU, if  you have a dedicated GPU: "))
buyyear = int(input("User, input the year that you bought or built your desktop/laptop: "))
date = int(input("User, input current year: "))

#Calculations
CPUcalc = date - CPU
GPUcalc = date - GPU
avgcalc = (GPUcalc + CPUcalc + buyyear)/3

#Outputs
if date > avgcalc:
    print("Your computer is " + str(avgcalc) + " years old. You might not need to upgrade your laptop/desktop, but if you think your laptop/desktop is slow, you may need to upgrade.")

elif date < avgcalc:
    print("Your computer is " + str(avgcalc) + " years old. You may need to upgrade your laptop/desktop, but if you think your laptop/desktop is not slow, you my not need to upgrade.")

else:
    print("ERROR: The program didn't meet the requirements for the program to calculate if you need to upgrade or not.")
    print("The equation is: ((current year - CPU year) + (current year - GPU year) + bought year) divide by 3) = x")
    print("If 'x' is less than 4, you may not need to upgrade")
    print("If 'x' is more than 4, you may need to upgrade")

如果您使用的是python 2:

在上面的代码中,将input()替换为raw_input,并用print "your string"替换print(“ Your string”)。

答案 2 :(得分:-1)

尝试一下: avgcalc =(GPUcalc + CPUcalc +购买年份)/ 3

avgcalc = int(avgcalc)