Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
shashiverma
Group Title
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
 2 years ago
shashiverma Group Title
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
 2 years ago

This Question is Open

krithika Group TitleBest ResponseYou've already chosen the best response.2
In varA=varB you should give in == and not =..a single =is meant for assigning only
 2 years ago

krithika Group TitleBest ResponseYou've already chosen the best response.2
try out this syntax if type(varA) == str or type(varB) == str:
 2 years ago

bwCA Group TitleBest ResponseYou've already chosen the best response.0
isinstance(thing, type) is the recommended way to check for an objects type http://docs.python.org/library/functions.html#type
 2 years ago

Chris2332 Group TitleBest ResponseYou've already chosen the best response.0
indeed, but this has not been "taught" by the time we get to this particular exercise!
 2 years ago

tyteen4a03 Group TitleBest ResponseYou've already chosen the best response.0
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 typesensitive 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.
 2 years ago

bwCA Group TitleBest ResponseYou've already chosen the best response.0
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
 2 years ago
See more questions >>>
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.