In: Computer Science
PYTHON
1.
Write a for loop to iterate across a user entered string in
reverse order
Use an input statement to ask the user for a string using the
prompt: Cheer? [space after the ?] and assign to a variable
Start the for loop to iterate across the elements in the string in
reverse order
Display each element of the string on screen
After the for loop ends, display the entire string
2.
Write a for loop with a range function and format output as
currency
Use an input statement to ask the user for # of iterations using
the prompt: #? [space after the ?] & assign to a variable
Convert the variable to an integer
Use a for loop and a range function to display all whole numbers
from 1 to the user entered number
Display the value of the item variable on screen in each iteration
in the following format:
The price is [item variable in currency format]!
(hint: use the format specifier $%.2f)
After the for loop ends, display the user entered number of
iterations
3.
Ask the user to enter his/her class level using the following
prompt: Class Level? [space after the ?]
Write branching code that uses the user entered class level to
determine the enrollment date using the following:
Condition: class level is Senior
Action: assign the date May 1 to a variable
Condition: class level is Junior
Action: assign the date May 3 to a variable
Condition: class level is Sophomore
Action: assign the date May 5 to a variable
All other conditions
Action: assign the date May 7 to a variable
After the branching code, display the user's enrollment date using
the following format (note that the year is added)
Your enrollment date is [enrollment date], 2019.
(Tip: use the + concatenator)
4.
Use a for loop to display the elements in a tuple
Assign the following words to a variable as a tuple:
one,two,three,four,five
Ask the user for a phrase using the prompt: Phrase? [space after
?]
Start the for loop to iterate across the elements in the
tuple
Display each element of the tuple on screen
After the for loop ends, display the entire tuple using the
following format:
[user entered phrase] [tuple]
The solution for the above problem is given below with screenshots of the code for indentation with each part and if you feel any problem then feel free to ask,
1.
# taking input from the user by prompting the required message
inputStr = input("Cheer? ")
# iterating over the string in reverse order and printing each element of the string
for i in reversed(inputStr):
print(i, end='')
# printing the string entered by user after the for loop ends
print("\n"+inputStr)
2.
# asking the user to enter the number of iterations by prompting the required message
numIter = input("#? ")
# converting the input string to integer
numIter = int(numIter)
# using range function in for loop to iterate over the range from 1 to entered number
# here numIter+1 is used to include the numIter term in range also
# as the specified upperbound gets excluded in range function
for i in range(1,numIter+1):
# printing the whole number in each iteration with specified format
print("The price is $%.2f"%i,end = '')
print("!")
# printing the entered number of iterations by user
print("The entered number of iterations:", numIter)
3.
# asking the user to enter the class level of user by prompting the required message
classLevel = input("Class Level? ")
# checking the conditions using if, elif, and else then assigning the date accordingly
if classLevel == "Senior":
dateVar = "May 1"
elif classLevel == "Junior":
dateVar = "May 3"
elif classLevel == "Sophomore":
dateVar = "May 5"
else:
dateVar = "May 7"
# printing the enrollment date in the required format
print("Your enrollment date is "+dateVar+", 2019.")
4.
# assigning the required words to a tuple
tupleVar = ("one","two","three","four","five")
# asking the user to input the phrase
phrase = input("Phrase? ")
# iterating over the tuple using for loop and printing it's each element
for i in tupleVar:
print(i)
# printing phrase and tuple in the format required after for loop
print(phrase, tupleVar)