Question

In: Computer Science

Python: IDLE exercise 1. Start by running, in the shell, the following assignment statement: s1 =...

Python:

IDLE exercise

1. Start by running, in the shell, the following assignment statement:

s1 = 'abcdefghijklmnopqrstuvwxyz'

Now write expressions—on separate lines of code—using sting s1 and the indexing operator that evaluate to 'm', 'p', 'z', 'e', 'a'.

2. Write Python statements that correspond to the following:

a. assign to variable flowers a list containing strings 'rose', 'bougainvillea', 'yucca', 'marigold', 'daylily', and 'lily of the valley'.

b. Write Boolean expressions that evaluate to True if the string ‘carrot’ is in the list of flowers, and evaluate the expression.

c. Assign to list thorny the sublist consisting of the three objects in the list flowers.

d. Assign to list poisonous the sublist consisting of just the last object of list flowers.

e. Assign to list dangerous the concatenation of lists thorny and poisonous.

Programming Problems

For the following two problems, create a module called a1.py and write the code for the following programs in it. Do NOT forget the required comments at the top.

  1. Write a program that requests as input from the user their lastName, firstName, month they were born, and day they were born. Print the user’s firstName, lastName, and dob in the format below.

For example, if the user inputs Eve, Lulis as the first and last name respectively, and December and 11 as the month and day of their birth, the program will print the following:

Usage (after pressing the F5 button):

>>>Please enter your first name: Eve

>>>Please enter your last name: Lulis

>>>Please enter the month of your birth spelled out: December

>>>Please enter the day of your birth as a number: 11

Hello, Eve Lulis!

You were born on the 11 day of December.

  1. Implement a program that requests a decimal value representing a restaurant bill total and an integer representing a percentage.  The program should compute the dollar amount of the tip and print all three values to the screen. You may assume that the user enters numbers for the two values and that the numbers are not negative. In addition to using the test case below, you need to write at least two other test cases for your code. Include the program you wrote and the runs of your test cases in the a1.txt file.

Usage (after pressing the F5 button):

>>> Enter a restaurant bill (> 0): 45.37

>>> Enter the tip percentage (e.g. 15 = 15%): 20

The tip for a bill of $45.37 at 20% is 9.074.

Solutions

Expert Solution

1.

>>> s1 = 'abcdefghijklmnopqrstuvwxyz'
>>> s1[s1.index('m')]
'm'
>>> s1[s1.index('p')]
'p'
>>> s1[s1.index('z')]
'z'
>>> s1[s1.index('e')]
'e'
>>> s1[s1.index('a')]
'a'

2.

>>> flowers
['rose', 'bougainvillea', 'yucca', 'marigold', 'daylily', 'lily of the valley']
>>> 'carrot' in flowers
False
>>> thorny = flowers[:3]
>>> thorny
['rose', 'bougainvillea', 'yucca']
>>> poisonous = flowers[-1:]
>>> poisonous
['lily of the valley']

dangerous = thorny + poisonous
>>> dangerous
['rose', 'bougainvillea', 'yucca', 'lily of the valley']

a1.py

fname = input("Please enter your first name: ")
lname = input("PLease enter your last name: ")
month = input("Please enter the month of your birth spelled out: ")
day = input("Please enter the day of your birth as a number: ")

print("Hello,", fname, lname, "!");
print("You were born on the", day, "day if", month)

Please enter your first name: Eve
PLease enter your last name: Lulis
Please enter the month of your birth spelled out: December
Please enter the day of your birth as a number: 11
Hello, Eve Lulis !
You were born on the 11 day if December

billing.py

Enter a restaurant bill (> 0): 45.37
Enter the tip percentage (e.g. 15 = 15%): 20
The tip for a bill of $ 45.37 at 9.074


Related Solutions

Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program:...
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program: Your program will create a username of your choice using a Kali Linux command "useradd -m mark", then set the password for that user using the Kali Linux Command "echo "mark:10101111" | chpasswd". Then you create a dictionary file using the Kali Linux command "crunch 8 8 01 > mylist.txt" Your python script should crack the password for that user and display on the...
In IDLE - Python 3, do the following: 1. Create File --> New 2. Enter the...
In IDLE - Python 3, do the following: 1. Create File --> New 2. Enter the code below in the new file (you may omit the comments, I included them for explanation #Python Code Begin x = int(input("Enter a number: ")) y = int(input("Enter another number: ")) print ("Values before", "x:", x, "y:", y) #add code to swap variables here #you may not use Python libraries or built in swap functions #you must use only the operators you have learned...
Please use python and run in IDLE Question 1 Write functions that do the following: i)...
Please use python and run in IDLE Question 1 Write functions that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both. Question 2 Write functions: i) One that prompts a user for 2 numbers. ii)...
4.9 Branching Practice - Python Exercise #Write the code for the following # Exercise 1 #...
4.9 Branching Practice - Python Exercise #Write the code for the following # Exercise 1 # Ask user for a number using an input statement (no prompt) & assign the input to a variable # Convert the user entry to an integer and assign it to a variable # Condition: user entry is greater than zero # Action: display the following word on screen: positive # Exercise 2 # Ask user for a number using an input statement (no prompt)...
BEFORE YOU START: Before working on this assignment you should have completed Assignment 1. PROGRAM STATEMENT...
BEFORE YOU START: Before working on this assignment you should have completed Assignment 1. PROGRAM STATEMENT AND REQUIREMENTS: You will implement in Java the WIFI troubleshooting program you created in Assignment 1 with the following additional conditions: At the end of the program, you will ask if the client wants to purchase a regular router for $50 or a super high-speed router for $200, adding the cost to the total amount the client needs to pay. Once your program calculates...
In this assignment you will start with Python code that builds a 2 host network connected...
In this assignment you will start with Python code that builds a 2 host network connected by a legacy router. You will modify it such that the two hosts can send packets to each other. It requires that you understand subnet addressing and the function of a router. The network built using Miniedit on a Mininet virtual machine: python mininet/examples/miniedit.py. We need to also make 6 static routes to make the program work. This is the work that I have...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
4.10 Branching Practice 2 - Python Exercise #Write the code for the following #Exercise 1 #Ask...
4.10 Branching Practice 2 - Python Exercise #Write the code for the following #Exercise 1 #Ask the user to input a name using an input statement (no prompt) and assign it to a variable #Condition: user input is equal to 'Joseph' #Action: display the user input on screen #Exercise 2 #Ask the user to input his/her first name using an input statement (no prompt) and assign it to a variable #Condition: user input is equal to 'Joseph' #Action: display the...
THIS IS A PYTHON ASSIGNMENT 1. The following are invalid variable names: 3g, 87 and 2h....
THIS IS A PYTHON ASSIGNMENT 1. The following are invalid variable names: 3g, 87 and 2h. 2. The operator != is an example of a relational operator. 3. A variable name identifies the kind of information stored in the object. 4. Python treats the variable names, a1 and A1, as the same variable. 5. The if/else selection structure is a single-selection structure. 6 A fatal logic error causes a program to execute and produce incorrect results. 7) A repetition structure...
I have the following python code. I get the following message when running it using a...
I have the following python code. I get the following message when running it using a test script which I cannot send here: while textstring[iterator].isspace(): # loop until we get other than space character IndexError: string index out of range. Please find out why and correct the code. def createWords(textstrings): createdWords = [] # empty list for textstring in textstrings: # iterate through each string in trxtstrings iterator = 0 begin = iterator # new begin variable while (iterator <...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT