计算机程序如何处理用户拼错的单词,迫使他们重新输入直到正确为止?e、 为性别争论输入男性和女性。我使用的是Python代码:
def mean(values):
length = len(values)
total_sum = 0
for i in range(length):
total_sum += values[i]
total_sum = sum (values)
average = total_sum*1.0/length
return average
name = " "
Age = " "
Gender = " "
people = []
ages = []
while name != "":
### This is the Raw data input portion and the ablity to stop the program and exit
name = input("Enter a name or type done:")
if name == 'done' : break
Age = int(input('How old are they?'))
Gender = input("What is their gender Male or Female?")
### This is where I use .append to create the entry of the list
people.append(name)
people.append(Age)
ages.append(Age)
people.append(Gender)
### print("list of People:", people)
#### useing the . count to call how many m of F they are in the list
print ("Count for Males is : ", people.count('Male'))
print ("Count for Females is : ", people.count('Female'))
### print("There ages are",ages)
### This is where I put the code to find the average age
x= (ages)
n = mean(x)
print ("The average age is:", n)
我也想把年龄限制在18-25岁之间。在
一直循环,直到他们给出有效的输入。对性别也一样。在
由于您还要求一种重新输入的方法,下面的代码片段使用格式为
\033[<N>A
的转义序列moves the cursor up N lines和Carriage Return转义序列\r
,打印无效数据并再次接受输入。在另一种解决方案是使用
\033[<N>D
形式的转义序列moves the cursor backward N columns。在只需使用while运算符,该运算符将一直持续到满足您希望满足的条件。在
相关问题 更多 >
编程相关推荐