<p>计算机程序如何处理用户拼错的单词,迫使他们重新输入直到正确为止?e、 为性别争论输入男性和女性。我使用的是Python代码:</p>
<pre><code>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 .<a href="https://www.cnpython.com/list/append" class="inner-link">append</a> 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)
</code></pre>
<p>我也想把年龄限制在18-25岁之间。在</p>
<p>一直循环,直到他们给出有效的输入。对性别也一样。在</p>
<pre><code>Age = ""
while True:
Age = int(input('How old are they?'))
if int(Age) >= 18 and int(Age) <= 25:
break
</code></pre>