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 in under 10 minutes. Go to 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


To see the expert answer you'll need to create a free account at Brainly

In varA=varB you should give in == and not =..a single =is meant for assigning only
try out this syntax if type(varA) == str or type(varB) == str:
isinstance(thing, type) is the recommended way to check for an objects type

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

indeed, but this has not been "taught" by the time we get to this particular exercise!
Reposting from another question created by the same user asking the same thing... The problems comes from this statement: if varA=varB: You cannot use the = sign to compare variables, the = sign in Python (and any other languages) is used to assign values. Use == for comparison, and === for type-sensitive comparison. Also, the statement if (type(varA) or type(varB))=='str': is wrong. The or operator is used for logic, so (type(varA) or type(varB)) will return True (because type(varA/B) will return either str or int) and the whole statement will return to False (because True != "str"). There are 2 ways to do this, one is: if "str" in [type(varA), type(varB)]. This statement creates a temporary list and checks if either of them is a string. Another way to do this is: if type(varA) == "str" or type(varB) == "str". This is the correct way of showing the logic, but less elegant in my opinion.
i don't think the Pythom documentation is a restricted resource. if you can read and have the inclination, anything in the Python documentation should be fair game

Not the answer you are looking for?

Search for more explanations.

Ask your own question