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.
Using Python write a program that does the following in order: 1. Ask user to enter...
Using Python write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out...
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)...
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 (Number1 should be less than number2) 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 1 and 20 (inclusive) 6. Calculate and...
Write, save, and run a Python program Ask the user to enter a number of grams....
Write, save, and run a Python program Ask the user to enter a number of grams. Your program converts that to whole tones, then whole kilograms and whatever is left is in grams. It prints the entered grams , tones , kilograms and the remaining grams.      4 marks for i in range(0,10): print(i) 2    what is wrong in the above python code? display your explanation using print statement. mark 3    insert the following comments into your program of part...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they type DONE to quit. The program should DOUBLE each of the digits in the number and display the original entry AND the SUM of the doubled digits. Hint: Use the // and % operators to accomplish this. Print the COUNT of entries that were invalid Examples: if the user enters 93218, the sum of the doubled digits is 18+6+4+2+16 = 46. User Entry Output...
Write a Python program to: ask the user to enter two integers: int1 and int2. The...
Write a Python program to: ask the user to enter two integers: int1 and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised to the int2 power. You also want to calculate the result when int1 is raised to the .5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int1 that is entered is a negative number,...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT