0

I am trying to add a way to ask the user what problem(1-5)they would like to run of the program. This should be repeated until the user enters an invalid input.

I think I just do not understand the logic behind it. I have looked into a lot and I still don't understand what I can do to separate the problems.

Here is the code that I have to work with:

#Problem 1
#create variable called fruits, store listed fruits, print num of items in list
#insert Orange, append Plum, remove Melon, reprint
fruits = ['Apple', 'Banana', 'Cherry', 'Kiwi', 'Melon', 'Mango']
print("Initial fruits list:", '\n', fruits)
print("-"*56)

q1= len(fruits)
print ("q1: the len of fruits is", q1)

fruits.insert(3, 'Orange')
print("q2: inserted Orange between Cherry and Kiwi:", '\n', fruits)

fruits.append('Plum')
print("q3: appended plum to the end:", '\n', fruits)

##fruits.insert(7, 'Plum')  #testing if insert works
##print(fruits)

print("Can use insert function to add an item to the end of the list")

fruits.remove('Melon')
print("q4: removed melon from the list:", '\n', fruits)

#Problem 2
#prompts user for a name, stores in variable called n
#display how many times lowercase 'a' appears within name

n= input("Please enter a name:")
char = 'a'

count = 0
for i in range(len(n)):
    if(n[i]== char):
       count = count+1
print("character", char, "appears", count, "time(s) within the name", n)

#Problem 3
#modify previous code to accept both uppercase and lowercase
#use upper() and lower() functions
n= input("Please enter a name:")
def eCount(n):
   return n.lower().count('a')
print("character a appears", eCount(n), "time(s) within the name", n)

#Problem 4
#Prompts user 5 times for integer, stores in list, display list
#print min, max, and avg
num1=int(input("Please enter a number:"))
num2=int(input("Please enter a number:"))
num3=int(input("Please enter a number:"))
num4=int(input("Please enter a number:"))
num5=int(input("Please enter a number:"))

list= [num1,num2,num3,num4,num5]
print("Number list is:", list)

minlist= min(list)
maxlist= max(list)
avglist= num1+num2+num3+num4+num5 /5

print("Min value is", minlist, ",","the max value is", maxlist,",", "and the average is", avglist)

#Problem 5
#create variables numList and numTuple, use slice operator, index() function
#membership operator, concatenate 2 sequences
numList= [-4,0,2,10]
print("Original List:", numList)
numTuple= (2,3,1)
print("Original Tuple:", numTuple)
print("-"*40)

print("Using slice operator on numList:",numList[1:3])
print("Using slice operator on numTuple:",numTuple[1:])
print("-"*40)

indexL= numList.index(2)
print("The index number of 2 in numList is",indexL)
indexT= numTuple.index(2)
print("The index number of 2 in numTuple is", indexT)
print("-"*40)

if -4 in numList:
    print("-4 is a member of numList:",True)
else:
    print("-4 is a member of numList:",False)

if -4 in numTuple:
    print("-4 is a member of numTuple:",True)
else:
    print("-4 is a member of numTuple:",False)
print("-"*40)

convTuple= list(numTuple)
concList= numList+convTuple
print("Concatenated as a list:", concList)

convList= tuple(numList)
concTuple= convList+numTuple
print("Concatenated as a list:", concTuple)
kit
  • 1
  • 1

1 Answers1

0

Are you talking about something like this?

def f():
    while True:
        take = input('Which problem you want to select?')
        try:
            id = int(take)
            if id > 0 and id <=5:
                return id
        except Exception as e:
            pass

if __name__ == "__main__":
    id = f()
    print(f'user selected {id}')
Hanzhou Tang
  • 351
  • 2
  • 6