- 此问题在此处已有答案**:
Asking the user for input until they give a valid response(22个答案)
2天前关闭。
我试图添加一种方法来询问用户他们希望运行程序的问题(1 - 5)。这应该重复,直到用户输入无效的输入。
我想我只是不明白背后的逻辑,我已研究了很多,但我仍然不明白我可以做些甚么来把问题分开。
下面是我必须使用的代码:
#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)
1条答案
按热度按时间2guxujil1#
你说的是这种事吗?