A community for students.
Here's the question you clicked on:
 0 viewing
 2 years ago
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<varB:
print("smaller")
Shows wrong
 2 years ago
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<varB: print("smaller") Shows wrong

This Question is Open

BPX
 2 years ago
Best ResponseYou've already chosen the best response.1There 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.

harsimran_hs4
 2 years ago
Best ResponseYou've already chosen the best response.0yes 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

bwCA
 2 years ago
Best ResponseYou've already chosen the best response.0use isinstance() for type checking http://docs.python.org/2.7/library/functions.html#isinstance http://docs.python.org/2.7/library/functions.html#type

Mathuba
 2 years ago
Best ResponseYou've already chosen the best response.0Just 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'

storozh
 one year ago
Best ResponseYou've already chosen the best response.0Another way of compare if your variables belong to type "string" is: if (type(a) == type(str()) or type(b) == type(str())):
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.