Question

In: Computer Science

PYTHON 1. Write a for loop to iterate across a user entered string in reverse order...

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]

Solutions

Expert Solution

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)


Related Solutions

Write a Python loop that goes through the list and prints each string where the string...
Write a Python loop that goes through the list and prints each string where the string length is three or more and the first and last characters of the strings are the same. Test your code on the following three versions of the list examples: examples = ['abab', 'xyz', 'aa', 'x', 'bcb'] examples = ['', 'x', 'xy', 'xyx', 'xx'] examples = ['aaa', 'be', 'abc', 'hello'].
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
Write a Flask app in which a Python script prompts the user to enter a string...
Write a Flask app in which a Python script prompts the user to enter a string and then that string, as entered, gets displayed on an otherwise blank HTML file, called output.html. Show the Python/Flask code and the html code of the output.html file, one below the other as part of your answer.
(using for loop, parameters, if, else condition only )Python: Write a program that prompts the user...
(using for loop, parameters, if, else condition only )Python: Write a program that prompts the user to enter a positive integer value, and compute the following sequence: • If the value is even, halve it. • If it's odd, multiply by 3 and add 1. • Repeat this process until the value is 1, printing out each value. • Then print out how many of these operations you performed. If the input value is less than 1, print a message...
Using Python Write a program that does the following in order: 1.     Asks the user to enter...
Using Python Write a program that does the following in order: 1.     Asks the user to enter a name 2.     Asks the user to enter a number “gross income” 3.     Asks the user to enter a number “state tax rate” 4.     Calculates the “Federal Tax”, “FICA tax” and “State tax” 5.     Calculates the “estimated tax” and round the value to 2 decimal places 6.     Prints values for “name”, “gross income” and “estimated tax” The program should contain three additional variables to store the Federal tax, FICA...
PYTHON Let n denote an integer entered by the user. Write a program to print n...
PYTHON Let n denote an integer entered by the user. Write a program to print n multiples of 5 in the descending order, with the last number being 5. Print the average of those n multiples
using java language "Data Structures" I have to write program which stores string entered by user...
using java language "Data Structures" I have to write program which stores string entered by user into cursor array implementation here is the code public static void main(String[] args) { CursorArray sorted =new CursorArray();//the strings must added here how can i store them                  String []inputs = new String[50];                   for (int i=0; i< 50; i++) {            System.out.println("Enter the words you want to sort and use exit to stop");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT