コンピュータプログラムは、単語が間違って入力されるように単語のスペルミスを処理する方法を教えてください。例えばジェンダーの議論のために男性と女性を入力する。私はこのPythonコードを使用しています: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の範囲で年齢を強制したいと思います。
ネストした 'while'ループを使ってみましたか? –
ネストされたwhileループで名前が 'Male'または' Female'でないことを確認してから、もう一度 – Li357