Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

bigted5

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

  • one year ago
  • one year ago

  • This Question is Open
  1. BPX
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • one year ago
  2. harsimran_hs4
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • one year ago
  3. bwCA
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • one year ago
  4. bwCA
    Best Response
    You've already chosen the best response.
    Medals 0

    http://dpaste.com/917499/

    • one year ago
  5. Mathuba
    Best Response
    You've already chosen the best response.
    Medals 0

    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'

    • one year ago
  6. storozh
    Best Response
    You've already chosen the best response.
    Medals 0

    Another way of compare if your variables belong to type "string" is: if (type(a) == type(str()) or type(b) == type(str())):

    • one year ago
    • Attachments:

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.