Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Struggling on Tuples
#1
Hello, I am brand new to python and I am highly willing to learn it. The problem is that I've been set homework on Tuples and I feel I've not been prepared well enough for it. Generally struggling to take any new information in this pandemic and I spent hours researching information and I still feel clueless.

The code at hand is here:

def string_fun(string):
    '''
    Return a tuple with three elements.
    The first element is True if the string contains only alphabet characters, otherwise False.
    The second element is True if the string ends with an exclamation mark ('!'), otherwise False.
    The third element is the string with all spaces (' ') replaced with hyphens ('-').
    Arguments
    string: a string
    Examples
    string_fun('Hello World!') returns (False, True, 'Hello-World!')
    string_fun('ThisIsAChallenge') returns (True, False, 'ThisIsAChallenge')
    '''

    # ====================================
    # Do not change the code before this

    # CODE1: Write code that will assign details with the appropriate tuple

    

    # ====================================
    # Do not change the code after this

    return details


if __name__ == '__main__':
    print(string_fun('Hello World!'))
    print(string_fun('ThisIsAChallenge'))

------------------------------------------------

From my interpretation of Code 1, I tried

    details = (True, False, '-')

    for alpha in details:
        if string_fun.isalpha():
            print(details[0])
        else:
            print(details[1])

    for symbol in details:
        if string_fun.endswith('!'):
            print(details[0])
        else:
            print(details[1])
Once I submit my answers it does not tell me if I am on the right track. It either says correct or wrong since it's marked by a computer and not a person. I'd like to know what to do because I feel clueless and also what is the correct code for this. P.S I am missing code for the final element which is replacing a space in the string with the hyphen element.
buran write Jan-28-2021, 12:06 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Why so complicated?

  1. The first element is True if the string contains only alphabet characters, otherwise False.
    Use: str.isalpha
  2. The second element is True if the string ends with an exclamation mark ('!'), otherwise False.
    Use: str.endswith
  3. The third element is the string with all spaces (' ') replaced with hyphens ('-').
    Use: str.replace

It's a simple assignment and afterwards you have everything, you create the final tuple.
    alpha = string.isalpha()
    exclamation = string.endswith("!")
    text = string.replace(" ", "-")
    details = (alpha, exclamation, text)
nilamo likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Thank you, I wasn't aware you could use tuples in that way and I understand it a little bit more now. The answer you provided came correct.
Reply
#4
A tuple is just a fancy word for a collection of items that can't be changed once it's created. Like a cardboard box, with stuff in it, that you tape shut.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Struggling with variables pythonstudy00 1 2,037 Sep-23-2020, 06:39 AM
Last Post: DPaul
  Struggling To Understand These 2 Python Files samlee916 2 2,682 Sep-22-2020, 04:50 AM
Last Post: ndc85430
  struggling w/ fonctions in Python Tiril123 7 5,002 May-09-2020, 10:51 AM
Last Post: pyzyx3qwerty
  struggling with one part of python Lucifer 23 9,626 May-08-2020, 12:24 PM
Last Post: pyzyx3qwerty
  struggling with python save/load function newatpython00 1 2,549 Nov-15-2019, 07:58 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020