Question

In: Computer Science

1. Write Python code to - (a) ask user to enter a list of animals, one...

1. Write Python code to -
(a) ask user to enter a list of animals, one item at a time, until “done” to terminate input
(b) randomly display a selected item from the list, starting with “I like ______”.

When run, the output should look something like this:

Enter an item; 'done' when finished: cats
Enter an item; 'done' when finished: dogs
Enter an item; 'done' when finished: guinea pigs
Enter an item; 'done' when finished: done
You are done entering items.
I like guinea pigs

2. Given the following two strings:

str_1 = "the rain! in Spain falls mainly on the plains"

str_2 = "cats don't like dogs and mice. What a day."

Implement logic to produce the following output by manipulating str_1 and str_2.      

# Output ---> rain! What a day.

Solutions

Expert Solution

1)

Python code:

from random import randint
#initializing lis
lis=[]
#accepting animal name
item=input("Enter an item; 'done' when finished: ")
#looping till done is entered
while(item!="done"):
    #adding it to lis
    lis.append(item)
    #accepting next item
    item=input("Enter an item; 'done' when finished: ")
#printing done entering items
print("You are done entering items.")
#printing a random item from list
print("I like",lis[randint(0,len(lis)-1)])

Screenshot:


Input and Output:

2)

Python code:

#initializing str_1
str_1="the rain! in Spain falls mainly on the plains"
#initializing str_2
str_2="cats don't like dogs and mice. What a day."
#printing rain! from str_1 and What a day from str_2
print(str_1[4:10]+str_2[-11:-1])

Screenshot:


Output:


Related Solutions

In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
Write a C++ code to perform the following steps: 1. Ask the user to enter two...
Write a C++ code to perform the following steps: 1. Ask the user to enter two whole numbers number1 and number2 ( Number 1 should be less than Number 2) 2. calculate and print the sum of all even numbers between Number1 and Number2 3. Find and print all even numbers between Number1 and Number2 4. Find and print all odd numbers between Number1 and Number2 5. Calculate and print the numbers and their squares between 2 and 20 (inclusive)...
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the...
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the user to enter the student names and scores as shown. Output the name and score of the student with the 2nd lowest score. NOTE: You may assume all students have distinct scores between 0 and 100 inclusive and there are at least 2 students NOTE: You may only use what has been taught in class so far for this assignment. You are not allowed...
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 mips assembly code program that ask the user to enter an integer value, and...
Write a mips assembly code program that ask the user to enter an integer value, and then print the result of doubling that number.
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order.
Please write in Python code please: Write a program that asks the user to enter 5...
Please write in Python code please: Write a program that asks the user to enter 5 test scores between 0 and 100. The program should display a letter grade for each score and the average test score. You will need to write the following functions, including main: calc_average – The function should accept a list of 5 test scores as an input argument, and return the average of the scores determine_grade – The function should accept a test score as...
Task 1. creating a nested if:   Need a python program that ask the user to enter...
Task 1. creating a nested if:   Need a python program that ask the user to enter the annual income of an employee and the years of experience. Pass these data to a function. The function decides if an employee does qualify for a loan or does not, then it prints a message based on the following rules: If the income is equal or greater than $40000, the function checks if the employee's years of experience is greater than 4, if...
1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT