Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

Question: Assume that two variables, varA and varB, are assigned values, either numbers or strings. Write a piece of Python code that prints out one of the following messages: "string involved" if either varA or varB are strings "bigger" if varA is larger than varB "equal" if varA is equal to varB "smaller" if varA is smaller than varB Answer I tried: if (type(varA) or type(varB))=='str': print ("string involved") else: if varA>varB: print("bigger") if varA=varB: print("equal") if varA

MIT 6.00 Intro Computer Science (OCW)
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Join Brainly to access

this expert answer

SIGN UP FOR FREE
There are two mistakes in your solution. The first is: Correct if varA=varB: if varA == varB: print("equal") print("equal") '=' is the assignment operator, and says 'put varB in varA' '==' is the equality operator, and says 'is varA equal to varB?' The second: if (type(varA) or type(varB))=='str': print ("string involved") Correct if type(varA) == type('a') or type(varB) == type('a'): print("string involved") type(varA) returns a 'type' object with the value of 'str', if varA is string. You can't compare a 'type' to a string (=='str'), so this doesn't work. Instead, compare the type of varA to the type of a string (for the example, I compared it to the type of 'a', you could do any string, or type(23) to get the 'int' type, etc.
yes BPX pointed out correctly about assignment and equality operator and for comparision part BPX made it complex, tough it would work but to check if it`s string or not no need to do type('a') just do str for the comparision of string use: if (type(varA) or type(varB)) == str: don`t do 'str' as this implies that you are comapring the type of variable to the string str which is incorrect so use str only as it`s a data type
use isinstance() for type checking http://docs.python.org/2.7/library/functions.html#isinstance http://docs.python.org/2.7/library/functions.html#type

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

http://dpaste.com/917499/
Just tried your method of type checking BPX, worked fine for me: name = 450 if(type(name) == str): print 'Yay a string' else: print 'No string here'
Another way of compare if your variables belong to type "string" is: if (type(a) == type(str()) or type(b) == type(str())):

Not the answer you are looking for?

Search for more explanations.

Ask your own question