In: Computer Science
Python Programming, Could you also show the right indents in Python Shell. Below are the skeleton of the program, and the input and output.
# Initialize list
mylist = [ ]
# Set up loop to put menu on screen
num = 1
while num != 0:
print(" ")
print(" ")
print(" Menu ")
print ("0 - Quit")
print ("1 - Add item to list")
print ("2 - Pop item off list and print it")
print ("3 - Print list")
print ("4 - Sort list")
print ("5 - Reverse list")
snum = input("Enter choice from menu ")
num = int(snum)
print (" ")
Put your if or if-else or if-elif statements here
(They will be inside the while
loop)
You should start this lab by entering the above program and saving it as Week5LabA in your Python Programs folder. Then run it to see how it displays the menu. Note that you can enter 0, 1, 2, 3, 4, or 5 in response to the menu choices. Your entry will be assigned to the variable num. Entering a 0 will end the program.
Your other entries (1, 2, 3, 4, or 5 ) should cause something to be done with the list that is called mylist. You will note that mylist has been initialized to the empty list with the mylist = [ ] statement.) For example, if you enter a 1 (value of num is 1), the program should ask you to enter a value to be placed on the list and then append it onto the list. As another example, if you enter a 3 (value of num is 3), the program should print the list using the statement, print(mylist).
The video below shows what the original program (the one I gave you above) should do and how you can enter the if statement for a value of 1 for num.
View video to get started=> https://youtu.be/PxTwMpBFFas
The following is a sample execution of the program. The following integers were placed on the list: (Values entered by the user are in red.)
Menu
0 - Quit
1 - Add item to list
2 - Pop item off list and print it
3 - Print list
4 - Sort list
5 - Reverse list
Enter choice from menu 1
Enter value to put on list 3
Menu
0 - Quit
1 - Add item to list
2 - Pop item off list and print it
3 - Print list
4 - Sort list
5 - Reverse list
Enter choice from menu 1
Enter value to put on list 7
Menu
0 - Quit
1 - Add item to list
2 - Pop item off list and print it
3 - Print list
4 - Sort list
5 - Reverse list
Enter choice from menu 1
Enter value to put on list 9
The following operations were performed on the list:
Menu
0 - Quit
1 - Add item to list
2 - Pop item off list and print it
3 - Print list
4 - Sort list
5 - Reverse list
Enter choice from menu 3
[3, 7, 9]
Menu
0 - Quit
1 - Add item to list
2 - Pop item off list and print it
3 - Print list
4 - Sort list
5 - Reverse list
Enter choice from menu 4
Menu
0 - Quit
1 - Add item to list
2 - Pop item off list and print it
3 - Print list
4 - Sort list
5 - Reverse list
Enter choice from menu 5
Menu
0 - Quit
1 - Add item to list
2 - Pop item off list and print it
3 - Print list
4 - Sort list
5 - Reverse list
Enter choice from menu 3
[9, 7, 3]
Menu
0 - Quit
1 - Add item to list
2 - Pop item off list and print it
3 - Print list
4 - Sort list
5 - Reverse list
Enter choice from menu 2
Value popped = 3
Menu
0 - Quit
1 - Add item to list
2 - Pop item off list and print it
3 - Print list
4 - Sort list
5 - Reverse list
Enter choice from menu 3
[9, 7]
# Initialize list
mylist = [ ]
# Set up loop to put menu on screen
num = 0
print(" ") #printing menu
print(" ")
print("
Menu ")
print ("0 - Quit")
print ("1 - Add item to list")
print ("2 - Pop item off list and print it")
print ("3 - Print list")
print ("4 - Sort list")
print ("5 - Reverse list")
num = int(input("Enter choice from menu ")) #taking choice
while num != 0: #if option is not 0
if num==1: #if option is 1
ele=int(input("Enter value to put
on list ")) #taking element from user
mylist.append(ele) #adding element
to mylist
elif num==2: #if option is 2
if (len(mylist)>=1): #if length
of the mylist is greater than 0
ele=mylist.pop()
#pop element
print("Value
popped =",ele) #print popped element
else: #if there is no element
print("No value
to pop")
elif num==3: #if option is 3
print(mylist) #print list
elif num==4: #if option is 4
mylist.sort() #sort list
elif num==5: #if option is 4
mylist.reverse() #reverse the
list
print(" ")
print(" ")
print("
Menu ")
print ("0 - Quit")
print ("1 - Add item to list")
print ("2 - Pop item off list and print it")
print ("3 - Print list")
print ("4 - Sort list")
print ("5 - Reverse list")
num = int(input("Enter choice from menu "))
print("Quit")
If you have any doubt comment me.