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

martinhugh 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

  • one year ago
  • one year ago

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

    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.

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

    In addition, you are not using 'elif' in your code. You have an 'else:' followed by three 'if' statements. This is not correct. Review this portion in the notes. Lastly, in the equal to comparison you are using a single = sign. Go back and review this. A single equal sign is only used for assignment. Hope this helps!

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

    Also, you have omitted the parentheses in your conditional statements, you must have these. ex: if(a > b) .....

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

    I got the above code to work making only 2 changes. If varA=varB changed to If varA==varB <-- two == for equality and == 'str' changed to ==type('') <--compares to type known to be string

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

    BTW, the last part type('') just has two apostrophes in the brackets (empty string).

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