Home > other >  Using the function mixedcase
Using the function mixedcase

Time:10-10

I'm new to coding, I'm using python, and I'm trying to make my input gives an output in mixed case, but it is returning an error. Here is my line of code

transport = input("what type of transport do you take to school? ")
transport = transport.mixedcase()
print(transport)

The error I get is object has no attributes in mixed case

CodePudding user response:

Make a for loop through the word and add the upper and lower case letters to a new string

word = "word"
copy = ''
low = False
for i in word:
    if low == False:
        copy  = i.upper()
        low = True
    else:
        copy  = i.lower()
        low = False

print(copy)

you can also create is as a function

def mixedcase(word):
    copy = ''
    low = False
    for i in word:
        if low == False:
            copy  = i.upper()
            low = True
        else:
            copy  = i.lower()
            low = False
    print(copy)

mixedcase("what type of transport do you take to school? ")

so you can call it

CodePudding user response:

Your reference to "mixed case" is a bit vague. If by mixed case, you mean to have the first character of your input capitalized, then you could use the "capitalize()" function as follows.

transport = input("what type of transport do you take to school? ")
transport = transport.capitalize()
print(transport)

Here is a sample of the input and output.

@Una:~/Python_Programs/Mixed$ python3 Mixed.py 
what type of transport do you take to school? bus
Bus

Give that a try if that's the type of result you are after.

  • Related