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 program that uses a for loop output a string in reverse printing it character...
Write a program that uses a for loop output a string in reverse printing it character by character. C++ Program Example: "Enter some text:" this is a test "your text in reverse is:" tset a si siht Please note that you can find the length of the string by using the length function: string str = "hello" cout << str.length(); Also you can et a character in a string by providing an index to the function string str = "hello"...
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
Using Python, use the following list (Temperature = [56.2,31.8,81.7,45.6,71.3,62.9,59.0,92.5,95.0,19.2,15.0]) to: - Create a loop to iterate...
Using Python, use the following list (Temperature = [56.2,31.8,81.7,45.6,71.3,62.9,59.0,92.5,95.0,19.2,15.0]) to: - Create a loop to iterate through each of the elements in the temperature list. - Convert each element of this list to a Celsius temperature and then, for each valid temperature in the list, print out both the original Fahrenheit temperature and the Celsius equivalent in this format: "32 degrees Fahrenheit is equivalent with 0 degrees Celsius."
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.
Write a python program that asks the user to enter a string containing numbers separated by...
Write a python program that asks the user to enter a string containing numbers separated by commas, e.g., s = '1.23,2.4,3.123', Your program should then calculate and print the sum of the numbers entered. Hint: you need to iterate over the string searching for the commas, i.e. their index. The first number is obtained by slicing between the start of the string and the index of the first comma. The second number is between the last comma and the next...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT