This is an archive of the mabination.com forums which were active from 2010 to 2018. You can not register, post or otherwise interact with the site other than browsing the content for historical purposes. The content is provided as-is, from the moment of the last backup taken of the database in 2019. Image and video embeds are disabled on purpose and represented textually since most of those links are dead.
To view other archive projects go to
https://archives.mabination.com
-
Chockeh wrote on 2011-08-31 06:12
I keep getting this error (in red) in Python:
#Kevin Fernandez
#August 30, 2011
#This program will calculate how many credits are needed to graduate.
studentName = input ('Enter student name. ')
degreeName = input ('Enter degree program. ')
creditsDegree = input ('Enter the number of credits required for the degree. ')
creditsTaken = input ('Enter the number of credits taken so far. ')
creditsLeft = creditsDegree - creditsTaken
print ('The student\'s name is '), studentName
print ('The degree program is '), degreeName
print ('The number of credits required for the degree is '), creditsDegree
print ('The number of credits taken so far is '), creditsTaken
print ('The number of credits left is '), creditsLeft
------------------------------------------------------------
This is the Python Shell:
Enter student name. Kevin Fernandez
Enter degree program. The degree of failing this program
Enter the number of credits required for the degree. 9001
Enter the number of credits taken so far. 9000
Traceback (most recent call last):
File "C:\Users\Kevin_3\Desktop\Homework\College\1st Semester\Logic of Programming\Lab 1-4.py", line 9, in <module>
creditsLeft = creditsDegree - creditsTaken
TypeError: unsupported operand type(s) for -: 'str' and 'str'
------------------------------------------------------------
Anyone knows what I should do? I'm using Python 3 btw.
-
Yoorah wrote on 2011-08-31 07:16
I don't know python, but it looks like it's failing because it's interpreting creditsDegree and creditsTaken as strings (letters), and you can't subtract those like integers (numbers). Try typecasting both of those into ints, like int(creditsDegree) and then see if the math will work. :3
-
Chockeh wrote on 2011-08-31 07:20
Enter student name. Kevin
Enter degree program. CST
Enter the number of credits required for the degree. 54
Enter the number of credits taken so far. 12
The student's name is
The degree program is
The number of credits required for the degree is
The number of credits taken so far is
The number of credits left is
I got this o-o... Better than error though I guess xD.
-
Yoorah wrote on 2011-08-31 07:38
oic
http://docs.python.org/release/3.0.1/whatsnew/3.0.html
It looks like you weren't using the print syntax properly. x)
-
Chockeh wrote on 2011-08-31 07:47
studentName = input ('Enter student name. ')
degreeName = input ('Enter degree program. ')
creditsDegree = input ('Enter the number of credits required for the degree. ')
creditsTaken = input ('Enter the number of credits taken so far. ')
creditsLeft = creditsDegree - creditsTaken
print ("The student\'s name is", studentName)
print ("The degree program is", degreeName)
print ("The number of credits required for the degree is", creditsDegree)
print ("The number of credits taken so far is", creditsTaken)
print ("The number of credits left is", creditsLeft)
Tried doing that, I get the same error. I'm a total noob :[.
-
Kingofrunes wrote on 2011-08-31 07:51
I wish I could help but my programming is limited to Actionscript 3.0,Assembly,C,C++,C#,Java,PHP and Javascript.
*cracks knuckles*
Time to quickly pickup Python so I can help you out. Shouldn't be any different from any other programming language I've worked with.
Downloading Python right now. You're lucky that I'm obsessed with programming and will easily pickup a new programming language if the opportunity presents itself and I'm bored.
Wow that was really easy to solve! Here try this:
studentName = input ('Enter student name. ')
degreeName = input ('Enter degree program. ')
creditsDegree = input ('Enter the number of credits required for the degree. ')
creditsTaken = input ('Enter the number of credits taken so far. ')
creditsLeft = int(creditsDegree) - int(creditsTaken) #This will convert the "strings" into int's so that math can occur
print ("The student\'s name is", studentName)
print ("The degree program is", degreeName)
print ("The number of credits required for the degree is", creditsDegree)
print ("The number of credits taken so far is", creditsTaken)
print ("The number of credits left is", creditsLeft)
Rest of the code worked just fine. :tea:
Hope that helps!
-
Chockeh wrote on 2011-08-31 08:07
OMFG King, you did it so easily. It worked, thanks a lot xD.
I knew that was the source of all my problems xD.
-
Kingofrunes wrote on 2011-08-31 08:15
Quote from Chockeh;572575:
OMFG King, you did it so easily. It worked, thanks a lot xD.
I knew that was the source of all my problems xD.
Programming comes naturally to me. I'm pretty good at picking up programming languages with very little difficulty. In the end, all the concepts are universal and it's just a matter of learning the proper syntax.
-
Ithiliel wrote on 2011-08-31 10:15
=D Yay! I'm glad someone was able to help you out Kev :thumb: Thank you King~