Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the original question: Create a file called alternative.py Write a program that reads in a string and makes each alternate character into an

Here is the original question:
Create a file called alternative.py
Write a program that reads in a string and makes each alternate
character into an upper case character and each other alternate character
a lower case character.
e.g. The string Hello World would become HeLlO WoRlD
Now, try starting with the same string but making each alternative word
lower and upper case.
e.g. The string: I am learning to code would become i AM learning
TO code.
Tip: Using the split() and join() functions will help you here.
I received the following feedback and need help with improving my code please:
Please note that for the second task, you are supposed to use the same string that you used for task 1 instead of asking the user for another user input. That is the only missing requirement in this program. So basically making each alternative word lower and upper case for the same input string from task one.
Choosing good names for variables in coding is important. It makes the code easier to read and understand. Good names are clear and meaningful also help avoid mistakes and make it easier to improve or change the code later. While this is a small program, for larger programs you must do this, but you need to practice while on small programs. Please notice the "c" in line 9,"i" in line 29,"sw" in line 19, and "s" in line 37 these are not descriptive at all.
Please can someone help asap?alternative.py
C: > Users > User
#First, define a function called alternate_case which will take in a single input argument st which is a string
def alternate_case(st):
#use for loop to iterate through each character in the list, and check the index to allocate case.
#ensure whitespace is not included
flag = True
result =
for c in st:
if flag:
result += c.upper()
else:
result += c. lower ()
if c?',?' :
flag = not flag
return result
SW = input('Please enter your sentence for alternate letter case change: ')
print(alternate_case(sw)) #use print command to execute
#define a function called alternate_case which will take in a single input argument st which is a string
def alternate_word(st):
words = st.split() #use built-in "split()" method to separate the input string into a list of individual words
for i in range(len(words)):
if i%2=0 :
words [i]= words [i].upper()
else:
words [i]= words [i]*lower()
return ''.join(words) #built-in "join()" method used to concatenate the modified words into a single string and return the result
s = input('Please enter your sentence for alternate word case change: ')
print(alternate_word(s)) #use print command to execute
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions