Reputation: 197
I am creating a program that will decide if you should upgrade your computer
#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"
Expecting: A very long description saying if you should upgrade your computer or not.
Result:
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
Upvotes: 0
Views: 198
Reputation: 1122122
You are converting your values to strings too soon, assuming you want to be able to print CPUcalc
, GPUcalc
and avgcalc
later on. You are trying to make a calculation with GPUcalc
and CPUcalc
but those values are strings, when they really don't need to be.
Only convert values to strings when they have to be strings:
CPUcalc = date - CPU
GPUcalc = date - GPU
avgcalc = (GPUcalc + CPUcalc + buyyear) / 3
You also have a typo, you reference both avgcalc
and avg_calc
. Presumably only avgcalc
exists:
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"
Note that you don't need to use str()
to convert values to strings, that's only needed when concatenating non-string objects with strings. You can have print
convert values to strings for you if you pass them to the statement separately (at which point print
inserts spaces between each element):
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:
# ...
You can also use string formatting, with the 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:
# ...
The {}
placeholder will convert values to a suitable string, or you can further configure how the value should be treated.
Note that your actual tests (date > avgcalc
, date < avgcalc
) and the text you print in the else
branch don't agree with one another, where you say you test the calculation against the number 4
.
Your calculation also doesn't make all that much sense, why would you add the year the computer was bought to the number of years that have passed since the CPU and GPU have been bought? You probably want to use (date - buyyear)
in the avgcalc
value. I'd also use the term age
in each variable, to make it clearer what the values 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
Your else:
branch doesn't make all that much sense, as that'd only happen if date
and avgcalc
were to be exactly equal. You probably want to learn about validating user input and use the error message there.
You can also consider using Python to get the current year, computers usually know the date already. Python has the datetime
module, where datetime.date.today()
gets you today's date, and the resulting object has a year
attribute:
import datetime
# ...
date = datetime.date.today().year
Last but not least: if you are learning Python, it is strongly recommended that you switch to Python 3. Python 2, the language you are using now, will reach end-of-life January 2020, a little over 8 months from today.
Upvotes: 4
Reputation: 4684
You have following issues:
- Difference between print statement print "Test"
in python 2 and print("test")
in python 3
- indentation problem after if else statements
- Variable name mismatch avgcalc
and avg_calc
- Concatenating avgcalc
with string to print
etc
Here is the working code but really you need to learn about python and difference between raw_input()
and input()
method , print
and print()
in terms of python versions.
if you use 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")
If you are using python 2:
replace input()
with raw_input
and replace print("Your string") with print "your string"
in above code.
Upvotes: 0
Reputation: 59
try this : avgcalc = (GPUcalc + CPUcalc + buyyear)/3
avgcalc = int(avgcalc)
Upvotes: -1